Using an IR remote with Arduino to display numbers

Seven segment display controlled with IR remote

In this tutorial I will be explaining how to use an Infrared remote to control your Arduino. In this project we will be using a remote, an infrared receiver and a seven segment display. All of these are all included in our ultimate starter kit.

Infrared communication in remotes with Arduino

Infrared communication is used all over the world in pretty much any remote that you will be able to find. Every remote follows a certain protocol to encode messages, thus we will use a library to decode them.

Wiring the receiver up

Luckily, the wiring of this project is reasonably easy, that is we will only be needing a 220 ohm resistor some wires and the modules. First of all, we will be hooking the Ir receiver. The receivers come in two forms, stand-alone diodes and on breakout boards. They are both used the exact same way, but the wiring is slightly different.

Pinout for standalone IR receiver for using remote with Arduino.
Pinout for breakout IR receiver for using    a remote with arduino.

The picture on the left is a stand alone diode, which we have included in our ultimate starter kit. The other picture is one on a breakout board which we included in our sensor kit. Connect GND to GND, Vcc to 5V and S to any digital pin (pin 7 in our case).

Wiring the seven segment display.

To display the number we are pressing on the remote, we will use a seven segment display. We have already discussed seven segment displays in this tutorial, though it is reasonably easy to follow without it. We will use ten wires as well as a 220 ohm resistor. Just follow the schematic below, and you won’t have any problems hooking it up.

Arduino IR schematic with seven segment display

Installing the library

Now that we have hooked everything up, we can start programming. We’ll be using a library to use the IR receiver which is named IRremote. To install it press Tools --> Manage Libraries and search for IRremote, it is the one by shirriff and z3t0.

Defining variables

After installing the library, we can start writing the first piece of code. First of all, we’ll have to include the library. Then we need to define all of the numbers, meaning we tell the code which pins belong to which number. Then we define some things that we need for the library. Finally we go through the pins on number eight,

#include <IRremote.h> //Including the library
 int n0[] = {3, 2, 8, 9, 10, 4};
 int n1[] = {4, 10};
 int n2[] = {3, 2, 5, 10, 9};
 int n3[] = {3, 2, 5, 8, 9};
 int n4[] = {4, 5, 2, 8};
 int n5[] = {3, 4, 5, 8, 9};
 int n6[] = {3, 4, 5, 8, 9, 10};
 int n7[] = {3, 2, 8};
 int n8[] = {3, 2, 5, 8, 9, 10, 4};
 int n9[] = {3, 2, 5, 8, 9, 4};

 IRrecv irrecv(7);// We are using pin 7 for the IR receiver
 decode_results results;
 unsigned long key_value = 0;

 void setup(){
   irrecv.enableIRIn();
   for (int i = 0; i < sizeof(n8)/sizeof(int);i++){
     pinMode(n8[i], OUTPUT);
   }
 }

Main loop and Function

In the second part of the program, we will actually read off the sensor and put it on the display. First of all, we will make a function that will draw out the number. In the loop we check which number as been sent, and draw the number accordingly.

 int draw(int number[], int sizes){
   for (int i = 0; i < sizeof(n8)/sizeof(int);i++){
     digitalWrite(n8[i], LOW);
   }
   for (int i = 0; i < sizes/sizeof(int);i++){
     digitalWrite(number[i], HIGH);
   }
}
void loop(){
   if (irrecv.decode(&results)){
     if (results.value == 0XFFFFFFFF)
       results.value = key_value;
     switch(results.value){
       case 0xFF6897:
       draw(n0, sizeof(n0));
       break ;
       case 0xFF30CF:
       draw(n1, sizeof(n1));
       break ;
       case 0xFF18E7:
       draw(n2, sizeof(n2));
       break ;
       case 0xFF7A85:
       draw(n3, sizeof(n3));
       break ;
       case 0xFF10EF:
       draw(n4, sizeof(n4));
       break ;
       case 0xFF38C7:
       draw(n5, sizeof(n5));
       break ;
       case 0xFF5AA5:
       draw(n6, sizeof(n6));
       break ;
       case 0xFF42BD:
       draw(n7, sizeof(n7));
       break ;
       case 0xFF4AB5:
       draw(n8, sizeof(n8));
       break ;
       case 0xFF52AD:
       draw(n9, sizeof(n9));
       break ;
           }
     key_value = results.value;
     irrecv.resume(); 
 }
 }

After uploading you should now see the display light up. Now you now how to use an IR Remote, and a seven segment display with an Arduino.

Leave a Reply

[]