A PICAXE based Model Railway Controller

IMPORTANT WARNING

The designs on this page are presented "as is" with no warranty or guarantee of operational correctness. The electronic design has not been built, tested nor proven, and there is no guarantee that the designs are sound or that damage will not occur when used with your own equipment. The software has not been tested, debugged and may not include the functionality described in this article. Anyone using the hardware and software designs or information related to them herein does so entirely at their own risk. No liability whatsoever is accepted by the author.

This project is a theoretical exercise and a work in progress and should be considered as an educational resource rather than as a completed or functional project.



Who needs push-buttons, switches and levers when you can control model railway points, signals and lighting using an advanced digital system ?



This article describes the design of a PICAXE based model railway controller for the control of ancillary systems ( points, signals and lighting ) rather than trains themselves.

The entire system is based upon a network of PICAXE-08 processors linked to a master controller through a serial bus. Every PICAXE-08 is a receiver module which receives commands from the controller which can manipulate the four output lines from each receiver. Each line can be set high, low or pulsed, individually or in combination with each other.

This article does not consider how the receivers are connected to additional hardware to perform their intended tasks ( such as providing the correct voltage to activate points solenoids ), but it does detail the rest of the system.


The Network

                  9600                     2400
                  baud                     baud
      PSU                    Controller                Receiver
    .------.                 .--------.               .--------.
    |   +V |---.------------>| +V  5V |----.--------->| 5V     |---> OUT1
    |   0V |---|--.  .------>| RX  CX |----|--.------>| RX     |---> OUT2
    `------'   |  |  |  .----| TX     |    |  |       |        |---> OUT3
               |  }--|--|--->| 0V  0V |----|--|--.--->| 0V     |---> OUT4
               |  |  |  |    `---.----'    |  |  |    `--------'
       PC      |  |  |  |        |         :  :  :
    .------.   |  |  |  |     .--^--.      :  :  :     Receiver
    |   TX |---|--|--{  |     | LCD |      |  |  |    .--------.
    |   RX |<--|--|--|--{     `-----'      }--|--|--->| 5V     |---> OUT1
    |   0V |---|--{  |  |                  |  }--|--->| RX     |---> OUT2
    `------'   |  |  |  |                  |  |  |    |        |---> OUT3
               |  |  |  |                  |  |  }--->| 0V     |---> OUT4
     KEYPAD    |  |  |  |                  |  |  |    `--------'
    .------.   |  |  |  |                  :  :  :
    |   +V |<--'  |  |  |                  :  :  :     Receiver
    |   TX |------|--'  |                  |  |  |    .--------.
    |   RX |<-----|-----'                  `--|--|--->| 5V     |---> OUT1
    |   0V |<-----'                           `--|--->| RX     |---> OUT2
    `--.---'                                     |    |        |---> OUT3
       |                                         `--->| 0V     |---> OUT4
    .--^---.                                          `--------'
    |[][][]|
    |[][][]|
    |[][][]|
    `------'


The Network Protocol

There are two protocols; that between the PC and main controller and that used between the controller and the receivers. The PC to controller protocol is a character based ( command line ) interface and is discussed later. The protocol between the controller and the receivers is a binary, packet-based protocol and is explained here.

The controller communicates with the receivers by sending packets of data which are received by all receivers. Every packet contains an address and an action to take; those receivers who recognise the address will take the action requested, while those which do not will simply ignore it.

The protocol is extremely simply; send-and-forget, with no error checking capability, no acknowledgements that the packet was received and no retry mechanism. Whether the protocol is robust in real world use or not is a matter of conjecture as I have absolutely no experience of model railways, their control systems or knowledge of issues which affect track electronics. This article is intended to show a design which is flexible and works in ideal circumstances, but it may be necessary to improve upon it to provide a more robust practical implementation. That is however beyond the scope of this article.

All commands are three bytes long, with two nibbles in each byte, giving a six nibble command in total ...

    .-------------.-------------.-------------.
    | aaaa | bbbb | cccc | dddd | wxyz | WXYZ |
    `-------------^-------------^-------------'

The first four nibbles 'abcd' are the address of the receiver module which is required to respond to the command. The address is compared with that which set in each receiver using 'SYMBOL UNIT_ADDRESS = ', and if the address matches the action is obeyed. If any of the nibbles of the address are zero, it is considered a 'wildcard digit' and any digit in the same position with the receiver's address will be treated as matched with it. This allows 'wildcard addressing' so a single action can be sent to multiple receivers.

The addressing scheme allows 50,625 separate and uniquely identified receiver modules to used on a single network. Over 200,000 lights, 100,000 points and 50,000 signals can be controlled by a single network. Any receiver module can be used to control lights, points and signals.

Every command includes an action byte, two nibbles which indicates what the receiver should do when the command is received ...

         action
    .-------------.
    | wxyz | WXYZ |
    `-------------'

The bits of each nibble 'wxyz' and 'WXYZ' relate to each of the receiver units digital outputs; w/W is OUT4, x/X is OUT2, y/Y is OUT2 and z/Z is OUT1.

When a bit is set (1) in 'wxyz' the corresponding output bit is set high.

When a bit is set (1) in 'WXYZ' the corresponding output bit is set low.

When a bit is set (1) in both 'wxyz' and 'WXYZ' the corresponding output bit is set high for a period defined by 'SYMBOL PULSE_LENGTH' within that receiver module and is then set low again.

Examples ...

    .-------------.
    | 1000 | 0000 |     Sets OUT4 high
    `-------------'
    .-------------.
    | 0000 | 0100 |     Sets OUT3 low
    `-------------'
    .-------------.
    | 0000 | 1111 |     Sets OUT4, OUT3, OUT2 and OUT1 low
    `-------------'
    .-------------.
    | 1010 | 1001 |     Pulses OUT4, sets OUT2 high and sets OUT1 low
    `-------------'
    .-------------.
    | 0000 | 0000 |     No changes made
    `-------------'

For specific point and signal configurations, the following commands could be used ...

Points - OUT1/OUT2 to solenoids, OUT3/OUT4 routing indicators

    0101 1011   Pulse OUT1, set OUT3, clear OUT4
    1010 0111   Pulse OUT2, clear OUT3, set OUT4

Dual Points - OUT1/OUT2 to solenoids #1, OUT3/OUT4 to solenoids #2

    0001 0011   Pulse OUT1 - Points #1 through
    0010 0011   Pulse OUT2 - Points #1 turned
    0100 1100   Pulse OUT3 - Points #2 through
    1000 1100   Pulse OUT4 - Points #2 turned

Three light signals - OUT1=Red, OUT2=Yellow, OUT4=Green

    0001 1110   Set Red
    0010 1101   Set Yellow
    1000 0111   Set Green

Four light signals - OUT1=Red, OUT2/OUT3=Yellow, OUT4=Green

    0001 1110   Set Red
    0010 1101   Set Yellow Top
    0100 1011   Set Yellow Bottom
    0110 1001   Set both Yellow
    1000 0111   Set Green


Network Addressing

Every receiver module on the network is allocated a unique four-digit address using the numbers 1-9 and A-F. This addressing scheme allows 50,625 separate and uniquely identified receiver modules to used on a single network. Over 200,000 lights, 100,000 points and 50,000 signals can be controlled by a single network.

While having four digit addressing may seem like overkill ( few people will ever need 100,000 point controllers ), the addressing scheme allows controllers for various types of hardware to be defined, say, 1nnn for points, 2nnn for signals and 3nnn for lights.

Having four digits also allows controllers to be grouped into zones; controllers for street lighting could have the first digit specified to indicate lighting controller, the second a street identifier, the third a house within the street and the fourth a particular room. Such a scheme would allow control over 15 streets, each with 15 fifteen-room houses, with four lights in each room.

In most cases it is unlikely that such fine control would be needed, and a two digit address could be used to identify up to 225 streets, each with fifteen four-room houses; one light per room. As can be seen; the type of zoning required will depend very much upon what you want to do in terms of control.

In addition to zoning, wildcards can be used to specify whole groups of receivers; whenever a command is sent with an address with one or more digits of value zero contained within it, any receiver which matches that address and has any digit value where the sent address digit is zero will obey that command sent. In our 3/two-digit-street/house addressing and zoning scenario described above, sending a command to 3120 will be obeyed by every house on street 12. If public houses were always considered as house '1' no matter where they were within a street, sending a command to 3001 would be obeyed by every public house and nothing else; ideal for turning the lights off in all pubs at closing time !

Four-digit addressing, zoning and wildcard addressing offers a very flexible mechanism for control, but the allocation of receiver module addresses will often require some thought for maximum optimisation and convenient control. Setting the receiver address for each receiver is very much in the hands of the model railway designer and users and there are few absolute guidelines which will be applicable to all layouts. In many cases, it would be appropriate to allocate addresses sequentially to start with, to get the layout workable, and then consider an addressing scheme which suits the final model. One really big advantage of using the PICAXE processors is that it is incredibly easy to re-program the receivers for a new address, so the configuration can change as the layout grows or changes itself.


Receiver Modules

All receiver modules are identical and are based around a PICAXE-08 processor. Each has a single receive input (RX) which connects to the main controller and is used to received command packets, and has four digital outputs which can be used to control external hardware to perform the function it needs to; LED control can be achieved by driving the LED directly from an output pin through a suitable resistor ( usually 330R to 1K8 ).

The design of the receiver is such that each output bit can be set high, low or pulsed individually, as groups or in a variety of combinations.

Circuit Diagram

    5V >----.-----------------------------.----> 5V
            |                             |
            | 100nF          PICAXE-08    |
          __|__             .----.----.   |
          --.--     .---.---| 0V   +V |---'
            |       |   `---| SI   O0 |--------> OUT1
            |       |   .---| D4   A1 |--------> OUT2
    RX >----|-------|---|-->| I3   D2 |--------> OUT3
            |       |   |   `---------'   .----> OUT4
            |       |   `-----------------'
            |       |
    0V >----^-------^--------------------------> 0V

Software

  PICAXERW.BAS

Description


Transmitter Module

Circuit Diagram

Software

  PICAXETW.BAS

Description

The controller is designed to be controlled directly from a PC or from another PICAXE which is scanning a 18-digit keypad ( 0-9, A-F, # and * ) and sending each key as its ASCII character equivalent. All communications with the PC or PICAXE keypad controller is done at 9600 baud, 1 stop bit and no parity.

Command Line Protocol

# Precedes the setting of an address. Once '#' has been sent it must be followed by a four digit address code. When the '#' is entered, the address is reset to 0000, and the command is set to 'no change'.

0-9 If the four digit address code is being entered, the digit is set in the address code. Once the four digit address code has been entered, the number is used to indicate which output bits are to be adjusted in the receiver; 1, 2, 3 and 4 select OUT1, OUT2, OUT3 and OUT4 bits respectively. 0 will select all output bits.

A-F If the four digit address code is being entered, the digit is set in the address code. Once the four digit address code has been entered, the number is used to indicate what action should be performed on the previously selected output bits. 'A' sets the bit, 'B' pulses the bit and 'C' clears the bit.

* Sends the currently specified command to the receivers. If used during entry of the four digit address code, any digits unentered are set to '0' ( wildcards ). This allows settings of large groups quickly; '#*0C*' clears all output bits for the whole network.

Note that there are some special bit setting commands which can follow the four digit address code entry ...

For a three light rail signal controller -

5 Will set the Red light 0001 1110 1S 2C 3C 4C
7/8 Will set the Top Yellow light 0010 1101 1C 2S 3C 4C
9 Will set the Green light 1000 0111 1C 2C 3C 4S

For a four light rail signal controller -

5 Will set the Red light 0001 1110 1S 2C 3C 4C
7 Will set the Top Yellow light 0010 1101 1C 2S 3C 4C
8 Will set both Yellow lights 0110 1001 1C 2S 3S 4C
9 Will set the Green light 1000 0111 1C 2C 3C 4S

For a three light road signal controller -

5 Will set the Red light 0001 1110 1S 2C 3C 4C
6 Will set the Red and Yellow light 0011 1100 1S 2S 3C 4C
7/8 Will set the Yellow light 0010 1101 1C 2S 3C 4C
9 Will set the Green light 1000 0111 1C 2C 3C 4S

For a single points controller -

E Will set the through points 0101 1011 1P 2C 3S 4C
F Will set the turned points 1010 0111 1C 2P 3C 4S

When using a PC to control the controller, the following additional key press characters can be used -

<CR> Carriage Return acts as '*'
S Acts as 'A' Set Bit
P Acts as 'B' Pulse Bit

These characters should only be sent from the PC at appropriate times and as applicable to the command being entered.

LCD Display


Keypad Module

Circuit Diagram

Software

  PICAXEKW.BAS

Description


PICAXE is a trademark of Revolution Education Ltd.





Associated Articles

  The PICAXE Processors
  PICAXE News
  PICAXE Questions & Answers
  PICAXE Comparisons
  PICAXE Pinouts
  PICAXE Serial Interfacing
  PICAXE Infra-Red 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 Saturday the 21st of August, 2004 at 20:16:06
Last upload was on Friday the 3rd of September, 2004 at 01:08:45