Channels ▼

Al Williams

Dr. Dobb's Bloggers

Standing on Shoulders

August 22, 2012

I was lucky enough to know many good mentors when I was a kid all the way through college. That's surprising, too, since I grew up in a small town. Of course, it didn't hurt that I was an amateur radio operator and that hobby attracts people who were interested in the same things I wanted to do.

Some of my mentors are gone now. Some are still around and I occasionally hear from them. There are even a few who inspired me in passing that I never really knew who they were. In any case, I can't possibly pay them back for all the knowledge and inspiration they gave me.

I think that's one reason I have a long history of working with school kids, mostly in the areas of robotics, electronics, and programming. I gained so much from people who were generous with their time and knowledge that I feel like it means something to pay it forward by working with students.

This year I have a high school senior in what's known as an independent study mentorship. I've done a few of these. The student works on a project that we mutually select and then gives a presentation to a group of professionals about the work.

This student is interested in Java programming, but since he wants to go into a computer engineering program, I wanted to give him a taste of hardware as well. The project we selected was to read an infrared remote control and use it to drive a state machine that could do different actions. For example, pressing a button might set a digital output or play an audio file on the PC.

The bulk of the work, of course, is supposed to be the Java-based state machine. I have written code to read IR remotes several times, and I figured I might just dust one of those projects off and have him help me modify it. Then I realized I should do a web search and see what was out there that might be simple enough for him to tackle as a secondary project.

I found a library that decodes (and encodes) remotes that run on the Arduino. I have a reputation as an Arduino hater, but that's undeserved. In fact, I've been a big fan of the AVR chips in general, and bootloaders long before there was an Arduino. In the case of a student, the Arduino is nearly perfect. You don't need a dedicated programmer, the IDE is friendly, and there is plenty of example code and — what I wanted in this case — libraries. Besides, if I was such an Arduino hater, would I have all the parts to make one in my garage? Well, granted, it wasn't specifically an Arduino, but a quick setup of the AVR chip with a programmer was all it took.

If you haven’t incorporated an IR remote into a project before, it might seem harder than it has to be. The trick is to use a receiver IC (like a Vishay TSOP4038). The remotes generate bursts of modulated IR (usually at around 38kHz). Different remotes use different schemes to encode binary data, and the modulation prevents false triggering. These little three-terminal devices have a window that lets the IR in and it demodulates the incoming infrared signal into a clean digital signal just right for reading with a microcontroller.

Given a clean input signal, you simply have to time the pulses that come in and determine the bit pattern the pulses represent. For example, a very long pulse might represent a start bit, followed by a certain number of short or long pulses that represent 1s and 0s.

Fortunately with the library, you don't have to worry about that. You simply follow the install instructions with the library. Then you can import it into your project (I refuse to call it a sketch, I'm sorry). However, when I did this, the IDE inserted a header file out of order and the program wouldn't compile. So I'd suggest you just wipe out the boilerplate code the puts in and replace it with this:

#include <IRremote.h>


int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(19200);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value!=REPEAT) {
       Serial.println(results.value, HEX);
       }
    irrecv.resume(); // Receive the next value
  }
}

This is very close to one of the examples that ships with the library. The differences include the baud rate and skipping the REPEAT value. Many remotes send an initial code and then a short code to indicate the key is still being pressed. For the purpose of this project, I didn't care to see the repeat codes so the program filters it out.

The library not only reads the IR sensor but it tries to figure out the protocol (and tells you what it decided in the results structure). If all else fails, it returns a "raw" output that just tells you the on/off times that it read.

If you haven't seen Arduino code before, the setup function executes once and the loop function executes repeatedly. Otherwise, it is just standard C code compiled by the GCC compiler.

The code worked reliably with several remotes, although I did have an old cheap one that occasionally gave unpredictable responses. Truthfully, I don't know what protocol it was speaking so I can't say if it was the remote or a bug in the library. However, universal remotes are cheap now so you should be able to find a code that reliably works.

The verdict? It all worked and the student is now ready to go write the Java side of the code. Although he's programming in Java, he now knows how to solder, how to troubleshoot reversed power and ground leads, and how to program using the Arduino. Of course, Java's support for the serial port is annoying (thank goodness for RXTX). Who knows what will grow from the seeds planted this way? If you get a chance to work with kids who have an interest in technology, take it! It can be very rewarding.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video