PICAXE Infra-Red Interfacing



Communicating using invisible light is quite magical, and is quite often a lot easier than wireless interfacing.



Index


The INFRAIN and INFRAIN2 Commands

The INFRAIN command is available on the PICAXE-18A, 18X, 28, 28A, 28X and 40X but the INFRAIN2 command is only supported by the PICAXE-08M ( the INFRAIN command is not available on the PICAXE-08M ).

Both INFRAIN and INFRAIN2 commands allow IR Command Codes using the Sony IR Protocol to be received. The INFRAIN2 command allows all posisble 128 command codes to be received while INFRAIN allows a subset of 17 command codes to be received.

A PICAXE can read IR Command Codes sent from a television remote control which uses the Sony IR protocol and can also read the IR Command Codes sent from a PICAXE-08M which is using the INFRAOUT command. For this to be succesful, the 'device code' specified in the INFRAOUT command must be '1' and the 'command code' must be appropriate to be recognised by the INFRAIN or INFRAIN2 commands.

The INFRA variable

On all PICAXE's the result returned by INFRAIN and INFRAIN2 is placed in the 'infra' variable. On all PICAXE's except the PICAXE-08M this is a special vraiable separate to the others, on the PICAXE-08M, 'infra' is also the 'b13' variable.

The IR Receiving Circuit

This is the standard Infrared Receiving circuit which uses the TSOP18 IR Receiver chip available from Revolution Education Ltd and distributors as "LED020".

.------------.           +V  ------------.---------.-------------------.----------
|     __     |                          .|.       .|.                  |
|   /    \   |                          | | 4K7   | | 330R             |
|  |      |  |                TSOP18    |_|       |_|          .-------^-------.
|   \    /   |                  .--.     |         |           |     PICAXE    |
|     --     |           -->  .-|  |-----^---------|-----.     |               |
|            |           -->  | |  |-----.         |     `---->| InfraIn Pin   |
`-..--..--..-'           -->  `-|  |-----|---------{           |               |
  ||  ||  ||                    `--'     |       __|__         |               |
  ||  ||  ||                             |       ===== 4.7uF   `-------.-------'
  ||  ||  ||                             |         |                   |
  OP  0V  +V             0V  ------------^---------^-------------------^----------

The 330R resistor and 4.7uF capacitor are there to smooth the power supply to the IR Receiver and prevent problems which may be caused by its operation. You may be able to operate the device without the resistor or capacitor as the IR Receiver has a specified operating range of 4.5V to 5.5V and it will operate at lower voltages, but it is recommended that thay are fitted; the IR Receiver can draw up to 5mA from the supply when operating which can cause unwanted effects on the power supply which can lead to unreliable operation of other devices using the same power supply.

The 4K7 resistor is simply a pull-up to give a clean signal into the PICAXE when the IR Reciever is powered through a resistor and capacitor, and in some cases it may be possible to omit it entirely, although it is recommended that it is fitted.

The signal from the TOPS18 sensor can connect to any PICAXE Digital Input, but to use the INFRAIN, its output must connect to Digital Input 0 on the PICAXE-18A, 18X, 28, 28A, 28X and 40X and to Digital Input 3 on the PICAXE-08M.


The INFRAOUT Command

The INFRAOUT command is available only on the PICAXE-08M. It allows IR Command Codes using the Sony IR Protocol to be transmitted. A PICAXE can read the IR Command Codes sent from a PICAXE-08M using the INFRAOUT command by using the INFRAIN and INFRAIN2 commands. For this to be succesful, the 'device code' specified in the INFRAOUT command must be '1' and the 'command code' must be appropriate to be recognised by the INFRAIN or INFRAIN2 commands.

The syntax of the INFRAOUT command is as follows ...

    INFRAOUT device code , command code

The 'device code' specifies which type of device it is talking to with '1' being necessary to control a television or for communications with another PICAXE using the INFRAIN or INFRAIN2 command.

The 'command code' is a value between 0 and 127 which is an instruction to carry out a particular action for the equipment indicated by the device code.

The Basic IR Transmitting Circuit

This is the basic IR Transmitter circuit which is the easiest to construct although it does have limited transmission range ...

