The Arduino Z-Scale Trolley Project.

Curn Jul 17, 2023

  1. Curn

    Curn TrainBoard Member

    752
    500
    32
    IMG_7338.jpg

    This is a project I started because Showcase Miniatures made a $35 trolley kit that goes over a $20 shorty mechanism. I didn’t have too much fondness for trolleys, or any desire to build another tiny layout. Yet, the Showcase kit just looks good, and the shorty chassis runs well around very tight curves. What seems like a cheap and simple locomotive project quickly grew into an electronics and programming project. As I figured my coding might be of some use to the rest of you, I figured I would share my results here. It’s actually pretty easy to build an Arduino DC train controller which can also serve as a fun way to learn about writing Adruino projects and coding. The ultimate goal here is to build a tiny 6” x 24” point to point layout that runs continuously on my desk.

    The start of the project was this YouTube video that describes the basic setup for N scale Kato trolleys.


    Rather than build the setup described with a separate motor driver, I figured I would use an L298 Arduino motor shield. After all, many of us already have these as they are needed for DCC++/DCC++ EX and don’t require a rats nest of wires. The advantages of the Arduino motor shield, is it has two analog inputs on the top of the shield with 5 V and ground pins allowing for building it without a breadboard, it has current sensing, and it just plugs into an Arduino. A version is also available for the Arduino Nano allowing for a very Z sized setup. Also, because the Rokuhan shorty runs on 3 volts, you can run the whole thing just using the 5 volts from USB. Two volts are lost on the L298p chip and a little under 3 volts max gets to the rails.


    The N scale code needed slight modification to change the digital pins used to those used by the Arduino Motor Shield, but otherwise that projects works and is elegant in how simple it is. The only notable issue was, a fast moving shorty can fly by IR sensor in the 200 ms refresh window, so that also had to be modified to a shorter interval. Then I wanted to see if I could add things and thats where things got more complicated.

    The use of the “delay” function throughout the code stops all other commands form being executed during the delay. So getting rid of the delay functions was needed to add the other features. I ended up completely re-writing the code. And I’m not a programmer so this took a bit of work for me. I’m sure someone experienced in C++ would laugh at how I went about doing certain things. But after much trial and error, I think I have something workable. It is vastly more complicated than where it started. But now I have an Arduino sketch that runs well with added functions for current/overload protection, acceleration and deceleration (rather than simple on/off) changing the PWM frequency depending on the locomotive speed, and adds more conditions to the logic to prevent errors (Like not allowing station 2 to be triggered while the train is in station 1).

    And after playing with the code and electronics, I realized this would also be useful for anyone wanting to build a any point to point DC layout, someone with a few T-Trak modules who wants to run trains back and forth without a full loop, or even automating a switch back with slight modifications of the code for individual use. You could even use this on a loop layout with 1 station to have a train stop at the station. Z scale has many DC users as our trains are so small.

    So what you will need

    * Arduino Uno or Nano. I’m currently using a Nano which I will cover later.
    (A Mega would need slight modification as pin D3’s PWM is on Timer 3 not Timer2)

    * Arduino L298p or L298n motor shield or equivalent like the Deeks Robot shield. That Alice account on the bay sells Nano shilds.

    * IR sensors with 3 pins ( I used some from HiLetGO)

    * A 10 ohm potentiometer

    * A 470 ohm resistor

    * Various wires to connect it all. Give strong consideration to a servo wire/jst crimp kit where you can make your own 3 pin wires.

    You of course will need to download/install the Arduino IDE, which also implies you need a computer.


    This is how you wire it all:
    ArduinoUnoPointToPoint.png
    The Code:

    You can get the code I wrote from here:
    https://github.com/Wonruc/ZScalePoint-to-Point

    There is alot of copyright/legalize in there because it is open source and free to use, You have to go out of your way to say that up front. Do with it as you want.
     
    Last edited: Jul 17, 2023
    ztrack, MMR283, HoboTim and 5 others like this.
  2. Curn

    Curn TrainBoard Member

    752
    500
    32
    Here is a short video of the thing running back and forth.
     
    Zscaleplanet, ztrack, MMR283 and 6 others like this.
  3. mdvholland

    mdvholland TrainBoard Member

    532
    891
    30
    Thanks for sharing, Curn. The little streetcar is a hit, I believe. Your version is looking very good, anyways.
    You make it seem easy to let it run this way. Arduino is totally new for me, but my trolleys are underway (I hope) and someday I hope to find the time to get acquinted with arduino and put it to use like you did.

    Matt
     
    Curn likes this.
  4. MIHS85

    MIHS85 TrainBoard Member

    47
    166
    9
    Very interesting, thanks for sharing.

    And a fantastic job on the trolley’s paintwork and decals!

    Cheers,

    Chris
     
    Curn likes this.
  5. Zscaleplanet

    Zscaleplanet TrainBoard Supporter

    677
    1,612
    37
    WOW!!! Very cool, and very educational !!! Nice work on everything.
    Indeed, I need to learn what the ARDUINO's are and what they do, how to program, etc., etc.
    Heck, I need a basic electronics class while I'm at it -- :ROFLMAO::LOL::ROFLMAO:
     
    Curn likes this.
  6. Curn

    Curn TrainBoard Member

    752
    500
    32
    Arduinos have alot of neat and practical uses. If anyone is playing with DCC, I highly recommend the DCC++ EX project to get started. The only real edit to the code you need to do is to configure the WiFi connection. https://dcc-ex.com/

    What I really thought was slick was how Rob Ray asked a chatbot to write a Arduino program to control all the lights in his model hotel, and it reduced his multipage code down to a few paragraphs. So even having an ability to write code isn't exactly a prerequisite.

    I'm using the Arduino here because there isn't alot of support for auto-reverse circuits out there, and likely none that will work with a shorty. Most of the ones out there use current detection.... Darn you for making me think. The motor shield has two current sense channels and I can set any threshold needed to make it work. I could make a version that relies completely on the current sensors and doesn't use any IR sensors at all.. Just need some rail isolating joiners and 3 power leads.

    I might have a new version of this soon because hiding the IR sensors was going to be hard. They are relatively large in comparison to the trains.

    This was the plan, but I'm going to try to make it work with current sensing detection first. After all, I already have the detection code in there for short protection.
    IMG_6992.jpg
     
    mdvholland, MMR283 and Kez like this.
  7. SJ Z-man

    SJ Z-man TrainBoard Member

    3,018
    1,027
    62
    I built this Shorty Mini-Case just for this purpose. I was going to use Hall-effect sensors (magnet/metal sensing) at many locations to create a stop and go (wait for Signal on 1st loop) then again for the crossover, then outer loop Station stop. Green light reverses and eventually parks back on the spur.

    Now, where has all my time gone ;(

    Mini-Attache for Shorty locos_3.jpg
     
  8. Curn

    Curn TrainBoard Member

    752
    500
    32
    Well I worked on a setup to use the current sensors to detect stations and, well, it would work well for the MTL F7. The setup used the A and B motor shield channels to power the tracks with the B channel to power the station tracks in a B-A-B configuration with two stations. Any time current in the B tracks was detected, the trolley would stop, the polarity of the track was reversed, and the train would leave. The issue is the shorty draws so low of current that you can barely tell it’s there. Even a TheZMaker switcher with a resister is too low of a current. There is so much noise in the current sensors that you get background reads in the 1 to 2 mA with occasional/rare spikes at 3 mA. Well a shorty cruising only reads at 2-3 mA. The solution is to try to take a bunch of readings to differentiate noise from a real signal, which causes a random delay from when it enters a station. In many cases, it wouldn’t trigger until the trolley hit the track bumpers, causing a readable rise in the current draw from pushing against an immovable object.

    Where as the IR sensors work very reliably, so I’m going to stick with them for now. I only have two technical issues with them. Sunlight can trigger them, and they don’t see black steam locomotives. You can adjust their sensitivity to deal with sunlight. As the sensors will be mounted under the track looking up, you can paint a silver patch on the bottom of equipment to reflect the IR light to solve any detection issue.

    I haven’t thought of a Hall effect sensor. I might order some and test. I suspect this code could be adapted to you purpose very easily. You basically just delete the direction change command so that it keeps going in the same direction.
     
  9. animek

    animek TrainBoard Member

    713
    109
    27
    That is a very nice project.

    Questions:
    A) Is your turnout connected to your assembly? Or did you activate it by hand?
    B) Where did you connect your Nano on your L298?

    Thanks.
     
  10. Curn

    Curn TrainBoard Member

    752
    500
    32
    Animek,

    The turnout was controlled by the arduino using the B channel of the motor shield. Currently all the turnout control code is in the get hub sketch, but it is commented out, so all you should need to do to test is uncomment the station 3 code. I may split the code into 3 versions for ease of use by others, because there are 3 common configurations that might be used: linear multi station, switchback, and loop each being slightly different.

    The Nano controller.

    I went with a nano because this is Z, and I wanted to stick this under a wood frame that has limited height. So I wanted one controller that was as short as possible. For this you are going to need the L298P Nano motor shield. I got mine from "Alice" seller on ebay. To build my setup I also got some 1/4 solderable breadboards which I cut down. These bread boards have power rails, and are 15 pins wide, just like the Nano!

    I used all this stuff:
    IMG_7233.jpg

    The bottom of the nano motor shield has connector pins, so I wanted to bend those over and connect all the sensors to those pins. I wanted to just use pre-made servo wires that are male-female. The IR sensors pins are signal-ground-VCC(5V), so I was going to build the analog input connection headers in the same way. I cut down the bread board so that it just had the power rails and two additional rows of solder points. I recommend tin snips to cut the PCB. I then took header blocks and bent the last pins out to bridge the gap:
    IMG_7234.jpg

    Then I placed those to line up with where the A2 through A7 pins would go.
    IMG_7240.jpg

    Then I soldered everything together, connecting the ground and 5V to the power rails of the bread board.
    IMG_7242.jpg

    IMG_7243.jpg

    And it all made for a nice and clean compact controller where the wires and connectors all come in from the side to allow for installation where height is an issue. As you can see I soldered the pins for the Nano board from the top so that it would be a few mm shorter. The whole thing is 0.65" in height.

    IMG_7245.jpg

    -Matt
     
    Last edited: Jul 19, 2023
    Zscaleplanet likes this.
  11. SJ Z-man

    SJ Z-man TrainBoard Member

    3,018
    1,027
    62
    Thats a HUGE PWM motor Shield. ! Can’t you just 2-pin (o,1) the code PWM direct to a 298 H-bridge?
     
  12. Curn

    Curn TrainBoard Member

    752
    500
    32
    So that is what is done in the original video at the top, and yeah you can with the downside being the separate bridge units don't have built in current sensors and the heat sink on most of them is 1 inch tall. They do have the mini versions that are quite small.

    The current draw on the shorty is so low, you could probably just tie a few of the digital pins together and run the shorty directly with the PWM pins from the Arduino (The spec is 40 mA peak, 20 mA continuous per pin) But then any short would run the risk of frying chip.

    The advantage of the motor shields is current sensing, and it is plug and play. The Uno version could be built without any soldering. I'm using this for a shorty, but there is no reason someone couldn't use this for HO scale by just increasing the current limit. Break the VIN trace on the motor shield to the Arduino and you can go up to 35 volts and 4 amps max which will run any model train out there. The current sense prevents your trucks from melting if the locomotive picks the turnout points and protects the hardware. So it is nice to have. But in an absolutely point to point track plan, a short would be unlikely.
     
    Last edited: Jul 19, 2023
  13. SJ Z-man

    SJ Z-man TrainBoard Member

    3,018
    1,027
    62
    For just point-to-point, Dalle makes automation products, for a price
     
    Kurt Moose likes this.
  14. Curn

    Curn TrainBoard Member

    752
    500
    32
  15. animek

    animek TrainBoard Member

    713
    109
    27
    Thanks for answering my first question Matt.
    Regarding my second question, I've reformulated it with the help of your pics. Still trying to figure out how the Nano connects to your motors shields (now two).

    Thanks

    Ben.

    [​IMG]
     
    Zscaleplanet likes this.
  16. Curn

    Curn TrainBoard Member

    752
    500
    32
    Ben,

    I think the confusion is that these two system are not connected. They were just shown together as a size comparison. See the image below:

    Regarding the Nano motor shield, it is a Deek-Robot model 8106 Nano Motor Shield, mostly sold under the confusing name of "Nano motor shield" on ebay and aliexpress. The nano shield uses the same pins as the larger Uno version.

    Motor Pins.PNG

    There is a US maker of this shield called Gravitech. They are currently sold out.
    https://gravitech-llc.myshopify.com...or-w-feedback-control-add-on-for-arduino-nano

    These nano shields are described on the DCC++ EX site:
    https://dcc-ex.com/reference/hardware/microcontrollers/nano.html

    SetupMC.jpg

    Hope this clears this up.
     
    Last edited: Jul 20, 2023
    Zscaleplanet likes this.
  17. animek

    animek TrainBoard Member

    713
    109
    27
    aaahhhh! Thank you, all clear now, thanks
     
  18. Curn

    Curn TrainBoard Member

    752
    500
    32
    Some updates.

    I have made some progress on the layout. One, I decided to stick with a point to point track plan, and not use the turnout. The main reason is this is going to be a small desk decoration, and hearing the turnout switch constantly would eventually drive me to madness. So I came up a railroad to nowhere with this:
    Track Plan Trolley.png

    I used Woodland Scenic foam and risers to create a small hill to climb between the two points. A total of 1/2 inch in elevation change.
    IMG_7625.jpg

    For the IR sensors, I wanted to try using smaller IR LEDs to see how small I could get things. I couldn't find a readily available source for 1.8mm LEDs, but I could get 3 mm ones. I also wanted to mount the LEDs off the back of the sensor board so that when installed, the adjustment dial would be facing down for easy adjustment.

    IMG_7470.jpg

    I did some testing with the 3 mm LEDs and they work the same as the stock 5 mm LEDs, so there wasn't any benefit or handicap to switching to them as far as function goes. One thing I found was that trying to shove both the LEDs between the rails does not work. The light bleed between the LEDs was so high you could not adjust the sensitivity enough to work. placing the LEDs between ties works with no issues.

    IMG_7473.jpg


    So I made a few sensor tacks by drilling two 3 mm holes in the Rokuhan track and the LEDs are held in place by friction alone until I have all the scenery done.

    IMG_7521.jpg

    The other thing I did was modify my nano setup to have headers for the extra digital inputs (D2, D4, D5, D6, D7, & D10), done in about the same way I did the analog ones. I ran the red wire (5V) from the analog side. I'm planning on moving the IR sensors to D5-D6 (Which are also on the Uno motor shield), to free up the analog inputs as A4 and A5 which have I2C signal generation functionality to be used for an optional OLED display. So now I need to figure out how to program an OLED display.

    IMG_7622.jpg

    -Matt
     
    Last edited: Aug 9, 2023
    CNE1899, JoeTodd and Zscaleplanet like this.
  19. Kurt Moose

    Kurt Moose TrainBoard Member

    9,860
    14,346
    147
    Wow, cool!

    Is this still a 6"x24"?
     
  20. rray

    rray Staff Member

    8,312
    9,463
    133
    Looks good. They do have 0805 SMT IR Detectors if you wanted smaller. Also, you can sand the T1 (3mm) package down to super small where the metal cup is actually exposed, and it will still work. One of my tricks is to sand a component down super small, placing it, then gluing it in place with UV resin which re-encapsulated the device as well as glues it in place.
     
    Zscaleplanet likes this.

Share This Page