599 Menlo Drive, Suite 100
Rocklin, California 95765, USA
Office:
(916) 624-8333
Fax:
(916) 624-8003
General:
info@parallax.com
Technical:
support@parallax.com
Web Site:
www.parallax.com
Educational:
www.stampsinclass.com
Infrared Remote AppKit (#29122)
A Wireless Keypad for Your BASIC Stamp
®
Microcontroller Module
With a universal remote and an infrared receiver, you can add a wireless keypad to your BASIC Stamp
Applications. The IR receiver is inexpensive, and only takes one I/O pin. Universal remotes are also
inexpensive, easy to obtain and replace, and have enough buttons for most applications. The parts in
this kit along with the example programs make it possible to enter values and control your projects in the
same way you might with a TV, VCR, or other entertainment system component.
IR Remotes can also add zing to your robotics projects. While this package insert provides you with
the essential background information, circuits, and example programs to get started, you can learn
lots more with
IR Remote for the Boe-Bot
by Andy Lindsay of Parallax Inc. This text is, for the most
part, a continuation of
Robotics with the Boe-Bot,
but with an IR remote twist. It follows the same
format in terms of introducing new hardware, explaining how things work, and demonstrating new
PBASIC techniques. IR remote applications for the Boe-Bot
®
robot include remote control, keypad
entry control, hybrid autonomous and remote control, and remote motion sequence programming.
Kit Contents*
Infrared Remote Parts List:
(1) 020-00001
(1) 350-00014
(1) 150-02210
(1) 800-00016
Universal Remote and
Universal Remote Manual
IR detector
Resistor – 220
Ω
Jumper wires – bag of 10
321
*Requires 2 alkaline AA batteries, not included
How IR Communication Works
The universal remote sends messages by strobing its IR LED at 38.5 kHz for brief periods of time. The
actual data is contained in the amount of time each strobe lasts. Each IR protocol is different. In general,
the amount of time each 38.5 kHz signal lasts transmits some kind of message. One duration might
indicate the start of a message, while another indicates a binary-1, and still another indicates a binary-0.
The IR detector's output pin sends a low signal while it detects the 38.5 kHz IR signal, and a high signal
while it does not. So, a low signal of one duration might indicate the start of a message, while another
indicates a binary-1, and still another indicates a binary-0. This communication scheme is called pulse
width modulation (PWM), because when it is graphed against time, the IR detector's high/low signals form
pulses of different widths that correspond to their durations.
© 2004 Parallax Inc. • IR Remote AppKit (#29122) • 10/2004
Page 1 of 10
0. 6
ms
1.2
ms
0. 6
ms
te
mo
Re
The examples here will rely on the protocol that universal remotes use to control SONY
®
TV sets. This
protocol strobes the IR thirteen times with roughly a half-millisecond rest between each pulse. It results in
thirteen negative pulses from the IR detector that the BASIC Stamp can easily measure. The first pulse is
the start pulse, which lasts for 2.4 ms. The next twelve pulses will either last for 1.2 ms (binary-1) or 0.6
ms (binary-0). The first seven data pulses contain the IR message that indicates which key is pressed.
The last five pulses contain a binary value that specifies whether the message is intended to be sent to a
TV, VCR, CD, DVD player, etc. The pulses are transmitted in LSB-first order, so the first data pulse is
bit-0, the next data pulse is bit-1, and so on. If you press and hold a key on the remote, the same
message will be re-sent after a 20 to 30 ms rest.
Resting state
between message
packets = 20-30 ms
Resting states
between data pulses
= 0.6 ms
2.4
ms
Handheld Remote
Infrared Messages
Excerpt from
IR
Remote for the Boe-
Bot
text.
2.4 ms
0.6 ms
1.2 ms
0.6 ms
Start
0
Bit-0
0
0
Bit-2
0
0
Bit-4
1
0
Bit-6
1
0
Bit-8
0
0
Bit-10
0
IR Message Timing
Diagram
Values are approximate
and will vary from one
remote to the next
Bit-1
Bit-3
Bit-5
Bit-7
Bit-9
Bit-11
Start pulse
duration = 2.4 ms
Binary-0
data pulse
durations = 0.6 ms
Binary-1
data pulse
durations = 1.2 ms
Page 2 of 10
© 2004 Parallax Inc. • IR Remote AppKit (#29122) • 10/2004
IR Detection Circuit
For testing purposes, all you need is this IR detector circuit and the BASIC Stamp Editors’s Debug
Terminal.
Vdd
IR Detector Circuit
P9
220
Vss
1
2
3
IR detector viewed
from the top. Also
see Kit Contents
figure for pin map.
BASIC Stamp 2 "Bare-Bones" Example – IrRemoteCodeCapture.bs2
This example program demonstrates how to capture and display a remote code with the BASIC Stamp 2.
If you modify the $STAMP directive, it can also be used with the BASIC Stamp 2e or 2 pe.
√
√
√
√
√
First, make sure to use the documentation that comes with your universal remote to configure it to
control a SONY
®
TV.
Press the TV button on your remote so that you know it is sending TV signals.
Download or hand enter and run IrRemoteCodeCapture.bs2
Point the remote at the IR detector, and press/release the digit keys.
Also try POWER, CH+/-, VOL+/-, and ENTER to view the codes for these values.
' Ir Remote Application - IrRemoteCodeCapture.bs2
' Process incoming SONY remote messages & display remote code.
' {$STAMP BS2}
' {$PBASIC 2.5}
' SONY TV IR remote variables
irPulse
remoteCode
VAR
VAR
Word
Byte
' Stores pulse widths
' Stores remote code
DEBUG "Press/release remote buttons..."
DO
remoteCode = 0
DO
RCTIME 9, 1, irPulse
LOOP UNTIL irPulse > 1000
PULSIN 9, 0,
IF irPulse >
RCTIME 9, 0,
IF irPulse >
RCTIME 9, 0,
IF irPulse >
irPulse
500 THEN remoteCode.BIT0 = 1
irPulse
300 THEN remoteCode.BIT1 = 1
irPulse
300 THEN remoteCode.BIT2 = 1
' Get data pulses.
' Wait for end of resting state.
' Main DO...LOOP
© 2004 Parallax Inc. • IR Remote AppKit (#29122) • 10/2004
Page 3 of 10
RCTIME 9, 0,
IF irPulse >
RCTIME 9, 0,
IF irPulse >
RCTIME 9, 0,
IF irPulse >
RCTIME 9, 0,
IF irPulse >
irPulse
300 THEN
irPulse
300 THEN
irPulse
300 THEN
irPulse
300 THEN
remoteCode.BIT3 = 1
remoteCode.BIT4 = 1
remoteCode.BIT5 = 1
remoteCode.BIT6 = 1
' Map digit keys to actual values.
IF (remoteCode < 10) THEN remoteCode = remoteCode + 1
IF (remoteCode = 10) THEN remoteCode = 0
DEBUG CLS, ? remoteCode
LOOP
' Repeat main DO...LOOP
How IrRemoteCodeCapture.bs2 Works
Each time through the outermost DO…LOOP, the value of remoteCode is cleared. There's also an inner
DO…LOOP with an RCTIME command to detect the end of a high signal that lasted longer than 2 ms.
This indicates that the rest between message packets just ended, and the start pulse is beginning. The
first PULSIN command captures the first data pulse, and the IF…THEN statement that follows uses the
value of the irPulse variable to either set (or leave clear) the corresponding bit in the remoteCode
variable. Since the next data pulse has already started while the IF…THEN statement was executing, the
remainder of the next data pulse is measured with an RCTIME command. This next value is again used
to either set (or leave clear) the next bit in remoteCode. This is repeated five more times to get the rest
of the useful part of the IR message and set/clear the rest of the bits in remoteCode.
The BS2sx and BS2p handle remote codes a little differently. The programs usually search for the actual
start pulse with a PULSIN command instead of searching for the resting state between messages. They
also use PULSIN commands to capture all the pulses since the IF…THEN statements that sets bits in the
remoteCode variable complete before the starting edge of the next data pulse. To see a code example
that does this, see the #CASE statement for the BS2sx and BS2p inside the next example program's
Get_Ir_Remote_Code subroutine.
BASIC Stamp 2 Series Application Example – IrRemoteButtonDisplay.bs2
You can use this application example with BASIC Stamp 2, 2e, 2sx, 2p or 2pe modules to test your
remote and display which key you pressed.
√
√
√
√
√
As with the previous example program, make sure your remote is configured to control a SONY
TV first.
Update the $Stamp directive for the BASIC Stamp module you are using.
Download or hand enter, then run IrRemoteButtonDisplay.bs2.
Point the remote at the IR detector, press and release buttons
Make sure the Debug Terminal reports the correct button. Start with digits, channel, volume, etc.
You can modify or expand the SELECT…CASE statement to test for VCR keys defined in the Constants
section (Play, Stop, Rewind, etc.). There are usually several different codes for configuring universal
remotes to control SONY VCRs, so you may need to try a few before finding the code that makes the
Page 4 of 10
© 2004 Parallax Inc. • IR Remote AppKit (#29122) • 10/2004
remote speak the same PWM language as the TV controller. You can determine if the code worked
because number, CH/VOL+-, and POWER keys will still work after you have pressed the VCR button.
' -----[ Title ]-----------------------------------------------------------
' Ir Remote Application - IrRemoteButtonDisplay.bs2
' Process incoming SONY remote signals and display the corresponding button
' in the Debug Terminal.
' {$STAMP BS2}
' {$PBASIC 2.5}
' BS2, 2sx, 2e, 2p, or 2pe
' -----[ Revision History ]------------------------------------------------
' V1.0 - Supports most SONY TV and VCR control buttons.
'
Supports BASIC Stamp 2, 2SX, 2e, 2p, and 2pe modules.
' -----[ I/O Definitions ]-------------------------------------------------
' SONY TV IR remote declaration - input receives from IR detector
IrDet
PIN
9
' I/O pin to IR detector output
' -----[ Constants ]-------------------------------------------------------
' Pulse duration constants for SONY remote.
#SELECT $stamp
#CASE BS2, BS2E, BS2PE
ThresholdStart CON 1000
ThresholdPulse CON 500
ThresholdEdge CON 300
#CASE BS2P, BS2SX
ThresholdStart CON 2400
ThresholdPulse CON 500 * 5 / 2
#CASE #ELSE
#ERROR This BASIC Stamp NOT supported.
#ENDSELECT
'
'
'
'
PULSE durations
Message rest vs. data rest
Binary 1 vs. 0 for PULSIN
Binary 1 vs. 0 for RCTIME
' Binary 1 vs. start pulse
' Binary 1 vs. 0 for PULSIN
' SONY TV IR remote constants for non-keypad buttons
Enter
ChUp
ChDn
VolUp
VolDn
Mute
Power
TvLast
CON
CON
CON
CON
CON
CON
CON
CON
11
16
17
18
19
20
21
59
' AKA PREV CH
© 2004 Parallax Inc. • IR Remote AppKit (#29122) • 10/2004
Page 5 of 10