Project 10.01 Sending IR

In this project, you'll discover how to transmit infrared (IR) data using the MC Trainer and the IRremote library!

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);  
  }
}
//////////////////////////////////////
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.