+V  --------.
            |
    .-------^-------.
    |   PICAXE-08M  |            IR LED
    |               |     ____
    |             0 |----|____|-----.
    |               |     220R     _|_   -->
    |               |             _\./_  -->
    `-------.-------'               |    -->
            |                       |
0V  --------^-----------------------'

The IR LED is simply driven through a current limiting resistor much as any visible LED would be, and the value of the current limiting resistor should be chosen in the same way that it would be for a normal LED.

With the PICAXE able to source some 20mA per Output Pin, any value from 220R upwards should be suitable ( at 5V operating voltage ); the lower the resistor value, the greater the IR light intensity, and the greater its range.

Note that the IR LED must be connected between the IR Transmitting Pin and 0V; it will not work correctly if connected between +V and the IR Transmitting Pin.

A Higher Power / Longer Range IR Transmitting Circuit

The intensity of the IR signal sent by an IR LED depends upon how much current is passed through it; the more current, the greater the intensity of its output, and the greater range it will have.

The key to greater range is therefore in getting more current through the IR LED and this can be achieved by lowering the current resistor value, and, because the times when the LED is actually on are so brief, sometimes removing it entirely.

Do not attempt to drive an IR LED ( or any other LED ) directly from a PICAXE Output Pin without a current limiting resistor as this will cause irreparable damage to the PICAXE.

To get more current we can drive the IR LED through a separate transistor or FET, and we can also connect it to a higher voltage power supply; this also has the advantage of preventing any 'current dumping' when the LED is activated from having an adverse effect on the devices connected to the normal power supply.

To get even more intense IR outputs we can use mutliple IR LED's either in parallel with each other or in series.

+VL ------------------------------------.------.
                                       .|.    .|.
                                       | |    | |
                                       |_|    |_|
                                       _|_    _|_
                                      _\./_  _\./_  -->
+V  -----------.           IR LED's    _|_    _|_   -->
               |                      _\./_  _\./_  -->
       .-------^-------.                |      |
       |   PICAXE-08M  |                }------'
       |               |     ___     | /
       |             0 |----|___|----|<
       |               |             | \
       |               |                |
       `-------.-------'                |
               |                        |
0V  -----------^------------------------'

The design of specific IR LED driving circuits is beyond the scope of this document.

Testing IR Output

One of the biggest challenges to IR communications is being able to tell when anything is working or not; with no visible light generated by an IR LED, it's no easy task checking if the LED is emitting an output or not.

Help comes to hand from a perhaps unexpected quarter; the Webcam. Most Webcams, along with Camcorders ( Digital and Analogue ) and Digital Cameras are very sensitive to IR light and will happily show transmissions from IR LED's. Simply point your Webcam, Camcorder or Camera at the LED and look for it illuminating on the preview screen.

A simple design for a PICAXE-based IR Tester is given later.


Inter-PICAXE IR Communications

You can communicate between two PICAXE's by using INFRAOUT and the INFRAIN and INFRAIN2 commands. PICAXE's using the INFRAIN2 command will be able to receive 128 different commands, while those using INFRAIN will only be able to receive a limited subset of 17 commands.

+V  ----------.                                       .----.---------------.----------  +V
              |                                      .|.  .|.              |
              |               ___                    | |  | |              |
      .-------^-------.   .--|___|--.                |_|  |_|      .-------^-------.
      |   PICAXE-08M  |   |         |           .--.  |    |       |   PICAXE-08M  |
      |               |   |        _|_   -->  .-|  |--^----|---.   |               |
      |             0 |---'       _\./_  -->  | |  |--.    |   `-->| 3             |
      |               |             |    -->  `-|  |--|----{       |               |
      |               |             |           `--'  |  __|__     |               |
      `-------.-------'             |                 |  =====     `-------.-------'
              |                     |                 |    |               |
0V  ----------^---------------------'                 `----^---------------^----------  0V
    TransmitterLoop:                  ReceiverLoop:
        b0 = command code                 INFRAIN2
        INFRAOUT 1,b0                     :
        PAUSE 50                          GOTO ReceiverLoop
        :
        GOTO TransmitterLoop

Note that if you are performing Inter-PICAXE communications, it is necessary to use the television 'device code' of '1' in the INFRAOUT command or the INFRAIN and INFRAIN2 commands will ignore the transmission. This means that, if you are performing inter-PICAXE IR communications, any IR Command Codes sent may also have an effect upon on any television you have which uses the Sony IR Protocol.

