InfraRed (IR) Communication - How It Works
These days everything is wireless – radios, telephones, computers, and even your printer!
Did you know that the concept is actually over one hundred years old? Back in 1901, discoverer Nikola Tesla began construction of the Wardenclyffe Tower in Long Island, New York. It was intended to send and receive trans-Atlantic wireless telephony and even transmit electrical power wirelessly; however, due to financial problems, it was never completed. Today, wireless communication is everywhere, and wireless power transmission is currently being developed. For the electronics enthusiast, there are basically two forms of wireless communication: radio and infrared signals. Technologies such as Wi-Fi and Bluetooth are forms of radio communication. Your TV remote control works by sending infrared signals to your television, which receives those signals and processes them to change the channel, volume, etc. The downside of IR (infrared) is that it requires line-of-sight for transmission / reception. For most of your electronics and robotics projects, it is a cheap and easy way to free yourself of those pesky wires!
My Infrared Transmitter / Receiver Project
Sony InfraRed Communication (SIRC) Protocol
Some months back, I was looking for information on IR communication when I stumbled across Nigel Goodwin's PIC tutorial website. This project uses SIRC, a communication protocol developed by Sony for control of their TVs, DVD players, etc; therefore, you can use a Sony remote control (transmitter) to send signals to your receiver circuit if you so choose.
SIRC uses a pulse width system with a start bit of 2.4 ms duration, followed by 12 data bits, where a '0' is 0.6 ms wide, and a '1' is 1.2 ms wide. The bits are separated by 0.6 ms gaps. The data consists of a 7-bit command code followed by a 5-bit device code, where a command is Channel Up / Down or Volume Up / Down; and a device is a TV, DVD player, etc. By transmitting different device codes, the same remote control can be used to control different devices – depending upon to which device code a particular device's receiver circuit is programmed to respond.
The table below (Table 1) shows the data format. After the start bit, the command code is sent, least significant bit first (D0). Then the device code is sent, again least significant bit first (C0). The entire series is sent repeatedly while the button is pressed, every 45 ms. In order to decode the transmission, we need to measure the width of the incoming pulses. First, we look for the long 'start' pulse and then measure the next 12 pulses to determine if they are 0's or 1's. After measuring a pulse, it is tested to see if it is valid. This routine returns four possible results: start, zero, one, or error. The routine loops until a valid start pulse is detected and then reads the next 12 pulses. If any of those twelve pulses is anything other than a zero or a one, the program goes back and waits for another start pulse. For this project, we will use the command codes for Channel Up, Channel Down, Volume Up, and Volume Down. In decimal numbers, these are 16, 17, 18, and 19, respectively; however, in binary (as the program uses), they are 0000 100, 1000 100, 0100 100, and 1100 100, respectively. Note that the preceding binary numbers are backwards with the LSB first (opposite of conventional notation), shown as the receiving circuit will see them, D0 – D6.
The table to the right (Table 2) shows the device identifications. The five bits of the device code (C0 – C4) allow the possibility of up to 32 different device ID's (2^5 = 32).
For this project, we will be using two electronic circuits, one as a transmitter and one as a receiver. Each circuit uses a Microchip PIC16F684 microprocessor, which can be bought directly from Microchip or from eBay for less than a dollar per chip! The component count is low; however, there are a few uncommon parts that you might have to order. Again, I prefer to purchase small quantities of parts from eBay, as they are inexpensive and usually have free shipping. For those of you new to PIC chips, the ICP01 USB-to-TTL programmer can be purchased from www.piccircuit.com for less than $20. (Figure 1)
Infrared Receiver Circuit
Simply Assemble the InfraRed receiver circuit on a solderless breadboard as shown in the IR-RX link below. Be sure to connect all of your grounds, or the circuit will not work! I have included connections for ISCP (In-Circuit Serial Programming) which connects to your PIC programmer via a 5-pin header. (If you order the iCP02, which comes with a sixth pin for +3.3VDC, ignore the last pin. The programmer's ribbon cable has a red stripe down one side to indicate the MCLR connection.) In this example circuit, Pin 2 on the PIC chip is used as the output pin. Connect Pin 2 to the device you want to control with your IR remote control. There are different PIC Assembly code programs for the receiver circuit ready for download at the links below. DIGITAL_IR_RX.asm can be used to send a pulse-width modulated output signal to control servos or speakers (musical tones). The code can be easily modified to send output signals to pins 5 - 10 (PORTC). After you modify your code (if you choose to do so), you will need to compile the .asm files into .hex files for download to your PIC chip.
Infrared Transmitter Circuit
Next, assemble the infrared transmitter circuit on another solderless breadboard as shown in the IR-TX link below. Again, be sure to connect all of your grounds! Connect your PIC programmer and download the IR_TX.hex file to your PIC chip via the ICSP pins. This circuit uses four momentary push button inputs with 10kΩ pull-up resistors for your Channel Up, Channel Down, Volume Up, and Volume Down commands. The function of these four buttons can be changed in the infrared receiver code.
It is worth noting that 4.7Ω resistors are uncommon and probably have to be ordered online. Make sure that your 4.7Ω resistor has a 3 watt (or greater) power rating! Here's why:
V/R = I (5Vsupply - 1.7VIR_LED) / 4.7Ω = 702 mA
I2R = P (0.702A) * (0.702A) * 4.7Ω = 2.3 Watts
3 Watts > 2.3 Watts
You might be wondering why a BC337 NPN transistor is necessary, rather than the more common 2N3904 NPN transistor. The 3904 is only rated for 200 mA collector current, where the BC337 is rated for 800 mA collector current (perfect for our 702 mA). The more current, the more power; the more power, the greater the transmission range of our IR transmitter circuit.
As a side note, if you do not have an IR LED, I found that a green (visible light) LED works just as well.
If you are already familiar with PIC programming, you may skip the following Software section.
In order to modify the PIC Assembly (.asm) code linked below, you will need a plain text editor such as Microsoft Notepad. Be sure to save your files with the .asm file extension, NOT .txt!
There are many PIC Assembly language compilers out there for free on the internet. Microchip offers free download of their MPLAB IDE (integrated development environment) at www.microchip.com. I use a DOS-based compiler called GPASM (general purpose assembler) available for free download at http://sourceforge.net/projects/gputils/files/. First, create a new Microchip folder inside of your Program Files folder. Then download the GPUTILS package to this file directory:
When you download the .asm files linked below, save them to this directory:
Next, press the START button at the bottom left corner of your computer screen, click the RUN button, and type CMD to bring up a window with the DOS prompt. Then type cd\ to get to your root directory C:\. Type this:
Now type gpasm _______.asm and press ENTER. For example, it should look like this:
C:\Program Files\Microchip\gputils\bin>gpasm Digital_IR_RX.asm
If you press ENTER and another command prompt pops up on the next line (and nothing else), then you have successfully compiled your PIC Assembly (.asm) language program and converted it to a .hex file. Otherwise, your code has errors. Now close the window with the DOS prompt.
Open your C:\Program Files\Microchip\gputils\bin folder, and you should now see a .hex file with the same filename as the .asm file. For example, Digital_IR_RX.hex.
You are now ready to download your .hex file to your PIC chip. For this, you will need to download the free PICkit2 v2.61 software from Microchip at the following web address:
Warning: disconnect any external power sources (i.e. 9 volt battery) from your solderless breadboard before going any further! If you connect your USB programmer to your circuit while a 9V battery is also connected, you could backfeed and fry your programmer and/or your USB port.
Go ahead and connect your USB-to-TTL PIC programmer to your computer's USB port and to the ICSP header on your solderless breadboard. After installing the downloaded software to your desktop, open it by double-clicking on the icon. You should see a small window on the PICkit2 software that says, "PICkit 2 Connected. ID = iCP01-V1.0". If it says, "PICkit 2 not found.", then make sure that your programmer is connected to both your computer and circuit. Then click Tools at the top of the PICkit2 window, which will drop down a menu where you will click on Check Communication. That should fix your connection problem. Now click File > Import Hex and select your .hex file from your C:\Program Files\Microchip\gputils\bin folder and click Okay. Finally, click Write on the main window of the PICkit2 software, which will download your .hex file to your PIC chip. The program will begin running immediately and automatically in your PIC chip, as your circuit is powered by the USB ports's +5VDC supply voltage. Give your USB programmer about 5 to 10 seconds to complete the download of the .hex file before disconnecting your programmer from the ICSP port on your circuit, though it generally only takes two or three seconds. Now that your USB programmer is disconnected, you may connect the 9V battery to your circuit. Follow this process for both the transmitter and receiver circuits, and you'll be ready to transmit and receive infrared signal!
Congratulations, you're wireless!