Kronos Robotics and Electronics
Site Map
 
Home Zeus Projects App Notes Downloads Dios Athena Forums
 

DAN102

Interfacing to an IR Module

  By Michael Simpson

Ever wanted to control one of your projects remotely.  How about data entry with out a keypad hookup? With a simple IR module and a bit of programming you can start collecting keystrokes with the best of them. 

I wont go into the theory behind infrared diodes or transistors nor will I get into distance detection. 

I will be concentrating on two particular modules.  The Sharp GP1U58X and the Vishay TSOP1840.  Both these modules operate at 40kHz and are well suited to our applications.  

There are a great number of remotes, and protocols to match but I will be writing about one in particular.  The Sony protocol.  

  • Its an easy protocol.  It is based on a pulse width bit stream which is very easy for microcontrollers to deal with.

  • The remotes are readily available. 

The Protocol

Lets start by diving into the protocol.  The IR module will output a pulse stream of bits with varying widths.  The widths are defined as follows. 

Start Bit 2400us
State 1 1200us
State 2 600us

The world is not perfect and to compensate for various manufactures we have expanded the specification a bit as follows.

Start Bit Any pulse greater than 2000us wide.
State 1 Any pulse between 1000us and 2000us wide
State 2 Any pulse less than 1000us

The actual protocol consist of a starting bit followed by 12 data bits.  The data bits can be further broken down into command and device bits as shown.

S C C C C C C C D D D D D

These bits come with the least significant first and will be represented as the command byte and the device byte.  We will get into this further when we look at the code.

The Modules

Lets look a little closer at the modules before we wire them up.

The Vishay and sharp modules are interchangeable except for the pin out as shown here.

Sharp
Pin1 Out

Pin2 VCC (+5v)

Pin3 GND

Vishay
Pin1 Out

Pin2 GND

Pin3 VCC (+5v)

 

The metal case can be removed from the Sharp module as shown here. 

Kronos Robotics carries both module just click on one of the following links purchase one.

Sharp GP1U58X

Vishay TSOP1840

 

The Circuit

Wire up the circuit as shown. You can use the Sharp or the Vishay module.  I used a Dios Ultra 32 Pin module here but any Dios Ultra chip or board will work.

Schematic 1

 

You don't have to hook up the LED for this first program.   We are just going to read several bits from the module and display them.  Something to keep in mind is that the Dios pulsin command returns a value in .1us units.  So a value of 24000 will be 2400us.  or 2.4ms.

Test Program 1 download it here


 'Dios IR Demo 1
func main()
loop:
readbits()
goto loop
endfunc

func readbits()
input 1

const irpin 3

dim b1,b2,b3,b4,b5,b6,b7,b8,b9,b10
dim b11,b12,b13,b14,b15,b16,b17,b18,b19,b20
dim b21,b22,b23,b24,b25,b26,b27,b28,b29,b30

pulsein irpin,0,1000,b1
pulsein irpin,0,1000,b2
pulsein irpin,0,1000,b3
pulsein irpin,0,1000,b4
pulsein irpin,0,1000,b5
pulsein irpin,0,1000,b6
pulsein irpin,0,1000,b7
pulsein irpin,0,1000,b8
pulsein irpin,0,1000,b9
pulsein irpin,0,1000,b10
pulsein irpin,0,1000,b11
pulsein irpin,0,1000,b12
pulsein irpin,0,1000,b13
pulsein irpin,0,1000,b14
pulsein irpin,0,1000,b15
pulsein irpin,0,1000,b16
pulsein irpin,0,1000,b17
pulsein irpin,0,1000,b18
pulsein irpin,0,1000,b19
pulsein irpin,0,1000,b20
pulsein irpin,0,1000,b21
pulsein irpin,0,1000,b22
pulsein irpin,0,1000,b23
pulsein irpin,0,1000,b24
pulsein irpin,0,1000,b25
pulsein irpin,0,1000,b26
pulsein irpin,0,1000,b27
pulsein irpin,0,1000,b28
pulsein irpin,0,1000,b29
pulsein irpin,0,1000,b30

'Now display the data
print b1," ", b2," ", b3," ", b4," ", b5," ";
print b6," ", b7," ", b8," ", b9," ", b10," ";
print b11," ", b12," ", b13," ", b14," ", b15," ";
print b16," ", b17," ", b18," ", b19," ", b20," ";
print b21," ", b22," ", b23," ", b24," ", b25," ";
print b26," ", b27," ", b28," ", b29," ", b30

endfunc
 

To work the IR.lib file must be in the lib directory of your Smart Module Editor.

Program listing 1 is just a simple program to demonstrate the raw pulse widths of the various bits.

The output should look something like this.

Figure 1

 

I have indicated a start bit in the Figure 1.   Generally anything over 2000 I will conceder a start bit.  Anything under 1000 will be considered a 0 and the rest a 1.   With this in mind the displayed readings above would be 000010010000 or a IR command of 16 and a device of 1.

Next

Wire up the LED  as shown in Schematic 1.  Note that LED1 has a integrated resistor.  If you dont have this type of LED make sure you use a 100-390 ohm resistor in series with the LED.

Test Program 2 download it here

'Dios IR Demo 2
func main()
low 4
output 4

dim cmd


loop:
cmd = IRread(3,9000)
if cmd = 0 then
low 4
goto loop
endif
high 4
print cmd," ",IRdevice
goto loop

endfunc

include lib\DiosIR.lib

 

The Dios software comes with a library that reads the IR code for you.   In Program #2 we use this library to read the commands.   One thing the library does is to add 1 to the command code.  This is done so that the library can return a 0 if a timeout occurs.  It also lets the number keys return the actual value of the number key that is pressed.

Be sure to check out the Dio IR library help file for more information and functions.

Parts List

Easy RS232 Driver  

DiosPro 28 Pin Chip

Dios 32 Pin Carrier (Carrier #1)

Vishay IR Module

 

Copyright © 2001 - 2007 Kronos Robotics