You are on page 1of 5

IRLib Tutorial part 1: Hardware set up

This is the first in series of articles on using my infrared remote library for Arduino. In this installment were going to show you how to set up the hardware and how to run a quick demo sketch. You can find out more about the library on my IRLib page. In order to detect a single from IR remote control such as you might use for your TV or home entertainment system you need an IR receiver module. Typically I use a unit I get from Radio Shack. You can also buy a similar module from my favorite supplier Adafruit.com. See the links at the end of this post for places where you can buy all of the parts mentioned in this article. Pin 1 (on the left as you looking at the lens) needs to be connected to a input pin of the Arduino. It can be any could be any pin that doesnt conflict with anything else youre doing with the device. All of the examples in the library assume you are connected to pin 11 so we suggest you use it. The center pin 2 connects to ground any right-hand pin 3 should connect to your +5V power supply. If you are using a different microcontroller that runs at +3V this device will work at that voltage. The simplest output device is simply an infrared LED and a current limiting resistor. IR LEDs can be purchased from a variety of places. Again see the links at the bottom of this post for sources. You are limited to using PWM pins for output because we use the PWM feature to modulate the signal. The default pin to use on Arduino Uno or other ATmega328-based controllers is pin 3. This particular pin is connected to timer 2 of the chip. However the Arduino Leonardo and other controllers based on the ATmega32u4 does not have a timer 2. On these devices the default is to use timer 1 and pin 9 for output. We will talk more later about other options for timers and how to use this library on other types of hardware. For now all you need to know is if you have an Uno you should use pin 3 or a Leonardo use pin 11. Connect a 100 ohm resistor in series with the LED. Make sure you get the polarity of the LED correct. The longer of the two leads should connect to ground. The shorter lead connects to the resistor which in turn connects to the Arduino. Here is a schematic for the simplest setup.

Simple IR I/O Schematic

That was the absolute simplest schematic however the output from the IR LED will be pretty weak because the output pins of Arduino cannot supply much current. You might want to consider adding an NPN transistor to drive the LED.

NPN Transistor Driving IR LED

Here is a schematic using a PN2222 NPN transistor and a 470 ohm base resistor with the LED. WARNING: This circuit will drive the LED beyond its continuous current limits of 100 mA. However because we are only pulsing the LED for a brief period using a modulated signal we can get away with this. So you should only use the circuit if you are certain that the output pin is not going to be left on continuously for more than a fraction of a second. Here is a simple sketch you can use to test if you are receiving IR signals. Is a

stripped down version of the example sketch IRrecvDump that is in the examples subdirectory of the library.

0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0

/ * R e c e i v i n g I R s i g n a l a n d d u m p t h e d e t a i l s * / # i n c l u d e < I R L i b . h > / / c r e a t e a r e c e i v e r o b j e c t I R r e c v M y _ R e c e i v e r ( 1 1 ) / / U s e i n p u t p i n 1 1 / / c r e a t e a d e c o d e r o b j e c t I R d e c o d e M y _ D e c o d e r v o i d s e t u p ( ) { S e r i a l . b e g i n ( 9 6 0 0 ) / / W e w i l l r e a d t h e o u t p u t o n t h e s e r i a l m o n i t o r M y _ R e c e i v e r . e n a b l e I R I n ( ) / / S t a r t t h e r e c e i v e r } v o i d l o o p ( ) { / / L o o p u n t i l w e g e t a s i g n a l a n d p a s s i t t o t h e d e c o d e r i f ( M y _ R e c e i v e r . G e t R e s u l t s ( & M y _ D e c o d e r ) ) { M y _ D e c o d e r . d e c o d e ( ) / / d e c o d e t h e s i g n a l M y _ D e c o d e r . D u m p R e s u l t s ( ) / / d u m p t h e r e s u l t s o n t h e s e r i a l m o n i t o r M y _ R e c e i v e r . r e s u m e ( ) / / r e s t a r t t h e r e c e i v e r } }

Upload the sketch and start the serial monitor on the Arduino IDE. Point a remote control such as a TV remote at the receiver and press a button. It will dump the information about the signal received. If its a protocol with the library understands it will tell you which protocol and it will give you a hex number of up to 32 bits that is the code for that particular function. To test your transmitter, first determine the 32 bit hex code and protocol that the library can decode. A good example would be the power off/on button of your device assuming that your protocol is supported. As an example I sent a power signal to my Sony DVD player. The dump program tells me that it is a 20 bit code that is Sony protocol and that the value is 0xa8bca. The following sketch is a version of the example IRsendDemo in the examples directory of the library.

0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6

/ * T r a n s m i t a p o w e r c o d e f o r S o n y D V D * / # i n c l u d e < I R L i b . h > I R s e n d M y _ S e n d e r v o i d s e t u p ( ) { S e r i a l . b e g i n ( 9 6 0 0 ) } v o i d l o o p ( ) { i f ( S e r i a l . r e a d ( ) ! = 1 ) { / / s e n d a c o d e e v e r y t i m e a c h a r a c t e r i s / / r e c e i v e d f r o m t h e s e r i a l p o r t / / S o n y D V D p o w e r A 8 B C A M y _ S e n d e r . s e n d ( S O N Y , 0 x a 8 b c a , 2 0 ) } }

You will need to substitute the protocol name, the hexadecimal code, and the number of bits for whatever device you are operating. After you upload the sketch type a character into the serial monitor and press enter. Every time you transmit a character from the serial monitor, it will transmit the code you have programmed into the My_Sender.send() Function call. In the installments which follow we will explain more of the details of the library including a more advanced circuit for transmitting codes. However for now this should get you up and running. As promised here are some links to the hardware mentioned in this post Arduino Uno available at Adafruit.com or RadioShack.com Arduino Leonardo available at Adafruit.com 38kHz IR receiver module available at Adafruit.com or RadioShack.com IR LED available at Adafruit.com (single unit) or Adafruit.com (25 pack) or RadioShack.com 100 ohm resistor at RadioShack.com (5 pack) 470 ohm resistor at RadioShack.com (5 pack) NPN transistor at Adafruit.com (10 pack) or RadioShack.com In the next installment will begin to cover some of the details of the library methods.

You might also like