Lego Trains

I finally had the courage and inspiration to do something with the Lego Trains project. As you may, or may not, know, Lego trains are controlled by an Infrared controller. The protocol for this has been released by Lego (awesome!) a couple years back and a Mr. Roland Wiersma made a library to control the Lego powerfunction usually send out with this remote.

The remote can control up to 8 trains. Four channels on the “Red” side and four channels on the “Blue” side. This is probably enough for most people, but some people just want to beyond this. The goal is thus to create some sort of decoders (as found in regular model railways) to control the Lego trains. What better than a nice Arduino to feed this? Well, Arduino’s combined with Blynk, even better!

Here’s a drawing of what I thought up in the shower this morning:

(jpeg got a bit big, stupid openoffice, sorry).

Anyway, because we can address many NRF’s using Arduino’s the problem of addressing a maximum of 8 trains has since passed and died a horrible death. This includes the stupid Power Functions controller, which is not that good.

It’s being replaced by Blynk as we see in the next video:

Here’s a picture of the Nano built into the train:

The next phase will be to add some LED’s, make the thing go reverse (with a button next to the slider or something) and get a bigger Arduino cause the libraries and short bit of code already filled up the UNO. Cheers!


Ahaha, that’s just awesome! You are cool! :+1:

1 Like

Ok, added some lights now. They also change with the direction of the train, just like in real life :slight_smile:

1 Like

Nice! :pray:
What’s next?

1 Like

Next thing up is detection of trains and running multiple trains on the same track automatically. I’ve tried reed switches, which work pretty well and LDR’s which are not good. I’ve ordered a color sensor so I can put little colored bricks under my trains and see where it goes from there.

Maybe even automate the trackswitches with little servo’s :slight_smile:


Hi Lichtsignaal,
I like your project, I have been working as you with legos but it takes more time than I really have.
Have you think about using hall sensors? Maybe this way you can control where the train is and move your trackswitches or something similar…

1 Like

Yeah, hall sensors could work, but you still have the problem that you actually don’t know for sure where your trains are. There is no way to differentiate between trains and determine safely if a piece of track is occupied. In regular model railsways they do this by adding a resistor across the two rail bars and effectively create a short-circuit. A controller then knows if the section is occupied but since Lego has plastic tracks…

So, that’s why I ordered the light sensor. It can probably do the best job of identifying a train and seeing where it is. At least, that is what I hope :slight_smile:

After that it’s a matter of making the routing of the trains automatically, electrify switches and make signposts.

It will take a long time, but I’m not alone in my quest, an uncle of mine also faces these issues and he’s really smart, so we’ll figure something out :smiley:


b.t.w., I made an Instructable for it too:


Hi Lichtsignaal,
I have been thinking about your trains just a bit,
what about RFID? Maybe you can add 1 reader each train and insert several cards in the track, this way the train knows where it is, can talk to other trains regarding its position (in order to avoid accidents between them) and you can control the movement… Crazy?

No definitely not crazy! This would be an ultimate type of detection, but there is one big disadvantage I’ve considered and that is the cost. I ordered one RFID reader and card and they were about 15 bucks together. I can order light sensors at 4 dollar each or reed sensors at even lower cost.

If you grow your track and have lots of it and lots of trains you gonna need to invest quite a bit of money, but I agree with you, this would be the best solution from a technical point of view, but I’m not that rich, lol.

That’s a great project!
I was thinking of two different ways to detects your trains positions:

  1. using the infra red barriers: you can detect track occupancy. There’s some work to do at the “system” level to compute the track occupancy based on the in and out barriers detection. But it’s possible.
  2. using the light sensor with bricks on the tracks. This time, the train knows its location, and the “system” can use this information to compute the track occupancy. You can also install two bricks very close, with different colors, so your train knows which direction it is going.

The infrared solution is indeed basically the same as all other methods in that, as you say, it cannot detect which train is where. It may be possible to add an infrared led to a train on the side and have it continuously sent out signals to IR detectors. That could be a possible solution, come to think of that.

If you mean the Lego light sensor, that is not an option as far as I’m concerned because of the costs. But can you elaborate on that?

I was thinking of this board: But I didn’t check the price before, it’s indeed quite expensive…

The IR on side sending to the train could be a great way. You could send a different ID for each IR Led, so the train could know it’s location.

Yep, that is something that just crossed my mind, when you said it. I’m gonna experiment with that soon-ish.

I did found the lightsensors for half that price and I ordered one, just to see if it could work :slight_smile:

1 Like