A PICAXE-08M as a Slave Transmitter

Only the PICAXE-08M has an INFRAOUT command, but a PICAXE-08M can be used as an 'IR Peripheral' allowing any PICAXE to send IR by giving that task to an attached PICAXE-08M to perform ...

+V  ----------.--------------------------------------------.
              |                                            |
      .-------^-------.                            .-------^-------.
      |   ANY PICAXE  |                            |   PICAXE-08M  |          IR LED
      |               |                            |               |     ___
      |    Output Pin |--------------------------->| Input pin   0 |----|___|----.
      |               |                            |               |            _|_   -->
      |               |                            |               |           _\./_  -->
      `-------.-------'                            `-------.-------'             |    -->
              |                                            |                     |
0V  ----------^--------------------------------------------^---------------------'
    MasterLoop:                       TransmitterLoop:
        b0 = command code                 SERIN RX_PIN,BAUD,b0
        SEROUT TX_PIN,BAUD,(b0)           INFRAOUT 1,b0
        PAUSE 50                          GOTO TransmitterLoop
        :
        GOTO MasterLoop

A PICAXE-08M as a Slave Receiver

If your PICAXE doesn't support INFRAIN or INFRAIN2, or only supports the INFRAIN command but you wish to receive the full 128 IR Command Codes, you can use a PICAXE-08M to receive the IR commands which then passes them onto your own PICAXE ...

+V  ---------.----.---------------.------------------------.----------
            .|.  .|.              |                        |
            | |  | |              |                        |
            |_|  |_|      .-------^-------.        .-------^-------.
       .--.  |    |       |   PICAXE-08M  |        |   ANY PICAXE  |
