Arduino Lesson #4 – programming IR Remote control

In this new post at my technic blog, I like to show you how to code an IR Remote control at Arduino UNO R3.

Arduino Lesson #4 - programming IR Remote control
Arduino Lesson #4 – programming IR Remote control

In my last post Arduino Lesson #3 – control tiny servo motor SG90 I show you how to code and control a servo motor. In this post, I want to control this servo motor with an IR remote control.

Required components

If you want to rebuild the following samples, you need:

required parts for samples with IR Remote control
required parts for samples with IR Remote control

You don’t have to buy an IR remote control, for this post you can also use your remote control from your TV or satellite receiver. This will save some money for other upcoming lessons.

IR Remote control module

The easiest way to read signals from an IR Remote control is to use a module. This module has 3 pins.

  • – > GND
  • middle > 5V
  • S > data line

Connect an IR Remote control module at an Arduino UNO

The connection to an Arduino UNO is very simple, you only have to connect three wires. The pin with symbol minus goes to GND, the middle pin to 5V and the pin with “S” have to connect to a digital pin.

connect IR Remote module to Arduino UNO
connect IR Remote module to Arduino UNO

Build your own tiny IR Receiver module at Breadboard

An IR Remote control module can easily build with the following parts:

tiny circuit IR LED & 3mm LED at Arduino Nano
tiny circuit IR LED & 3mm LED at Arduino Nano
easy IR Receiver with IR LED and LED at Arduino Nano
easy IR Receiver with IR LED and LED at Arduino Nano

Programming an IR Remote control Module

The next following samples will work with the ready to use module and the DIY IR Receiver circuit.

First, you need a library to read the IR receiver led signals, in your Arduino IDE is such a library pre-installed. So you don’t have to install this.

Here is a small sample of how to read and handle such signals.

//library to handle IR Receiver Diode
#include <IRremote.h>

//tiny, 3 mm Status LED at digital pin 13
#define statusLed 13
//IR Receiver Diode connected at digital pin 8
#define irReceiverDiode 8

//some connected LEDs
#define ledGreen 3
#define ledYellow 4
#define ledRed 5
#define ledBlue 6

//create object with attached
IRrecv irrecv(irReceiverDiode);
//field for results
decode_results results;

void setup() {
  //define pins from LEDs as OUTPUT
  pinMode(ledGreen, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(statusLed, OUTPUT);

  //define pin from IR Receiver Diode as INPUT
  pinMode(irReceiverDiode, INPUT);

  //begin communication
  irrecv.enableIRIn();
  //begin serial communication with 9600 baud
  Serial.begin(9600);
}

void loop() {
  //if successful receive some data, then ...
  if (irrecv.decode(&results)) {
    //store data in field value
    int value = results.value;
    //consume the data and clear
    irrecv.resume();
    //print the value to the serial interface
    Serial.println(value);
    //activate status LED
    digitalWrite(statusLed, HIGH);
    //tiny brake to light up the LED
    delay(75);
    //deactive status LED
    digitalWrite(statusLed, LOW);
  }
}

When you run this sample and press buttons on your IR remote control, the value from your key will write to the serial interface.

Next, you can use this numbers in a Switch-Case construct to control some LEDs.

//fields to store status of LEDs
bool ledGreenActive = false;
bool ledYellowActive = false;
bool ledRedActive = false;
bool ledBlueActive = false;

void loop() {
  //if successful receive some data, then ...
  if (irrecv.decode(&results)) {
  ...
     //handle received data...
     switch (value) {
        case 12495: ledGreenActive = !ledGreenActive; break;
        case 6375: ledYellowActive = !ledYellowActive; break;
        case 31365: ledRedActive = !ledRedActive; break;
        case 4335: ledBlueActive = !ledBlueActive; break;
     }

     resetLEDs();
   ....
}

//set actual states to LEDS
void resetLEDs() {
  digitalWrite(ledGreen, ledGreenActive);
  digitalWrite(ledYellow, ledYellowActive);
  digitalWrite(ledRed, ledRedActive);
  digitalWrite(ledBlue, ledBlueActive);
}

Here you get the complete Code to download.

Prevent message “The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.”

If you run the above example, you will see the following message on the serial port.

The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.<fieldname> .

The code will work, but if you want to prevent this message, you have to rewrite this code a little bit.

//library to handle IR Receiver Diode
#include <IRremote.h>

//tiny, 3 mm Status LED at digital pin 13
#define statusLed 13
//IR Receiver Diode connected at digital pin 8
#define irReceiverDiode 8

//some connected LEDs
#define ledGreen 3
#define ledYellow 4
#define ledRed 5
#define ledBlue 6

//fields to store status of LEDs
bool ledGreenActive = false;
bool ledYellowActive = false;
bool ledRedActive = false;
bool ledBlueActive = false;

void setup() {
  //define pins from LEDs as OUTPUT
  pinMode(ledGreen, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(statusLed, OUTPUT);

  //define pin from IR Receiver Diode as INPUT
  pinMode(irReceiverDiode, INPUT);

  //begin communication
  IrReceiver.begin(irReceiverDiode, ENABLE_LED_FEEDBACK);
  //begin serial communication with 9600 baud
  Serial.begin(9600);
}

void loop() {
  //if successful receive some data, then ...
  if (IrReceiver.decode()) {
    //store data in field value
    int value = String(IrReceiver.decodedIRData.command, DEC).toInt();
    //consume the data and clear
    IrReceiver.resume();
    //print the value to the serial interface
    Serial.println(value);
    //activate status LED
    digitalWrite(statusLed, HIGH);
    //tiny brake to light up the LED
    delay(75);
    //deactive status LED
    digitalWrite(statusLed, LOW);

    switch (value) {
      case 12: ledGreenActive = !ledGreenActive; break;
      case 24: ledYellowActive = !ledYellowActive; break;
      case 94: ledRedActive = !ledRedActive; break;
      case 8: ledBlueActive = !ledBlueActive; break;
    }

    resetLEDs();
  }
}

//set actual states to LEDS
void resetLEDs() {
  digitalWrite(ledGreen, ledGreenActive);
  digitalWrite(ledYellow, ledYellowActive);
  digitalWrite(ledRed, ledRedActive);
  digitalWrite(ledBlue, ledBlueActive);
}

Leave a Reply

Your email address will not be published. Required fields are marked *