Arduino IR Sender and Receiver
Arduino IR Sender and Receiver
View Blog
On this tutorial, we will show how to implement an IR sender and receiver with Arduino. To do
this, you will need 2x Arduinos, 1x IR emitter, 1x IR receiver, 1x 200 ohm resistor, 1x push
button, and 1x LED. You can use your TV remote as an IR transmitter.
First, you have to check the receiver pinout. Look at the datasheet, you should find something
like that:
The sample program was extracted from the Arduino Cookbook book. Open your Arduino IDE
and upload the following code:
/*
irSend sketch
this code needs an IR LED connected to pin 3
and 5 switches connected to pins 4 - 8
*/
#include <IRremote.h>
// IR remote control library
const int numberOfKeys = 1;
const int firstKey = 4;
// the first pin of the 5 sequential pins connected to buttons
boolean buttonState[numberOfKeys];
boolean lastButtonState[numberOfKeys];
long irKeyCodes[numberOfKeys] = {
0x18E758A7, //0 key
};
IRsend irsend;
void setup()
{
for (int i = 0; i < numberOfKeys; i++){
buttonState[i]=true;
lastButtonState[i]=true;
int physicalPin=i + firstKey;
pinMode(physicalPin, INPUT);
digitalWrite(physicalPin, HIGH); // turn on pull-ups
}
Serial.begin(9600);
}
void loop() {
for (int keyNumber=0; keyNumber<numberOfKeys; keyNumber++)
{
int physicalPinToRead=keyNumber+4;
buttonState[keyNumber] = digitalRead(physicalPinToRead);
if (buttonState[keyNumber] != lastButtonState[keyNumber])
{
if (buttonState[keyNumber] == LOW)
{
irsend.sendSony(irKeyCodes[keyNumber], 32);
Serial.println("Sending");
}
lastButtonState[keyNumber] = buttonState[keyNumber];
}
}
}
/*
IR_remote_detector sketch
An IR remote receiver is connected to pin 2.
The LED on pin 13 toggles each time a button on the remote is
pressed.
*/
#include <IRremote.h> //adds the library code to the sketch
const int irReceiverPin = 2; //pin the receiver is connected to
const int ledPin = 13;
IRrecv irrecv(irReceiverPin); //create an IRrecv object
decode_results decodedSignal; //stores results from IR detector
void setup()
{
pinMode(ledPin, OUTPUT);
irrecv.enableIRIn();
}
boolean lightState = false;
unsigned long last = millis();
It's time to test! When you press the button, the LED on the other Arduino should switch on.
When you press it again, the LED switch off.