Project 10.01 Sending IR

This project aims to replicate the functionality of an IR remote control. We'll be using a push-button switch to trigger the sending of specific IR signals, which can be read by an IR receiver. This can be used to do things like turn on a TV!

Project Code:

//////////////////////////////////////
// 10.01 Sending Data

#include <IRremote.h>

byte sCommand = 0x9; 
byte ADDR = 0x0;  
 
byte SW1 = 1;
bool pressed = 0;

byte IRPin = 5;

void setup() {
  Serial.begin(9600); 
  IrSender.begin(IRPin); 
  pinMode(SW1, INPUT);
}

void loop() {
  if (digitalRead(SW1) == pressed) {
    Serial.println();
    Serial.print("Send now: address=0x");
    Serial.print(ADDR, HEX);
    Serial.print(", command=0x");
    Serial.println(sCommand, HEX);

    IrSender.sendNEC(ADDR, sCommand, 0); 

    delay(250);  
  }
}
//////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

In the setup() function, the IR emitter is started on pin 5 using IrSender.begin(IRPin).

IrSender.begin(IRPin);

In the loop() function, SW1 is monitored to see if it has been pressed.

if (digitalRead(SW1) == pressed) { 

If the button has been pressed, information about what is being sent is printed using the Serial methods. You’ll notice that some of the Serial.print methods have two parameters. You can use keywords such as DEC, HEX, BIN, OCT to print numbers in a specific number base. Data used with the IR protocol is generally in hexadecimal, so we print it in that fashion with the HEX keyword.

Serial.println();
Serial.print("Send now: address=0x");
Serial.print(ADDR, HEX);
Serial.print(", command=0x");
Serial.println(sCommand, HEX);

Next, the data is actually sent with the IrSender.sendNEC method. There are three parameters to this method. The first parameter is the address that the data should be sent to, the second parameter is the data to be sent, and the last parameter is the number of times the data should be repeated. Sometimes it is useful to repeat the signal a few times to ensure it makes it to the intended device.

IrSender.sendNEC(ADDR, sCommand, 0); 

What if the data is not the NEC protocol? This library supports a wide range of protocols. They are listed here:

Protocol Method
NEC IrSender.sendNEC()
Sony IrSender.sendSony()
RC5 IrSender.sendRC5()
RC6 IrSender.sendRC6()
Dish IrSender.sendDISH()
JVC IrSender.sendJVC()
Samsung IrSender.sendSAMSUNG()
LG IrSender.sendLG()
Whynter IrSender.sendWhynter()
COOLIX IrSender.sendCOOLIX()
Denon IrSender.sendDenon()
Sharp IrSender.sendSharpRaw()
Panasonic IrSender.sendPanasonic()
Sanyo IrSender.sendSanyo()
Mitsubishi IrSender.sendMitsubishi()
Apple IrSender.sendApple()
Pronto IrSender.sendPronto()
LEGO Power Functions IrSender.sendLegoPowerFunctions()
Bose Wave IrSender.sendBoseWave()
Metz IrSender.sendMetz()
MagiQuest IrSender.sendMagiQuest()
RCMM IrSender.sendRCMM()

Some of these methods may take different parameters so consult the library reference, or simply Google it before using.

The loop is then ended with a delay if the button was pressed as a crude debounce.

    delay(250);  
  }
}
//////////////////////////////////////
Previous
Previous

Project 11.00 Using the Board as a Mouse

Next
Next

Project 10.00 Decoding IR