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.
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.
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.
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.
Parts Required
- ATtiny13 – i.e. MBAVR-1 (Minimalist Development Board)
- USB-UART Converter
- TSOP31238 – IR receiver (38kHz)
Circuit Diagram
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'); } } }
ı 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.
Nice post. But not working with VS1838B module. Is there any fix?
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
carpe diem
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?
Thanks! yep, You need to change ADC channel and it should also work with OCR0B.
Please tell me, which remote you use for control this circuit.
It was tested with some old TV remote and remote from Yamaha amplifier. If you need to analyse what codes has been produced by your remote, then I suggest to read this – ATtiny13 – IR receiver / NEC proto analyzer
I love your blog man. I was looking for this example for about 2-3 months.
Thanks. I’m glad you found it useful.