ATtiny13 – IR receiver / NEC proto analyzer

In this example we will make a project that uses standard IR remote control from your TV, audio system or other using NEC signal format, to receive and recognize address/command with AVR ATtiny13. Thanks to Software UART we will print received NEC address/command via serial console. Project has been tested with ATtiny13a @9.6 MHz, VCC=5V and with serial configuration 8/N/1 format at 57600 baud (lowest error rates during tests). The code is on Github, click here.

nec-analyzer-serial-console

Theory

IR Communication

The IR Transmitter consists of the LED that emits the IR (InfraRed) radiation. Since the IR radiation is invisible to human eye it is perfect for using in wireless communication. A electronic remote device mainly consists of one IR transmitter or IR receiver. A IR Transmitter flashes an IR LED that emits invisible light which is turned into an instruction and is received by the IR Receiver module. The IR signal is modulated during transmission (Pulse Width Encoding). The most commonly used IR modulation is about 38khz (36 kHz, 37.9 kHz, 38 kHz and 40 kHz). The transmission distance can range from 8 m to 45 m, with the most common infrared receivers having a transmission distance of 45 m.

IR-Transmitter-signal

The IR Receiver consists of receiver module (photo-diode, photo-transistor). On receiver side the modulated signal is demodulated and the pattern is obtained as on diagram bellow.

IR-Receiver-signal

The parts  necessary to construct transmitter and receivers are typically inexpensive, but these systems are limited to line of sight operation.

NEC Protocol

Features

  • 8 bit address and 8 bit command length.
  • Extended mode available, doubling the address size.
  • Address and command are transmitted twice for reliability.
  • Pulse distance modulation.
  • Carrier frequency of 38kHz.
  • Bit time of 1.125ms or 2.25ms.
  • Logical ‘0’ – a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
  • Logical ‘1’ – a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25

Modulation

  • A 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
  • A 4.5ms space
  • The 8-bit address for the receiving device
  • The 8-bit logical inverse of the address
  • The 8-bit command
  • The 8-bit logical inverse of the command
  • Final 562.5µs pulse burst to show end of message transmission.

NECMessageFrame

Parts Required

Circuit Diagram

013_nec_proto_analyzer

Firmware

This code is written in C and can be compiled using the avr-gcc. Additional source files can be found here. More details on how compile this project is here.

/**
 * Copyright (c) 2016, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
 * ATtiny13/013
 * NEC proto analyzer. Example of monblocking IR signal 
 * reader (38kHz, TSOPxxx) and NEC protocol decoder.
 * Settings:
 *  FUSE_L=0x7A
 *  FUSE_H=0xFF
 *  F_CPU=9600000
 */

#include "ir.h"
#include "uart.h"


int
main(void)
{
    uint8_t addr, cmd;

    /* setup */
    IR_init();

    /* loop */
    while (1) {
        if (IR_read(&addr, &cmd) == IR_SUCCESS) {
            uart_puts("addr=");
            uart_putu(addr);
            uart_putc(',');
            uart_puts("cmd=");
            uart_putu(cmd);
            uart_putc('\n');
        }
    }
}

11 thoughts on “ATtiny13 – IR receiver / NEC proto analyzer

  1. ı used avr-gcc to compile by following your tutorial, but those codes not work on this example.
    the code used for avr; (these codes works perfectly on main.c and makefile)
    $ avr-gcc -Wall -g -Os -mmcu=attiny13 -o main.bin main.c
    $ avr-objcopy -j .text -j .data -O ihex main.bin main.hex
    $ avrdude -p attiny13 -c usbasp -U flash:w:main.hex:i -F -P usb

    but there are also ir.c, uart.h, uart.c, uart.h, how ı include these files avr-gcc process. I am very noob, pls show me directly. or could you add tutorial how use avr on c files which has libraries.

  2. Hi Lukasz i am working on attiny13 . I have square pulse input of frequency 3khz to 5 khz. If i have my input in this range of frequency i need to get my result otherwise not. Do you have any recommendation and code

    • Hi, it’s quite possible to make it on Attin13. I would use external interrupt for an input to count pulses and Timer to calculate frequency (F=pulse_counter/period). In a main loop you can verify frequency is in range.
      /L

  3. Awesome project! Would it be possible using your project with OCR0B instead of OCR0A? This would allow me to use OCR0A for a PWM control. Or am I wrong in my understanding of the ATtiny13?

Leave a Comment