Model train controller uses PIC and a full-bridge motor driver IC
This model railway train controller is based on a PICAXE18M2 microcontroller and an ST Micro L6203 full-bridge motor driver IC. Its features include speed regulation, simulated inertia (momentum), reversing, infrared remote control, over-current protection, derailment warning and low-speed starting.
It also uses high-frequency PWM (pulse width modulation) control for quiet motor operation.
It is designed for HO/OO gauge locomotives such as those made by Athearn. These have 5-pole, skew-wound motors with twin flywheels. It should work with similar motors as long as they have flywheels and sufficient back-EMF.
The L6203 contains four N-channel Mosfets in a full bridge configuration, ie, one high-side driver and one low-side driver for each rail. It runs off 12-48V and can pass up to 4A RMS. It’s controlled via three logic-level inputs: a low/high input for each half-bridge plus a shared enable pin which, if driven low, turns off all four Mosfets. These can be pulsed at frequencies in excess of 100kHz.
The L6203 also contains cross-conduction prevention logic which prevents current “shoot-through” when switching. This is similar to the “dead-time” feature in some switching circuits. It also has a 13.5V reference voltage generator (not used in this circuit) and over-temperature shut-down. And it has separate power and logic grounds, to enable low-side current sensing with an external shunt resistor.
When the ENABLE (pin 11) and IN1 (pins 5) inputs are high and IN2 (pin 7) is low, OUT1 (pin 3) is pulled high (to Vs, pin 2) and OUT2 is pulled low (to SENSE, pin 10). This runs the motor in one direction. If IN1 is low and IN2 is high, OUT1 goes low and OUT2 goes high, running the motor in the opposite direction.
If IN1 and IN2 are in the same state (low or high), both outputs are at the same voltage and the motor brakes. If ENABLE is taken low, both tracks are disconnected from the supplies and the motor coasts. By “chopping” the IN1 and IN2 signals at a specific frequency (15kHz in this circuit), the average voltage applied to the motor can be varied and thereby the loco’s speed.
The two 10nF capacitors connected between each output and the corresponding BOOTS (bootstrap) pin are used by the internal charge pump to generate the voltage necessary to turn the high-side Mosfets on.
While IC2 contains internal ground clamp diodes for each output, these are relatively slow and this results in higher dissipation in the chip. So Schottky diodes D3 & D4 are connected in parallel with the internal diodes and their lower forward voltage and fast switching makes the circuit more efficient while providing more effective clamping.
A snubber, comprising a 22nF capacitor and 10Ω series resistor is connected between the outputs. This limits the rate of change in the voltage across the rails, reducing electromagnetic interference (EMI) and cleaning up the back-EMF waveform. The back-EMF spikes on each rail are averaged using a pair of 10kΩ resistors, so that a positive spike on either rail can be sensed.
This is then low-pass filtered using a 10nF capacitor which has a parallel 22kΩ resistor to discharge it (also reducing the sensed back-EMF amplitude). Schottky diode D5 and the 10nF capacitor and 10MΩ resistor at its cathode form a peak-hold circuit. A positive back-EMF spike forward-biases D5 and charges the capacitor which then discharges through its parallel resistor. Micro IC1 can read its amplitude at pin 11 (input B.5) using its internal analog-to-digital converter (ADC).
Back-EMF feedback is used for speed regulation. The micro adjusts the motor drive duty cycle to keep the back-EMF amplitude constant for a given speed setting. It controls the bridge through output pins C.6, B.3 and B.6 (pins 15, 9 & 12 respectively) which connect to the ENABLE, IN1 and IN2 inputs of IC2.
To improve the linearity of the back-EMF measurement, the micro holds NPN transistor Q1 on for most of the time, driven by output B.4 (pin 10). This keeps the 10nF capacitor at IC1’s pin 11 discharged. When the micro wants to measure the back-EMF it turns Q1 off, allowing the capacitor to charge for a certain period before sampling the voltage across it. This improves measurement linearity.
An over-current condition is de-
tected by measuring the voltage across the 0.1Ω resistor from IC2’s SENSE terminal (pin 10) to ground, using the same ADC. All current flowing across the tracks also flows through this resistor. A low-pass RC filter (10kΩ and 100nF) suppresses voltage spikes and if the measurement exceeds 0.1V (>1A motor current), power is cut, the piezo buzzer sounds and the two LEDs flash. Moving the speed control to neutral resets it.
This buzzer is driven directly from the C.7 output of IC1 (pin 16) while the LEDs are driven from outputs B.0 and B.1 (pins 6 & 7), with 470Ω series resistors limiting the LED current to around 6mA. Normally, these LEDs indicate the current motor speed.
The circuit is controlled by potentiometer VR1 and/or infrared remote control via receiver IRD1. VR1 selects between five different states: full speed reverse, minimum (shunting) speed reverse, stopped (neutral), minimum speed forward and full speed forward. When its position is changed, the loco’s speed is ramped. If changing direction, it briefly stops.
Trimpots VR2, VR3 & VR4 allow these speeds to be set. VR2 configures the duty cycle for minimum speed while VR3 selects maximum speed. However when changing from stopped to minimum speed, initially the duty cycle is ramped up until the motor starts to turn. As soon as this occurs, the duty cycle is then reduced to a percentage as set by VR4 and then slowly changes to that set for minimum speed. So VR4 is set to prevent too much of an initial lurch while still allowing reliable starting.
Power is from a 15-18V DC supply which should be capable of at least 1A. Diode D1 (3A) provides supply reverse polarity protection while two 2200µF capacitors provide bulk bypassing for the motor. IC2 also has two smaller bypass capacitors for its own supply.
5V for the micro is provided by a 78L05 linear regulator which is isolated from the motor supply by diode D2 and has further input bypass and output filter capacitors. The supply for IRD1 is filtered by an RC low-pass filter (47Ω/47µF) to remove any switching noise as it contains a high-gain amplifier which can be sensitive to supply noise.
Because the L6203 is being run well below its current rating, it doesn’t need a large heatsink and should stay cool.
For remote control, a Sony TV protocol is used. The volume up and down buttons select the direction while the channel up/down buttons switch the speed. Mute is equivalent to moving the control knob to the neutral position. When a valid command is received, the piezo buzzer chirps. If the remote control is not used, replace IRD1 with a 10kΩ pull-up resistor to 5V on pin 4 of IC1.
The software (train controller.bas) and user notes are available for download from the SILICON CHIP website. Finally, the L6203 bridge IC can be purchased from Element14 (au.element14.com) or Futurlec (www.futurlec.com.au). It is also available from DigiKey and Mouser.