Basic Home Automation With an IR LED and Receiver
February 19, 2017
Controlling devices that have infrared remotes is an easy and low-cost form of home automation, so I decided to make a YouTube tutorial video about it. I'm using the "IRremote" library by Ken Shirriff, and wrote some firmware that uses an IR receiver and an IR LED as a computer-controlled universal remote. The end result was being able to press a key on my keyboard, and having household decives respond accordingly. I set it up so i can turn my TV and tower fan on or off.
After installing the IRremote library, the firmware is pretty simple:
#include <IRremote.h>
IRrecv receiver(2); // receiver is connected to pin2
IRsend sender;
decode_results results;
long repetitions;
long count;
unsigned int durations[100];
void (*reset)(void) = 0;
void setup() {
Serial.begin(9600);
receiver.enableIRIn(); // start receiving signals
}
void loop() {
// check for text from the PC
// the PC sends a string containing "r,n,a,b,c,d,e,..." where r is how many times to repeat the command,
// n is the number of durations, and a/b/c/d/e/... are the durations.
// the durations are how long each mark and space period of an infrared command should last, in microseconds.
if(Serial.available()) {
// parse the text
repetitions = Serial.parseInt();
count = Serial.parseInt();
for(int i = 0; i < count; i++)
durations[i] = Serial.parseInt();
// send the command using 40kHz PWM
for(int i = 0; i < repetitions; i++) {
sender.sendRaw(durations, count, 40);
delay(50);
}
// for a bit of fault tolerance, reset the arduino after receiving any command
reset();
}
// check if a decoded infrared signal is available
if(receiver.decode(&results)) {
Serial.println(results.value, HEX);
Serial.print(results.rawlen - 1);
for(int i = 1; i < results.rawlen; i++) {
unsigned int number = results.rawbuf[i] * USECPERTICK;
Serial.print(",");
Serial.print(number);
}
Serial.println("");
receiver.resume();
}
}
If you aim a remote at the IR receiver and press a button, details about that waveform will be printed to the serial port. To have the Arduino send that IR command, just send that text back to the Arduino, but add a number and comma to the beginning. That number specifies how many times to repeat the command. Some protocols, like Sony's, require a command be sent three times. So you would send "3," followed by the text the Arduino printed out.
We now have the hardware and firmware all setup, but it's a pain to send those IR commands (copy-and-pasting text into the Serial Monitor.) We can create some batch files to automate the sending of text to the Arduino. These will need to be adjusted for your IR commands and COM port, but for me it was:
fan_on_off.bat
mode COM3 baud=9600 parity=n data=8 stop=1
echo "1,23,1200,450,1250,400,400,1300,1200,450,1200,450,400,1250,400,1250,400,1300,350,1300,400,1250,400,1250,1250" > COM3
tv_on_off.bat
mode COM3 baud=9600 parity=n data=8 stop=1
echo "3,25,2400,600,600,600,1150,600,1200,600,1150,650,1150,600,1150,650,550,600,1200,600,600,600,550,660,600,600,600" > COM3
By default the Arduino will reset immediately after you connect to it's serial port, so that needs to be disabled. I show how in the video, you just need to cut a trace on the PCB. After that, double clicking on those batch files will send the IR commands.
I used AutoHotKey to have key presses trigger those batch files. A simple script tells AutoHotKey to turn my tv on/off if I press F12, and turn my fan on/off if I press F11:
f12::
Run, C:\Users\FarrellF\Desktop\tv_on_off.bat
Return
f11::
Run, C:\Users\FarrellF\Desktop\fan_on_off.bat
Return
See the video for more details.
This is the infrared LED and receiver that I used in the first part of the video: http://amzn.to/2lZ7pTy
For a higher-power LED: http://amzn.to/2lZ6oLk