Richfilth

Controlling 9V trains with Arduino

Recommended Posts

I recently got my first Arduino, and I'm in the process of automating some tracks. This vid is just a proof-of-concept, showing that two tracks can be controlled, with a motorised switch, and some light- and ultrasound sensors.

https://drive.google.com/file/d/0B8Bxk35bJ4FPZHJNZ2pLY1JiTVU/view?usp=sharing

At the double junction, whichever train arrives first has automatic priority, and the second train will wait (about 2 seconds) for the junction to clear before moving on.It would have been a lot easier to give one track priority (i.e the train never stops), but I like a challenge :grin:

Is anyone interested in some How To videos?

Edited by Richfilth

Share this post


Link to post
Share on other sites

i always wanted to buy an Arduino to control all of my trains.. but I didnt have the courage to do it as I have zero knowledge in programming. Just checking if is hard to do programming?

Share this post


Link to post
Share on other sites

At Christmas 2014 I had zero programming experience. This video was filmed in early February, six weeks later. So no, not hard at all :D The Arduino forums are very supportive.

The only modification to the lego that I had to make (oh no, sacrelige!) was the power cable. I had some old Technic cables with perished wires, so I removed the plugs and connected them to fresh cables - so one Technic cable makes two Arduino-to-Lego cables.

Share this post


Link to post
Share on other sites

This is great work. I am very interested in the solutions you used. I would like to use arduino for some automation also. What shield did you use? And any more how to's would be awesome.

Thanks

Share this post


Link to post
Share on other sites

This configuration was relatively easy. I used:

1 x Arduino Mega

2 x L298N motor controllers (each module can control 2 motors)

2 x light sensors

2 x ultrasound sensors

3 x homemade 9V cables (described in the post above)

1 x 12V 1.5A power brick

The only difficult part is the wiring. I spent a few evenings with some pliers and crimp connectors to make enough cables, but connecting everything to the Arduino is very simple. I don't use any shields - I just used a breadboard to "share" the 5V and ground wires for all the sensors. I didn't have to play around with any resistors, capacitors or other electronic stuff - it's really just plug-and-play.

Share this post


Link to post
Share on other sites

2 x L298N motor controllers (each module can control 2 motors)

2 x light sensors

2 x ultrasound sensors

That is really cool. Which sensor is for what? You listed a light sensor and an ultrasound sensor. Your detection of the train is perfect (and the shutoff is quick!)

I've done a trolley setup with an arduino uno that used reed switches at both sides. The first couple versions would have failures on the reed switches. I moved them 90 degrees and since then, I've had only 2 failures in about 50 hours of use.

Did you get a shield or breakout for the L298N?

Edited by coreyg

Share this post


Link to post
Share on other sites

This is the L298N I use. It really is just 4 wires between it and the arduino (or 7, if you control two motors):

ibch7xnAhDDkoT.jpg

The two light sensors are the glowing lights by the crosstrack (housed in red and green bricks respectively):

ydsuzx1371627762804.jpg

One 5V wire, one ground wire, one sensor wire. Really simple.

And the Ultrasound sensors are at the end of each siding. They detect the reversing train, and when it is 25cm away the Arduino cuts power to the track, switches the points, then powers up in the opposite direction to bring the new train onto the loop:

sensor_pins.jpg

Again, a live, a ground, and two sensor wires this time.

I'm not an Arduino expert so I can't offer detailed advice on how to code these things (maybe the Almighty Arjen can help here :sweet: ), but I am currently putting together some guides on how to get started with Arduino and 9V lego. So far I've been able to convert my setup to remote control, so I can control train direction and speed, plus track switches.... with my hifi remote :laugh:

Share this post


Link to post
Share on other sites

This is some great information, thank you for sharing, do you use LED's with the light sensors to make them more reactive? if so any particular LED's?

Share this post


Link to post
Share on other sites

I have nothing to add :wink: Only a tip: I use optical sensors which give a 0 when activated. That whay you can use the sensors very easily for interrupts so your arduino doesn't have to wait for the signal, but can do some other stuff in the meantime (but maybe interrupts are something for a bit advanced programmers).

I was also writing a little how-to for the beginners among us. I'm planning to put it on my website, but I have to upload the whole website first, since it's still in developer mode :laugh:

But once you know how to control a track with an L298, the limitation is only learning by doing and a bit of imagination :)

Share this post


Link to post
Share on other sites

I have nothing to add :wink: Only a tip: I use optical sensors which give a 0 when activated. That whay you can use the sensors very easily for interrupts so your arduino doesn't have to wait for the signal, but can do some other stuff in the meantime (but maybe interrupts are something for a bit advanced programmers).

I was also writing a little how-to for the beginners among us. I'm planning to put it on my website, but I have to upload the whole website first, since it's still in developer mode :laugh:

But once you know how to control a track with an L298, the limitation is only learning by doing and a bit of imagination :)

Do the optical sensors have to be adjusted for lighted rooms vs darker rooms or have anything specific on the "other side of the track" so it doesn't see something in the distance?

For my train layout, I'm not using any sensors. I have 4 lines of track power and up to 16 lines for controlling switches. I've attached a bluetooth module (HC-05) to the arduino pro mini and I'm using DRV8835 motor controllers for the power and multiple shift registers for the on/off input of the switches (through the DRV8835s). The pro mini only has 6 PWM lines so I was limited on how many variable controlled lines I could have. I also wrote a simple android app that can control the speed/direction of the train as well as the switches. Here's my previous version (the current one is still in development):

16055094041_7e9595d691.jpg

This version is using the DRV8835 in a mode that causes damage to the 9v motors. The new version I'm working on now will fix that. The main issue is if the h-bridge is configured in a Forward/brake,Reverse/brake or a Forward/Coast,Reverse/Coast mode. It seems the "Coast" mode is required unless you want to put undo stress on the motors.

I have tons of plans to add stuff (detectors, sounds, lights, etc) to this layout, but the issue with damaging the motors had slowed my progress down a lot.

Edited by coreyg

Share this post


Link to post
Share on other sites

My light sensors need to be adjusted to the ambient light conditions via the screw on the resistor, but you can do this via code to take an "average" reading you want to. It doesn't need any other light source, but obviously they won't work in a dark room.

As for interference, they can be very sensitive (walking around is enough to trigger them with your shadow), so I'm switching IR sensors similar to Arjen's design. I'm trying to make them fit inside a 1x3 stud area, but this involves soldering which isn't plug-and-play like I stated in my first post :wink:

Share this post


Link to post
Share on other sites

Thanks for posting this. I have very simple needs, I don't have a complicated layout... I'd just like to do something like a train doing it's loop some random number of times and stopping occasionally at a station; I'd also like to play out train sounds. I'm not sure if I want to try RaspberryPi or Aduino; I've seen some cool mini-LCD displays (like train schedules) with RaspberryPi.

In any event, I'm barely electronics literate, but these things are inexpensive enough to experiment with, so thanks for posting again - it's the kind of push someone like me needs to actually try it out instead of thinking about it.

Share this post


Link to post
Share on other sites

is there a driver motor module which can control 12 motors? also which Arduino board I should get which can cater so many control output?

Share this post


Link to post
Share on other sites

is there a driver motor module which can control 12 motors? also which Arduino board I should get which can cater so many control output?

I think the easiest way is to use multiple L298 modules. Each module can drive two separate motors. (That's the way I do it) If you want to control 12 motors all seperately without using a multiplexer, you must assume 3 I/O pins per motor:

- 1x EN (with PWM control for speed regulation)

- 2x direction control

That means 36 I/O pins, so you would need an Arduino Mega for that (also to have enough PWM-outputs)

Share this post


Link to post
Share on other sites

As Arjen says, you need a PWM pin and two digital pins on your Arduino for every motor you want to control. The Arduino Mega has 15 PWM pins and 48 digital pins (some of these are shared), so you can run 12 motors if you don't really want to do anything else (like sensor reading).

Fred, what you want to do is extremely simple and I can give you code if/when you buy an Arduino, as this was one of my "learning" projects. You only need one sensor (light, IR, reed switch - doesn't matter) and a motor controller. I also have a 16x2 LCD screen set up, but you can also use the old Nokia phone screens.

Share this post


Link to post
Share on other sites

great video!! makes me more confident to jump into Arduino.. Would you be able to do another video on your sensor input?

Share this post


Link to post
Share on other sites

Thanks! The next videos will be:

1) Introduction to sensors (stopping at stations and using counters)

2) Motorising your switchtracks

3) Distance detection via ultrasonic for sidings control

4) LCD info displays

5) Controlling your train with an IR remote

6) Crash avoidance with junction sensors

7) Switching train programs with a Finite State Machine

8) The automated decoupler (still a WIP on the lego side of things)

9) The Arduino Marshalling yard for mega train management.

So that will be 10 videos in all, but it takes time to record all the video :sweet:

Share this post


Link to post
Share on other sites

so looking forward to all of these videos!!

I second the motion! I'll be watching with great interest, as I want to incorporate this type of control to my 9volt layout. :thumbup:

Share this post


Link to post
Share on other sites

Here's the next video in the series: an introduction to Sensors, featuring an auto-stop setup with 5-second wait time:

Edited by Richfilth

Share this post


Link to post
Share on other sites

@Richfilth, nice video, quick stop => reverse power for a 1/2 second

@Fred67, simple => kemo relay card M162, 8 port relay card, usb, contacts max 25 V=, 3 A (it's Deutsch but clear English explain)

Edited by Q3671

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.