-->  .-|  |--^----|---.   |               |        |               |
-->  | |  |--.    |   `-->| 3  Output Pin |------->| Input Pin     |
-->  `-|  |--|----{       |               |        |               |
       `--'  |  __|__     |               |        |               |
             |  =====     `-------.-------'        `-------.-------'
             |    |               |                        |
0V  ---------^----^---------------^------------------------^----------
    ReceiverLoop:                     MasterLoop:
        INFRAIN2                          SERIN RX_PIN,BAUD,b0
        SEROUT TX_PIN,BAUD,(infra)        :
        GOTO ReceiverLoop                 GOTO MasterLoop


IR Remote Control Tester

Because the IR Receiver chip simply demodulates any IR signal it receives, and, although it has a specific 38kHz centre frequency at which its sensitivity is maximised it can respond to frequencies either side of the optimum, its output can be used to determine the presence of an approriately modulated IR signal even when INFRAIN and INFRAIN2 commmands are not available. When an IR signal is present the IR Receiver chip's output will be 0, and when not it will be 1.

It is therfore very easy to build an IR Remote Control Tester which will work for a great many Remote Control types ...

+V  ---------.----.---------------.-------------------------
            .|.  .|.              |
            | |  | |              |
            |_|  |_|      .-------^-------.
       .--.  |    |       |   PICAXE-08M  |
-->  .-|  |--^----|---.   |               |     ___
-->  | |  |--.    |   `-->| 3  Output Pin |----|___|----.
-->  `-|  |--|----{       |               |            _|_
       `--'  |  __|__     |               |           _\./_ LED
             |  =====     `-------.-------'             |
             |    |               |                     |
0V  ---------^----^---------------^---------------------^---

The following program simply waits until any IR signal is detected and then briefly flashes the LED to show something was received ...

        SYMBOL IR_PIN  = pin?
        SYMBOL LED_PIN = ?

    WaitForSignal:

        IF IR_PIN = 1 THEN WaitForSignal

        HIGH LED_PIN
        PAUSE 250
        LOW LED_PIN

        GOTO WaitForSignal

Note that the signal from the IR Receiver is Active Low, and it will read as a High signal ( 1 ) when no IR signal is present.


The Sony IR Control (SIRC) Protocol

Every command sent consists of a Start Bit, a 7-bit 'Command Code' indicating an action to be performed, followed by a 5-bit 'Device Code' indicating which device should act upon the Command Code.

To prevent errors in reception, most receivers will check that they have received consecutive duplicates of a command before acting upon a command, so it is usual for every command to be sent multiple times, with delays of 45mS between each transmission.

Every command has the following format ...

.---------.------------------------------------.--------------------------.
|  Start  |  C0   C1   C2   C3   C4   C5   C6  |  D0   D1   D2   D3   D4  |
`---------^------------------------------------^--------------------------'

The Start Bit is encoded as a pulse which is on for 2.4mS followed by an off period of 0.6mS.

A '0' bit is encoded as a pulse which is on for 0.6mS followed by an off period of 0.6mS.

A '1' bit is encoded as a pulse which is on for 1.2mS followed by an off period of 0.6mS.

            Start Bit              0           0           0             1
  |                          |           |           |           |                |
  |<------------------------>|<--------->|<--------->|<--------->|<-------------->|
  |                          |           |           |           |                |

   ____________________       _____       _____       _____       __________
  |                    |     |     |     |     |     |     |     |          |
__|                    |_____|     |_____|     |_____|     |_____|          |_____...


  |                    |     |           |     |     |           |          |     |
  |<------------------>|<--->|           |<--->|<--->|           |<-------->|<--->|
  |                    |     |           |     |     |           |          |     |
           2.4mS        0.6mS             0.6mS 0.6mS                1.2mS   0.6mS

The encoded data, 0.6mS, 1.2mS or 2.4mS on periods followed by 0.6mS off periods, is then used to modulate a 38kHz signal which is used to drive the IR LED.

When the encoded signal is on, the 38kHz signal is transmitted, when the encoded signal is off, the 38kHz signal is not transmitted, this gives a 'modulated' 38kHz signal which is actually used to drive the IR LED ...

  ||||||||||||||||||||||     |||||||     |||||||     |||||||     ||||||||||||
__||||||||||||||||||||||_____|||||||_____|||||||_____|||||||_____||||||||||||_____...


Decoding without INFRAIN or INFRAIN2

Because the Sony IR protocol is quite slow and uses fairly long pulses for modulation, it is possible to decode the received Infra-Red signal without having to use the INFRAIN or INFRAIN2 commands at all.

This is ideal for those PICAXE's which do not support INFRAIN or INFRAIN2 when it is desirable to use those commands, and where it is not possible to connect the IR Receiver to the Digital Input which the INFRAIN and INFRAIN2 commands require.

The hardware interface is the same as described earlier for receiving infra-red, except that the IR Receiver's output can be connected to any available Digital Input ...

+V  ---------.----.---------------.----------
            .|.  .|.              |
            | |  | |              |
            |_|  |_|      .-------^-------.
       .--.  |    |       |  ANY PICAXE   |
-->  .-|  |--^----|---.   |               |
-->  | |  |--.    |   `-->| Digital Input |
-->  `-|  |--|----{       |               |
       `--'  |  __|__     |               |
             |  =====     `-------.-------'
             |    |               |
0V  ---------^----^---------------^----------

The following program can be used to decode the 128 possible command signals sent by a Remote Control transmitting using the Sony IR protocol into a byte value in 'b0' ...

        SYMBOL IR_INPUT   = 0

        SYMBOL MIN_PULSIN = 200
        SYMBOL PAUSE_TIME = 20
        SYMBOL BIT_TIME   = 80

    Loop:
        GOSUB BruteForceInfraIn2
        GOTO Loop

    BruteForceInfraIn2:

        PULSIN IR_INPUT, 0, w0
        IF w0 < MIN_PULSIN THEN BruteForceInfraIn2

        PAUSE PAUSE_TIME
        PULSIN IR_INPUT, 0, b0

        PULSIN IR_INPUT, 0, b0
        PULSIN IR_INPUT, 0, b1
        PULSIN IR_INPUT, 0, b2
        PULSIN IR_INPUT, 0, b3
        PULSIN IR_INPUT, 0, b4
        PULSIN IR_INPUT, 0, b5
        PULSIN IR_INPUT, 0, b6

        b0   = b0 / BIT_TIME
        bit1 = b1 / BIT_TIME
        bit2 = b2 / BIT_TIME
        bit3 = b3 / BIT_TIME
        bit4 = b4 / BIT_TIME
        bit5 = b5 / BIT_TIME
        bit6 = b6 / BIT_TIME

        RETURN

The program will work at 4MHz and also at 8MHz with the values for all constants doubled.

At 16MHz, the PULSIN times will be greater than 256 which presents problems with the use of byte variables, however a little ingenuity can overcome that problem when performing the conversion of sampled pulse times to a command byte value.

Note that the decoding in the above program does not check the device identifier to which the infra-red command was sent and nor does it make any other checks to confirm the validity or reliability of the transmitted data. Such extensions and checks could all be added with a bit of experimentation. The most obvious addition being to check that the same command has been received at least twice before responding to it ...

        SYMBOL lastB0 = b13

    ReadyForNewCommand:

        lastB0 = $FF

    Loop:
        GOSUB BruteForceInfraIn2
        IF lastB0 = b0 GOTO GotNewCommand
        lastB0 = b0
        GOTO Loop

    GotNewCommand:
        :
        GOTO ReadyForNewCommand

Many thanks go to Bernard Grabowski and Peter Dixon for pointing out this solution and providing demonstration code.


Transmitting without INFRAOUT

Decoding the raw Sony IR signal can only be done because the IR Receiver is able to demodulate the actual 38kHz data stream which is far to fast for a PICAXE to handle directly.

The PICAXE is equally incapable of generating the 38kHz data stream to drive an IR LED directly as well, and could only realistically produce the data stream which is then used to modulate a 38kHz signal using external hardware.

Using a PICAXE to transmit IR without using the INFRAOUT command is currently beyond the scope of this document.


Decoding Toshiba TC9148P-based Remotes

Many 'vintage' remote controls use the Toshiba TC9148P Remote Control IC or variants of that device. These IC's produce a 12-bit data stream that is very easy to decode with a 4MHz PICAXE using 'bit-banged' techniques as described for decoding the Sony IR protocol without using INFRAIN or INFRAIN2 commands.

Some modern programmable conrollers may be configurable to generate a TC9148P compatible signal, and the decoding scheme should be applicable to remotes which use a clone or similar IC to the TC9148.

The TC9148P IC automatically handles single-shot button pushes, where the button must be released before a further button push transmission is sent, and a continuous mode where the transmission continues while the button is held. It is also capable of handling multiple button pushes simultaneously sending a single transmission with multiple button pushes indicated.

The 'good features' of the TC9148P can however be a problem when interfacing to a PICAXE as it can make handling of button pushes quite complex. The easiest solution is to ignore multiple button pushes by checking for specific commands received and discarding those which indicate multiple button pushes. This may be a necessity anyway as testing showed that some continuous button pushes may occassionally transmit unexpected codes, although that may have been a 'feature' of the specific remotes used for testing.

The auto-repeating or one-shot nature of transmission may also be a problem in some applications, however it should be a fairly simple matter to re-wire the remote control to generate the required button pushes for particular buttons. Single button pushes can be made continuously transmitting so they are not missed and auto-repeating button pushes can be made single-shot if they cause extrneous and unwanted application activity.

A TC9148P-based remote control with just a few buttons is often ideal when only simple IR control is needed, and they can often be picked up at bootsales and elsewhere for little more than a few pence. It is of course impossible to tell if a remote control uses a TC9148P or not before purchasing, however, a good guideline is that if it looks 'archaic' and has only a few buttons then it may well do so, although it could equally contain a Mitsubishi M50560-100P, NEC uPD1913C or any other exotic and no longer well documented device, or one which uses a peculiar protocol, none of which are quite so useful here.

Given the low cost of such remotes it is however often worthwhile taking a chance, and it should be possible to modify a non-TC9184P remote by using the IC from another which is. If nothing else, you'll be buying a lowcost Infrared LED possibly cheaper than over the counter.

The datasheet for the Toshiba TC4198P, with instructions on button wiring and LED interface along with details of the transmission protocol, can be found at ...

The hardware required to decode the TC9148P signals is the same as that for decoding Infrared Input generally, and is as discussed earlier ...

+V  ---------.----.---------------.----------
            .|.  .|.              |
            | |  | |              |
            |_|  |_|      .-------^-------.
       .--.  |    |       |  ANY PICAXE   |
-->  .-|  |--^----|---.   |               |
-->  | |  |--.    |   `-->| Digital Input |
-->  `-|  |--|----{       |               |
       `--'  |  __|__     |               |
             |  =====     `-------.-------'
             |    |               |
0V  ---------^----^---------------^----------

The TC9148P protocol is quite simple and consists of 12 pulses; a "1" is represented by a pulse that is 3-units long followed by a gap of 1-unit, and a '0' is represented by a pulse that is 1-unit long followed by a gap of 3-units. Each 'unit' is approximately 400uS long ( with the TC9148P clocked at 455kHz ), so a "1" is a pulse of about 1.2mS duration and a '0' of about 400uS duration ...

            1                1                 0                0
  |                 |                 |                 |                |
  |<--------------->|<--------------->|<--------------->|<-------------->|
  |                 |                 |                 |                |

   ____________      ____________      ____              ____
  |            |    |            |    |    |            |    |
__|            |____|            |____|    |____________|    |___________


  |                 |            |    |                 |    |
  |<--------------->|<---------->|    |<--------------->|<-->|
  |                 |            |    |                 |    |
          1.6mS          1.2mS               1.6mS       0.4mS

The command transmitted consists of three parts; a 3-bit header code identifying the remote control, 3 bits of information which indicates whether the button pushes are continuous ( H, buttons 1-6 ) or one-shot ( S1 is button set 7-12, and S2 is button set 13-18 ), and 6 bits representing the buttons that are held in a particular set ...

.--------------.-------------.--------------------------.
|  C1  C2  C3  |  H  S1  S2  |  D1  D2  D3  D4  D5  D6  |
`--------------^-------------^--------------------------'

The following program will read the full 12-bit code and place it in 'w0' in bit 11 (C1) to bit 0 (D6) ...

        SYMBOL IR_INPUT   = 0

        SYMBOL MIN_PULSIN = 20
        SYMBOL BIT_TIME   = 80

    Loop:
        GOSUB InfraInTC9148P
        GOTO Loop

    InfraInTC9148P:

        PULSIN IR_INPUT, 0, b11
        IF b11 < MIN_PULSIN THEN InfraInTC9148P

        PULSIN IR_INPUT, 0, b10
        PULSIN IR_INPUT, 0, b9
        PULSIN IR_INPUT, 0, b8
        PULSIN IR_INPUT, 0, b7
        PULSIN IR_INPUT, 0, b6
        PULSIN IR_INPUT, 0, b5
        PULSIN IR_INPUT, 0, b4
        PULSIN IR_INPUT, 0, b3
        PULSIN IR_INPUT, 0, b2
        PULSIN IR_INPUT, 0, b1
        PULSIN IR_INPUT, 0, b0

        b0   = b0 / BIT_TIME
        bit1 = b1 / BIT_TIME
        bit2 = b2 / BIT_TIME
        bit3 = b3 / BIT_TIME
        bit4 = b4 / BIT_TIME
        bit5 = b5 / BIT_TIME
        bit6 = b6 / BIT_TIME
        bit7 = b7 / BIT_TIME

        b1    = b8  / BIT_TIME
        bit9  = b9  / BIT_TIME
        bit10 = b10 / BIT_TIME
        bit11 = b11 / BIT_TIME

        RETURN


PICAXE is a trademark of Revolution Education Ltd. PICmicro is a registered trademark of Microchip Inc. Sony is a registered trademark of Sony Corporation.





Associated Articles

  The PICAXE Processors
  PICAXE News
  PICAXE Questions & Answers
  PICAXE Comparisons
  PICAXE Pinouts
  PICAXE Serial Interfacing
  PICAXE Wireless Interfacing
  PICAXE LCD Interfacing
  PICAXE LCD Interfacing
  A Real-Time Clock for the PICAXE-18X
  PICAXE Optimisations
  The PICAXE Birthday Box Project
  PICAXE Telephone Exchange Simulator
  The Brainf**ked PICAXE
  The PICAXE Extended Programming Interpreter
  Build Your Own Basic Stamp
  Tech Toys



Sites to Visit

  PICAXE Home Page
  Revolution Education Ltd

  Tech-Supplies Ltd



Site Navigation

  Home Page
  What's New
  Search
  Add Bookmark
  Have Your Say
  Guestbook




First published on Friday the 30th of April, 2004 at 18:51:49
Last upload was on Monday the 23rd of August, 2004 at 00:20:47