Simple Stick User Guide - Working with TSOP IR Receiver
The
TSOP SM0038 is an IR receiver on the Simple Stick. The TSOP will help you
to interface your TV remote with the Simple Stick and in the Process learn
the basics of Wireless Communication. The TSOP is connected to pin
digital 8.
The
TSOP outputs a constant HIGH signal when idle and as it receives data,
it tends to invert the data. i.e when an IR LED is transmitting data
onto the TSOP, everytime the IR led goes high, the TSOP will go LOW and
vice versa. Remote control signals are often bytes of data that is
encoded and transmitted by pulsing(switching ON & OFF the IR LED at a
specific frequency) Most TV remote controls work at 32-40 Khz frequency
and most receivers can receive this range.
Heres
a link to a nice write up on different remote control protocols. lets
first take a look how the Sony Remote Control Protocol Works. We stick
to Sony as it is the easiest one to get started with. Read this before proceeding
Here's
a basic outline of how the data is sent. Every time you press a button
on a Sony remote control, it sends out a 13Bit data. The first bit is a
start bit indicating there are 12 bits of data following it. The next 7
bits are the command bit which will vary depending upon the keys being
pressed. The last 5 bits are the address bits which will the same for
all buttons but vary for remote controls of different devices.
The
black bars in the following image correspond to high signals (called
marks) and the white spaces in between correspond to low signals (called
spaces). The duration of the 'marks' varies according to the bit being
transmitted. It is 2.4ms for the start bit, 1.2ms for HIGH bit and 0.6ms
for LOW bit. The duration of the 'spaces' is a constant 0.6ms. Every
mark is followed by a space. Any data can be converted to binary format
and transmitted in this manner. In fact this is the basic form of all
types of serial communication.
Technique
to decode this signal train, would be to constantly monitor the TSOP
pin[Digital 8] for its normal state and the moment it produces a low
signal, measure the duration of the low signal. If the measured duration
of the low signal is around 2ms then measure and store the duration for
the next 12 bits of the incoming data. After storing the data, evaluate
the duration and based on the duration convert the data to decimal /
hexadecimal and use it in your application.
There
is an interesting IR remote library that can help you read different
remotes without any difficulty. It can also generate different remote
signals. It can be used to generate these remote control signals on the IR LED connected to the 9th
pin of the Simple Stick (PWM pin).
How to use Libraries in Arduino - An Overview
To use any library you download, unzip the downloaded file and copy its
contents to the libraries folder inside your arduino directory. You can
check the library by opening the arduino ide and going to Sketch ->
Import Library Option, if your library is in the proper location, it
will show up here. Next if there is an example provided with the library
(it will be inside a folder called example inside the base folder of
the library) it will show up under the libraries name in the
File->Examples Menu. You should reopen Arduino for the library to
show up.
Once you install the IRremote, You can try the example program,
IRrecvDemo. This program will give you a serial output of the HEX code
for each value corresponding to each button on a remote. We will be
using the decimal value in our next program. To get the decimal value,
just do the following modification.
replace this line
int RECV_PIN = 11;
with
int RECV_PIN = 8; and this line
Serial.println(results.value, HEX);
with
Serial.println(results.value);
Here's a video of a simple project - A remote control interface for our Binary Counter.
Here's the source code for the same
Remote_Binary_Counter.ino
/*
This sketch increases a 3 bit number every time '+' button is pressed and decreases the value when '-' button is pressed on the remote.It shows the output on 3 LEDs in Binary Format
*/
#include <IRremote.h>
int RECV_PIN = 15;
IRrecv irrecv(RECV_PIN);
decode_results results;
int i = 0;
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
irrecv.enableIRIn(); // Start the Remote receiver
Serial.begin(9600);
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value);
switch(results.value) // if the '+' button is pressed
{
case 2320:
i=0;
break;// 2320 is the value for '0'
case 16:
i=1;
break;// 16 is the value for '1'
case 2064:
i=2;
break;// 2064 is the value for '2'
case 1040:
i=3;
break;// 1040 is the value for '3'
case 3088:
i=4;
break;// 3088 is the value for '4'
case 528:
i=5;
break;// 528 is the value for '5'
case 2576:
i=6;
break;// 2576 is the value for '6'
case 1552:
i=7;
break;// 1552 is the value for '7'
case 1168: // this is the value for the increment button
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0;
break;
case 3216: // this is the value for the decrement button
if(i>0) // if counter value is greater than 0 or 3 bits
i--; // decrement counter value
else
i=7; // reset counter to 7
break;
}
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,c); // write MSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,a); // write LSB
irrecv.resume(); // Receive the next value
}
}