Background

A demonstration was given using a PIC Microcontroller simulator that showed how a train travelling around a track anti-clockwise could be guided onto an inner track via a set of points.  The position of the train was determined by a proximity sensor.  Logic was written in the PIC to control the points based on the detected position of the train.  The logic worked as follows:-

  • Direct a single train anti-clockwise around an outer track five times.
  • Direct a single train anti-clockwise around an inner track five times.

Brief

To prove that the PIC microcontroller demonstration can be realised on a real track, using real hardware and trains.

A PDF of this build log can be downloaded here.

Components

Item

#

Description

Hardboard

1

SUNDEALA works well for this (Dimensions 3’ x 6’)

2” x 1” 3m wood

AR

Make an enclosed HH frame to add strength to the board

Wood Screws

AR

Wood screws to fix the support braces to the SUNDEALA

00 Gauge Track

AR

Enough to make a double loop

IR Proximity Sensors

3

 

Hornby Point Motors

2

R8014 - Point Motor and R8015 - Point Motor Housing (Adaptor Base)

CDU

1

 

PIC IO Dev Board

1

 

Power Jack (2.1mm)

1

Maplin Order Code: HH60Q

PIC16F628A

2

 

Red Wire

10m

Maplin Order Code: FA33L

Blue Wire

10m

Maplin Order Code: FA27E

Black Wire

10m

Maplin Order Code: FA26D

12VDC  Regulator Board

1

Item

Quantity

Maplin Part #

TS7812CZ Regulator

1

Order Code: QL32K

100nF Polyester Capacitor

1

Order Code: N43CJ

100uF Electrolytic Capacitor

1

Order Code: KQ70M

4700uF Electrolytic Capacitor

1

Order Code: AT24B

1N4001S Diode

1

Order Code: QL73Q

1K0 1/8W Resistor

1

Order Code: M1K

5mm Red LED

1

Order Code: WL27E

Two terminal PCB mount block

6

Order Code: RH80B

 

Train controller

1

Hornby R8012 HM 2000 Dual Power Controller

Table 1: Bill of materials

 

Part One - Hardware

Building the board (120m)

Firstly construct an enclosed “H” support brace around the perimeter of the board, with two braces that run left to right across the board.  It is important to place the braces such that the 1 inch side it screwed to the board and the 2 inch side is at right angles to the board.  This both allows strength and provides protection to the control electronics.  Cut two holes in each of the braces to allow cables to pass through.

Laying the track (30m)

Once the braces have been fitted the track can be laid.  The exact position of the track is not important, only that there is an inner loop and an outer loop.

Fitting the Electronics (180m)

The electronics can now be fitted to the board in three parts.  The first and most difficult part is fitting the IRDOT-2D proximity sensors to the board underneath the tracks.

Proximity Sensors (60m)

Tools required: 4mm wood drill bit, 4mm circular file, 10mm rectangular file.

Using the 4mm drill bit, drill a hole from the top of the board between two of the sleepers on the track as a guide (Be careful at this stage to make sure that the sleeper is not damaged by the drill)

Remove the excess wood splinters from the underside of the board and place the circular file through the hole to clean and clear it.

Gently drag the circular file left in the circular hole to make it rectangular and large enough to put the 10mm rectangular file in.

Using the rectangular file widen the hole so that it is the width of the gap between the sleepers and no more than 10mm long (it should look like a letter box).

Remove the excess splinters from the underside of the board.

Place the IRDOT-2D below the rectangular letter box hole such that the IR transmitter and received slot into the hole but do not stick up above the sleeper (it may be necessary to file the hole slights to get a good fit).

Once the PCB is in place measure the distance between it and the underside of the board to enable spacers to be selected (and cut if required).

Secure the PCB to the track board using the required spacers ensuring that the holes required to hold the spacer to the track board do not foul the track.

Repeat for the other two IRDOT-2D sensors.

Control Circuits (15m)

Tools Required: Philips Screw Driver, Pliers, M3 bolts (3cm long), M3 nuts.

The position of the PIC IO-Board, CDU and 12VDC Regulator board is not critical and should be mounted close to the Proximity sensors, but avoiding where the Point Motors will go.

Note: The PIC-IO Board, CDU and 12VDC Regulator board should be mounted directly to the underside of the track board WITHOUT spacers.  This firstly gives more clearance of the high capacitor components and secondly ensures the board does not flex during wiring.

Note: In order to aid program de-bugging do not obscure the RS232 port on the PIC IO Board.

Point Motors (15m)

Tools Required: Philips Screw Driver, Phillips countersunk screws (supplied).

Fit the two point motors on the top of the track board next to the points ensuring they are mounted level and the control levers are not fouled.  Place a three connection terminal block next to the point motors to allow them to be easily removed and drill a small hole next to the terminal blocks for the wires to route to the underside of the board.

Wiring (90m)

Tools Required: Philips Screw Driver, Slot Screw driver, terminal blocks.

Wire the board as per the schematic (Appendix B), which was created using Eagle Lite Edition.  There are no specific rules to wiring and what should be connected first, just try to stick to the following guides:-

  • Use Red for positive or Point Motor Direction A.
  • Use Black for negative or Point Motor Common.
  • Use Blue for signal or Point Motor Direction B.

Note: Remember to check the wiring before power-up and use duck tape to secure loose cables to the underside of the board.

Power-up (5m)

Place the programmed PIC16F628A (Appendix A) into the PIC IO Board, and then connect the power.  The RB5 Status LED should instantly light for five second and each of the IR Proximity sensors LEDs should also light up.  After approximately four seconds the sensor lights should begin to switch off followed by RB5.  The board will then pulse each relay output in turn to test the Point Motors.  The board is ready for use.

Note: If any of the above does not happen remove power and double check the wiring.

Part Two - Software

Layout and Design

The software design was built using Dia and is shown in the figure below.

Development

The control software has been written in Microchip C using the MPLAB IDE and is modularised into components:-

main.c – This file contains the main track control algorithms.

track.c – This file contains the routines to control the board.

usart.c – This file contains the routines to communicate via the RS232 port.

track.h – This file contains the function headers and constant defines for track.c.

usart.h – This file contains the function headers and constant defines for usart.c.

The compile source code from the above five files is included in Appendix A as hex code ready for programming into a PIC16F628A using an appropriate programmer.

The software was programmed to the PIC16F628A using the Microchip PIC Start Plus as this is what the Author already had.

This is however a legacy product and Microchip recommend using the PICkit 2 Starter Kit instead.

Testing

The software has been tested using a PIC16 Simulator prior to being installed in the test track.  The majority of the testing has been done on the Test Track board with the RS232 debug output connected to a laptop via an RS232 to USB converter running HyperTerminal under Windows XP Home SP3.

 Part Three – Future Ideas

Diode Point switching

By using diodes connected to the drive relays and re-wiring the common from the Point Motors it would be possible to use three relays to drive two points rather than four relays to drive two points.  This would then free up the fourth relay to control something else.

Auxiliary control

Once Diode point switching has been put in place, the fourth relay could be used to switch signals, power to track sidings, turn table motors, special effects units, etc to enhance realism of the layout.