CS 2000 -
Program Design in C
Lab session - Week 11
Your last two Arduino projects of the semester will deal with infrared communications. This lab will prepare you for those projects. In this lab, you will:
This lab is self-paced, but your instructor will be happy to help you with any questions you have as you complete the lab.
When you finish this lab, have the TA check your work and mark the roll sheet (to get credit).
At the beginning of every hardware lab, please review the safety precautions from lab #2. Remember, please think carefully about your actions.
Students should again work in pairs for this lab and the next two assignments. You may choose any partner for lab, and you can switch later. For the Arduino project assignments, once you choose a partner, you will not be allowed to switch. I recommend choosing a partner whose understanding of the material is equivalent to your own. (Don't let your partner do all the work.)
Partners on the last two Arduino projects will have to follow the pair programming rules.
Both partners should wire up their boards during this lab.
Here is the schematic for today's lab. You may also see my completed Arduino circuit by clicking on this picture.
Come up to the front and check out an IR LED (for creating infrared light) and an IR receiver (for detecting IR light). Your TA will mark a list when you pick up the parts. These parts are yours to keep. There are a few things you should note about the parts:
The IR detector only detects infrared light modulated at 38 kHz. An infrared light source has to be toggled on and off approximately 38,000 times a second or it will be ignored by this detector. This allows the detector to ignore IR signals that are not intentionally sent.
The detector outputs a low signal on its output pin when it detects 38 kHz infrared light.
The maximum detection signal length is about 1/2 second. If the detector continues to see a signal after 1/2 second, the detector stops outputting a low signal. This shouldn't be a problem for us, our signals will all be very short.
For full details, see this datasheet.
The IR LED is just like any other LED, except that it outputs infrared light. You will not see any light coming from this LED when it is working. The light that it outputs is not modulated - it is just on, or off, based on how you use it.
In my code below, I set up a PWM timer to control output pin 11 on the Arduino board, and then I connected an IR LED to this pin. I set the timer controls to turn the LED on and off 38,000 times each second, thus producing the modulated light that the detector can detect.
Once you have a bit of simple code in place, you can easily send and detect infrared signals using these parts.
Download this starting code into an Arduino project. Save the project in your own personal space.
Review this code - the functions help with both transmission and reception of IR signals. Here are the key ideas:
transmitting_IR: This global variable
will be set to true when the IR LED is on and sending 38 kHz
infrared light. It will be false when the IR LED is off.
receiving_IR: After a call to the
detect_IR() function, this global variable will be set to
true if the IR receiver is detecting infrared light; otherwise, it will be
turn_on_IR(): These functions control the
PWM timer and digital output pin 11. It is assumed that you will
wire up pin 11 to your infrared LED. When you call these
functions, the LED either stops or starts sending 38 kHz modulated
infrared light. In addition, these functions appropriately
In each of these functions I directly modify the memory locations associated with the Arduino's timers and output pins. It only takes a few lines of code, but knowing how to write them is the hard part. (It took me the better part of six hours to locate the information and understand how to write the three critical lines of code.) If you are interested in learning about timers, check out this blog entry titled "Secrets of Arduino PWM", and refer to this timer register schematic.
detect_IR(): This function simply
reads input pin 12 to see if the infrared receiver is receiving a
signal. It always sets the global variable
receiving_IR based on the result. (You should
repeatedly call this function and then check the variable to see if an
IR signal is detected.)
Note: The functions and global variables are not the ideal way to send or receive IR signals. They are a bit 'slow', and the code can be written more compactly if performance or timing is critical. On the other hand, for teaching reasons I feel this arrangement is ideal.
Wire up a button to your Arduino as follows:
Note: Since the left and right side pins of the button are connected through the button, you can connect your wires to either side of the button. (See above schematic.)
setup() function that you downloaded already sets
up this button. Here is the code again for review:
pinMode (4, INPUT); // Button digitalWrite (4, HIGH); // Button pull-up resistor
The first line sets up pin 4 as an input, and the second line activates the internal pull-up resistor. The input will be high as long as the button is not pressed. As soon as the button is pressed, a path to ground is created and the input will become low.
It's time to test the button. Copy this loop function to the end of your Arduino project:
Download your completed code into your Arduino. Open up the serial debugging window. You should be receiving a lot of 'o' characters. When you press the button, these should change to 'x' characters. Make sure the button works before proceeding.
Because the IR LED emits invisible light, you will wire up a second visible-light LED to the same output. That way, when the IR LED is on, the second LED will also be on. (This is possible because the Arduino outputs enough power for both LEDs.) Wire up the LEDs as shown below:
In the pictures above, notice that I put a bend in the leads of my IR LED so that it points horizontally towards the front of the board. I want the IR light to shine directly on my receiver.
Because you have a debugging LED hooked up to the output pin, you should be able to see when
the IR LED is transmitting. Change your
loop() function as follows:
The code now uses the button to control the IR LED output. Try it. When you press the button, the IR LED and debugging LED should light up. (You'll only be able to see the debugging LED, and it may appear a bit dim because it is turning on and off 38,000 times each second.) Make sure the button controls the LEDs before moving on.
The debugging LED above is very useful. You'll add one to the IR receiver as well so that you can see when it is receiving an IR signal. Wire up the IR receiver as follows:
Note: The second debugging LED is wired up to a resistor and +5 volts, not ground. The receiver outputs a low signal when it receives data, so I use the low as the ground for the LED. That way, the LED will illuminate when data is received. Care is required to orient the LED properly. In my wiring I was clever about how I connected the LED to +5 volts -- I made use of the +5 volts being supplied to the receiver.
Double-check the wiring, then proceed to step 6.
A simple way to test the receiver is to simply write some
statements to check for reception. The code below checks for
received IR light, and if it is detected, it determines the length of
time that the light is received. Copy this code to the end of
In addition, add these global variables to the top of your code:
Again download your code to the Arduino and open up the serial debugging window. Every time your receiver 'sees' a pulse of infrared light, your code will compute the elapsed time and send it out the serial port to the debugging window. Try the following:
Press the button on your board. Your receiver should 'see' the infrared light being transmitted from your LED.
Test your board with your partner's board. You should be able to see the IR light from each other's boards. (You may need to point their LED directly at your receiver.)
Test your board with my board at the front of the room. My board is configured to emit a 1 millisecond pulse every five seconds. (Have your partner point my board's LED directly at your receiver.)
We brought a remote control with us to lab. Use the remote control in the lab to test your board. Point it at the receiver and press any button. You should see a bunch of pulses.
Caution: A lot of students will be testing their boards. You may see pulses from their tests.
The code only displays the 'on' time for each light pulse. When you press the button on your project, the IR light is sent and then detected by your receiver. Your code then computes the total length of time that the light is received. This is useful, but you need more information to send data.
Adapt the code so that when the 'on' time is sent, it puts a small message in front of it like this (in the debugging window):
IR signal: 234170
Serial.print("...") to add this additional
message to the appropriate part of the code.)
Next, adapt the code so that it also measures the length of time between pulses. In other words, measure the length of time during which the receiver is not receiving data. Display a message like this to the debugging window:
IR Dark: 234170
Once you get this working you have completed the lab. Find a partner for the next project and begin discussing how you would send characters between Arduino boards using infrared signals.
Demonstrate your working IR transmitter and receiver for the TA. Your TA will mark the roll to give you credit for the lab.
The lab instructor gave out parts during lab. Please see Peter Jensen in his office to get the needed parts. You should complete this lab as it is the basis of the next Arduino project.
Page updated Friday November 11, 2011 at 10:29:36.