A model train controller using pulse-width modulation for speed control and an H-bridge output to control direction
Following a discussion on the ngrm-online forum, I decided to look at modifying my picAXE-based pwm train controller so that the picAXE controls the direction as well as the speed. This would dispense with the mechanical double-pole double-throw (dpdt) switch.
To do this requires what is called an "H-bridge" driver; basically four power devices with the motor between pairs of them. The current through the motor (and hence its direction) is controlled by which two of the power devices are on at any one time.
I decided that the 8-pin picAXE that I used for the original controller could still be used because the two pins normally used for programming can be used by a program for input and output.
I also found a rotary encoder that incorporated a push-switch (for emergency stop and direction control) and three LEDs, which shine out through the transparent control shaft and translucent knob. I thought this would make a nice minimalist-look to the control box.
I used the AXE091 breadboard to develop the circuit; and what an advantage that was, as I found the performance of this particular rotary encoder was quite different from the one I had use in the Mk3 controller. I went through a few development cycles before finding a hardware and software solution that worked reliably.
This is the circuit diagram for the working controller. It has a few extra bits from the original design idea. The three LEDs and the push-switch are all integrated into the rotary encoder and they are all commoned to the positive line. Pressing down on the knob activates the push-switch.
The extra resistor and capacitor connected to each encoder output act as low-pass filters to counter any "chatter" from the switch contacts. I have included a 7404 hex inverter IC to drive the three LEDs and to reshape the pulses from the switches. There are two 10k resistors grounding C.0 and C.1, to prevent the H-bridge shorting the power supply while the picAXE goes through its initialisation at power-up.
In use, the encoder governs speed only, following convention that clockwise increases the speed and counter-clockwise decreases the speed.
If the button is pressed while the train is in motion, the PWM output is stopped, but the direction is saved, so the train will carry on in the same direction when the knob is rotated clockwise.
If the button is pressed while the train is stationary, then the direction is reversed. The colour shining out of the encoder shaft indicates the direction and speed, as we have three colours (red, green and blue) to play with.
Some modellers like to have control over direction as well as speed using the control knob; there are a number of controllers available with a "centre-off" control knob (indicated by a detent). You can't have a centre-off on a rotary encoder, of course, but it is quite possible to program this controller to behave in this way. I would think a programmed "dead-spot" at zero-speed would do the trick. Perhaps I'll leave that as an exercise for the reader!
More details on my web site, including the BASIC program.
Following a discussion on the ngrm-online forum, I decided to look at modifying my picAXE-based pwm train controller so that the picAXE controls the direction as well as the speed. This would dispense with the mechanical double-pole double-throw (dpdt) switch.
To do this requires what is called an "H-bridge" driver; basically four power devices with the motor between pairs of them. The current through the motor (and hence its direction) is controlled by which two of the power devices are on at any one time.
I decided that the 8-pin picAXE that I used for the original controller could still be used because the two pins normally used for programming can be used by a program for input and output.
I also found a rotary encoder that incorporated a push-switch (for emergency stop and direction control) and three LEDs, which shine out through the transparent control shaft and translucent knob. I thought this would make a nice minimalist-look to the control box.
I used the AXE091 breadboard to develop the circuit; and what an advantage that was, as I found the performance of this particular rotary encoder was quite different from the one I had use in the Mk3 controller. I went through a few development cycles before finding a hardware and software solution that worked reliably.
This is the circuit diagram for the working controller. It has a few extra bits from the original design idea. The three LEDs and the push-switch are all integrated into the rotary encoder and they are all commoned to the positive line. Pressing down on the knob activates the push-switch.
The extra resistor and capacitor connected to each encoder output act as low-pass filters to counter any "chatter" from the switch contacts. I have included a 7404 hex inverter IC to drive the three LEDs and to reshape the pulses from the switches. There are two 10k resistors grounding C.0 and C.1, to prevent the H-bridge shorting the power supply while the picAXE goes through its initialisation at power-up.
In use, the encoder governs speed only, following convention that clockwise increases the speed and counter-clockwise decreases the speed.
If the button is pressed while the train is in motion, the PWM output is stopped, but the direction is saved, so the train will carry on in the same direction when the knob is rotated clockwise.
If the button is pressed while the train is stationary, then the direction is reversed. The colour shining out of the encoder shaft indicates the direction and speed, as we have three colours (red, green and blue) to play with.
Some modellers like to have control over direction as well as speed using the control knob; there are a number of controllers available with a "centre-off" control knob (indicated by a detent). You can't have a centre-off on a rotary encoder, of course, but it is quite possible to program this controller to behave in this way. I would think a programmed "dead-spot" at zero-speed would do the trick. Perhaps I'll leave that as an exercise for the reader!
More details on my web site, including the BASIC program.
Last edited: