Sunday, May 26, 2013

IR remote emulator with attiny45

The goal

Build IR remote emulator, so we can control our stereo from network.

Usability

High. Can be part of intelligent house project.

Parts

  • attiny45/85
  • IR led
  • BC547 transistor
  • 10ohm resistor
  • 4.7k resistor
  • crystal resonator 8Mhz
  • universal pcb board or any other
  • pin connectors male and female

Code


Hardware


Media

 IR emulator connected to Raspberry PI

Description

Description of IR remote decoding is in IR remote decoding article.

IR diode is connected through transistor to one of Attiny's pins. MCU turns On and Off diode with frequency about 38kHz in constant amount of time. Output from diode look something like 38kHz-Off-38kHz-38kHz-38kHz-Off-Off-38kHz.

Frequency of 38kHz is generated by internal TimerCounter with interrupt. So to start/stop sending modulated signal simplifies to Enable/Disable TimerCounter interrupt, this guaranties stable frequency and trivial program code.

Above interrupt is in Enabled/Disabled state in fixed sequence in constant amount of time. Different IR messages has different sequences, total duration of message is the same for every sequence.
void sendOneByte(const unsigned char *byte) {
   unsigned char e = 7;

   do {
      if ( (*byte & (1<<e)) == (1<<e) ) {
         H(TIMSK, TOIE0);
      }
      else {
         L(TIMSK, TOIE0);
         L(PORTB, PB1);
      }
//       delay_us(410);
      delay_us(300);
   } while(e--);
}
IR messages sequences are long, but can be represented as 1 or 0 state. This leads us to pack 8 states in one byte, so it's more economic in sense of available memory.
const unsigned char mute[]       = { 0b10101000,  0b10001000,  0b10101000,  0b10001000,  0b10101010,  0b00100010,  0b10000000 };
const unsigned char volUp[]      = { 0b10100010,  0b00100010,  0b00101010,  0b10001010,  0b10101000,  0b10001000,  0b10000000 };
const unsigned char volDown[]    = { 0b10001000,  0b10001000,  0b10001010,  0b10101010,  0b10101000,  0b10001000,  0b10000000 };

Communicating with IR emulator

Main goal was build IR emulator and to connect it to Raspberry Pi, so we can control our stereo from Android phone like in this project. The next assumption was the size of emulator and number of connection pins.

Attiny45 has a USI (I2C compatible) interface, so I've use it to make it more universal. Alternative was to use RS232, but to I2C you can connect more than one device, so its more handy to use I2C.

Emulator has only 4 pins: GND/SDA/SCL/VCC and works on 3.3V or 5V, so it's small and can be connected to Raspberry Pi (3.3V logic) or MCU like Atmega32 (5V logic).


Device is small and have only 4 connection pins

I2C library

USI/I2C communication is described in Atmel's document, you can find it here. I use Atmel's code to communicate with Raspberry Pi, this code is in two files: USI_TWI_Slave.c and USI_TWI_Slave.h. I've made some changes, they are marked with // HC- comment.


With IR diode connected

Communication example

Picture below is showing IR emulator connected to Raspberry Pi. all 4 pins are connected, and it's powered from 3.3V GPIO pin.

 Connected to Raspberry Pi by I2C

Emulator can be controlled from command line, by using i2c-tools package. Device's address is 0x10, it can be changed directly in C code.
pi@raspberrypi ~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
pi@raspberrypi ~ $ 
And now send command (for e.g.) 2 to device. Command 2 is Volume Down.
pi@raspberrypi ~ $ i2cset  -y 1 0x10 2

Electric diagram

IR emulator

On above there is Attiny13, so look only on pin numbers, not on their descriptions. I didn't have Attiny25/45/85 in my library in Eagle.

No comments:

Post a Comment