Skip to main content
Add Me To Your Mailing List
HomeBlogsRead Blog

Zog's Blog

Arduino-based touch screen turnout control
By Chris Herzog
Posted on 3/09/2017 7:39 PM
A new project is underway down at the WCMRRA clubhouse and this post provides a few details about what I'm working on and how things are progressing.

Arduinos are cheap; so cheap I don't build 555 timer circuits anymore and just whip out an Arduino Pro or Nano (literally $2-3 on eBay) and just start writing some code.  Want a 60/40 duty cycle operating on a 6 second period?  I can do that in well under 3 minutes with 100% timing accuracy (or more correctly, "good enough for government work") without farting around with R/C math equations (which isn't that bad) but even worse, tolerance "stackup" between resistors and capacitors (caps can run from -20% to +80% which really throws a wrench in your calculations which assume the specified part values.)  It's a pain to tweak your 555 circuit to get exactly what you want, though in it's day, it was a game changer.

Once you whip a few of those up, a fellow gets a hankering to do something else.  Down at the club, we've got a module we "salvaged" from another club and decided to give it some new life.  It's a 6', double track, old NMRA-style (offset mains), two cross overs and an industrial siding (see the previous posts about Arduino LED control and "Swifty's Toxic Waste" to see a few more Arduino crimes on this module; this module is where Swifty and his crew do their day job - poorly I might add from the looks of their facility).

With the availability of cheap LCD touch screens, I thought this would be a cool module to add touch control for the 5 Tortoise-driven turnouts.  Using a readily available 2.8" color LCD touch screen and an Arduino Uno provided the computing, display, and sensor horsepower needed for this project.  Using the supported graphics library for the LCD, I was able to draw up the track configuration and add touch points to the screen for controlling the turnouts.  Since I wanted control on both sides, I anticipated using an Uno / LCD screen combo on each side of the module and would need to connect them together in some way to keep them coordinated.

Here is a quick pic of the one of working modules - green means the turnout is aligned for through traffic and red indicates the diverging route is selected.



For controlling the Tortoises, I figured one of the cheap eBay H-bridge modules might work.  They can easily be controlled from the Arduino and will change the polarity of an independent 12v power supply for the Tortoises all from just a few inputs (2 per Tortoise - more on how that fits into the story in a bit).

A photo of the H-bridge module I used is below - it's a fairly generic China-sourced module from an outfit called "Deek-Robot" which identifies itself as "SHIELD MODEL STEPPER MOTOR SHIELD V1" (yes, it's all in caps on the bottom of the PCB).



Now we get to the problem.  It's not enough to just display the track diagram and respond to touches.  I need to keep a pair of modules (one on each side of the module) in sync, allow control of the switches from either side, and drive the Tortoises themselves.  I also need to coordinate the two display / control modules on the separate sides of the module.  No problem, right?  Arduinos are microcontrollers s their all about I/O pins.  Right?  Right?

Remember when I said the H-bridge module needed 2 inputs per Tortoise?  Here's the truth table for driving the outputs from the inputs (showing only the states for the IN1 and IN2 inputs which drive the A+ and A- outputs; IN3 and IN4 drive B+ and B- the same way):

 
 IN1 
 
IN2  
 
IN3 
 
IN4 
 Tortoise Outputs
  A+   A-   B+  B- 
 L L L L - - - -
H L L L + - - -
L H L L - + - -
H H L L + + - -

"Turns out" (get it?) that since the 4 of the Tortoises are paired into 2 cross overs, they can be controlled together so I really only need to drive 3 distinct sets of turnouts which means instead of 10 I/O pins to drive 5 Tortoises (using 3 Deek-Robot H-bridge modules), I could do it with only 6 pins (and 2 H-bridge modules).  Not so bad; or so it seems.

To keep the two sides of the module in sync as far as showing the same state of the turnouts, they need to communicate.  For that, I'm using the Arduino's serial port to write any state change to the other controller and receive state updates from the other as well.  Each one runs the same code so when one changes state, it makes the change to the local display and sends the new state to it's peer.  That module then changes state and sends a state update back to the originating module and since that new state matches the originators current state, nothing changes and the update cycle is complete.  There is some timer logic in the code to prevent the problem of each updating the other continuously so this has shown itself to be pretty robust.

Back to the I/O pins.  The LCD display uses a lot of I/O; you could even say almost all I/O pins get involved.  Once I looked at driving even a reduced number of Tortoises and then adding in serial send and receive between the modules, I was flat out of pins.  Time for a Plan B.

Plan B was to look at a larger Arduino.  The Mega would have so many pins I wouldn't have a problem and would have pins to spare!  But it did have a problem; a different form factor which meant the LCD I was using didn't fit anymore.  I thought about making an adapter board or a set of jumpers but it seemed like a pretty low value approach; complicated and fragile.  I looked at a different LCD which used an intermediate board to drive it off of a Mega but that made the overall module stack larger and less robust, combined with the fact that the software for that LCD was too twitchy to count on (an entirely new interface for drawing and getting touch screen input which I would have adapted to if it only worked which it didn't).  So in the end, Plan B was a crushing bust.

What I needed was a Plan C.  Problem was I didn't immediately have one.  After thinking about it, I realized I didn't really need the ability to "turn off" the Tortoises since they were a stall motor design (they can stay "on" forever wedged or "stalled" at one end of their travel).  All I really needed was a way to invert the polarity going to a specific set of Tortoises so they would change direction.  This might be my way out.  If I could take a single I/O pin, connect it to one of the Deek-Robot H-bridge module inputs, invert it and connect that inverted signal to the other paired input, it looked like I could get what I wanted.  I bread boarded it up and it looked promising (I knew it would, it was so simple that it couldn't go wrong really).

OK, so where will this inverter live?  Ta-da!  The "Zog's Gadgets Tortoise Driver for Deek Stepper Motor Shield V1" was born.  Use a simple 7404-style hex inverter to do the grunt work and I was in like Flynn (Rick Flynn that is, our long time club treasurer and all around good guy).

Below is the schematic, PCB layout (a 2 count panelized layout to save some Hamiltons), and a photo of the finished and assembled board (Express PCB to the rescue again - schematic and PCB files available for download in the Tech Library section of the web site).







This gives me the ability to use 4 I/O pins to drive 4 H-bridge modules (I actually only need 3 pins for this project), get 5VDC from the Arduino, and distribute 5VDC power to each the H-bridges for their on-board logic (the 12VDC for the Tortoises is supplied separately using the screw terminal connector on the H-bridge module).  I used .1" spacing single row headers with MTA-style IDC connectors to be able to make the interconnection harnesses using 22ga stranded wire.  Should be pretty cool.



Will it work?  Testing of the gadget seems to do what I expected and wanted so the next step is to get some dimensions for a panel for mounting this whole operation under the module in question and start to wire things up.

The Arduino / LCD modules will mount to the fascia but I plan to make a single panel that will include all of the other parts needed to bring this to life:

  • 5VDC power supply for the Arduinos
  • 12VDC power supply for the Tortoises
  • The "gadget"
  • A pair of H-bridge modules
  • A cup holder for a beverage of your choice (OK, we don't need that; it'll be under the module anyway but it would be cool)

Stay tuned for a wrap up post soon ("soon" in model railroader terms means anything from 1 week to 6 months depending on how distracted I get...)