public
Last active

A simple Arduino driver for the NEC (Japanese) Infrared IR protocol. Drive an IR LED direct with your own code. This is a direct pin bit-bang approach, so beware about interrupts and timing difference between hardware. Feel free to use hardware PWM to generate the carrier frequency if you want better accuracy.

  • Download Gist
Arduino-IR-TX-NEC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
//*****************************************
// NEC (Japanese) Infrared code sending library for the Arduino
// Send a standard NEC 4 byte protocol direct to an IR LED on the define pin
// Assumes an IR LED connected on I/O pin to ground, or equivalent driver.
// Tested on a Freetronics Eleven Uno compatible
// Written by David L. Jones www.eevblog.com
// Youtube video explaining this code: http://www.youtube.com/watch?v=BUvFGTxZBG8
// License: Creative Commons CC BY
//*****************************************
 
#define IRLEDpin 2 //the arduino pin connected to IR LED to ground. HIGH=LED ON
#define BITtime 562 //length of the carrier bit in microseconds
//put your own code here - 4 bytes (ADDR1 | ADDR2 | COMMAND1 | COMMAND2)
unsigned long IRcode=0b11000001110001111100000000111111;
 
// SOME CODES:
// Canon WL-D89 video remote START/STOP button = 0b11000001110001111100000000111111
 
void setup()
{
}
 
void IRsetup(void)
{
pinMode(IRLEDpin, OUTPUT);
digitalWrite(IRLEDpin, LOW); //turn off IR LED to start
}
 
// Ouput the 38KHz carrier frequency for the required time in microseconds
// This is timing critial and just do-able on an Arduino using the standard I/O functions.
// If you are using interrupts, ensure they disabled for the duration.
void IRcarrier(unsigned int IRtimemicroseconds)
{
for(int i=0; i < (IRtimemicroseconds / 26); i++)
{
digitalWrite(IRLEDpin, HIGH); //turn on the IR LED
//NOTE: digitalWrite takes about 3.5us to execute, so we need to factor that into the timing.
delayMicroseconds(9); //delay for 13us (9us + digitalWrite), half the carrier frequnecy
digitalWrite(IRLEDpin, LOW); //turn off the IR LED
delayMicroseconds(9); //delay for 13us (9us + digitalWrite), half the carrier frequnecy
}
}
 
//Sends the IR code in 4 byte NEC format
void IRsendCode(unsigned long code)
{
//send the leading pulse
IRcarrier(9000); //9ms of carrier
delayMicroseconds(4500); //4.5ms of silence
//send the user defined 4 byte/32bit code
for (int i=0; i<32; i++) //send all 4 bytes or 32 bits
{
IRcarrier(BITtime); //turn on the carrier for one bit time
if (code & 0x80000000) //get the current bit by masking all but the MSB
delayMicroseconds(3 * BITtime); //a HIGH is 3 bit time periods
else
delayMicroseconds(BITtime); //a LOW is only 1 bit time period
code<<=1; //shift to the next bit for this byte
}
IRcarrier(BITtime); //send a single STOP bit.
}
 
void loop() //some demo main code
{
IRsetup(); //Only need to call this once to setup
IRsendCode(IRcode);
delay(5000);
IRsendCode(IRcode);
while(1);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.