A while ago my dad and I were discussing the best ways to control a small model train. As a seasoned model train enthusiast, he was looking for the best way to control a single track train. As an additional requirement, he wanted to show the trains’ speed on a small display … Challenge accepted!
My dad thought me the foundation of my electronics knowledge. But whenever I start to talk about micro controllers, OLEDs and H-bridges, he usually mumbles that I tend to make things overcomplicated, and grabs a simple voltage regulator…
Time to backup my suggestions with a cool working demo!
Immediately after I got home, I grabbed a breadboard and started prototyping. The speed controller consists of 4 elements:
- A 128x64 pixel I2C OLED screen for the visual feedback.
- A Rotary encoder with integrated switch to control the speed.
- A SN754410 H-bridge motor driver.
- And of course a Arduino Nano as the brains of this project.
Connecting the OLED screen is pretty straight forward: simply connect it to the 5V & GND supplied by the Arduino, connect SDA to the Arduino’s A4 pin and connect CLK to the Arduino’s A5 pin.
Connecting the Encoder
The rotary encoder with integrated switch needs a bit of help of passive components to debounce the signals. Without these passives, the interaction is a bit unpredictable.
Signal A and B of the encoder are connected to D2 and D3 of the Arduino via an 1K resistor. It’s important to use these two inputs, since these inputs have Interrupt support on the Arduino Nano. The switch part of the Encoder is connected to D4, also via an 1K resistor. D2, D3 and D4 are all connected to ground using an 0.1uF capacitor. These capacitors and the resistors make sure the signal is debounced. Note that this will only work if the internal pull-ups of the Arduino are enabled.
Connecting the H-bridge.
The SN754410 allows me to control the train’s motor using PWM (Pulse Width Modulation). The SN754410 supports two motors. And even though my dad only needs to control one train at this point, it can’t hurt to wireup the second half of this H-bridge as well. The SN754410 is capable of 1A continuous output current. More than enough for the 50mA train motor.
Even though the SN754410 has some internal diodes, Texas Instruments suggest you incorporate your own flyback diodes to prevent your circuit from voltage spikes due to the inductive load.
Both motors need 3 Arduino Pins. 1 enable pin and 2 PWM pins: one for each direction. Make sure you use the right pins on your Arduino, since not all pins support PWM.
Last but not least, the SN754410 needs two power inputs: +5V from the Arduino to power the logic, and a separate supply (VIN) between 4V5 and 36V to power the motors. Since the trains run on 10V, I’ll be using 10V as the VIN. Conveniently this 10V can also power the Arduino by connecting it to the Arduino’s VIN pin.
Just to be on the safe side: I’ve added a large electrolytic capacitor on the VIN line to prevent power drops when the motor kicks in. (Not visible on the schematics above, but visible on the end result.)
And with all the schematics worked out, it’s time to work on the final product for my dad. Don’t we all love nights like these?
With the help of a 3D printed mounting bracket, a nice label and some fancy connectors, it really is a nice way to make my point: Microcontrollers, OLEDs and H-bridges are freaking cool!
Of course, I still need to work on some fancy code. But if you’re curious for my undocumented bug prone beta code, make sure to check out the code on GitHub.
If you just want to see it working, check out the following video:
Now, let’s hope my Dad will agree that this is the best way to control his trains. Hopefully you’ll see this controller on his site in the near future.