Infrared library for remote controls using the NEC protocol

BSD licensed infrared receiver library for ATMega and ATTiny / AVR-GCC

Since I couldn't find any code with a suitable license on the internet, I wrote my own function library. The implementation needs an interrupt and a 8-bit timer and works on an ATMega48, ATMega88, ATMega168, ATMega328 as well as ATTiny2313 and ATTiny4313.

Photo: Different suitable remote controls

In the interrupt a state machine is used to decode the signal. The signal is completely analyzed and checked for errors. It can also be read out if the user is holding the button down. The library is used in several kits and projects I developed.

Using the library

The corresponding timer and interrupt settings are initialized with the following call:

// Initialize IR lib

Now the last received packet is available in a struct ir_struct, which can be retrieved in the global variable ir. This contains address, command and a status flag. The bit IR_RECIEVED of the flag is set when a valid packet was received and must be reset in the main program after processing. As long as the key is held, the bit IR_KEYHOLD is set.

The processing could look like this:

// Check if new code is received
if(ir.status & (1<<IR_RECEIVED)) {
 // Remote address ok?
 if(ir.address==42) {
  switch(ir.command) {
   case 64:
    // Do something useful
    // [...]
 // Reset state
 ir.status &= ~(1<<IR_RECEIVED);

If no more reception is required, the interrupts can be stopped again by the following call:

// Stop IR lib
Further links