Monthly Archives: December 2015

Lego train revisited

General idea for a new train controller!

The idea is to have each train equiped with the following:

Arduino Nano

NRF204 Wireless transmitter

Infrared Emitter

The IR emitter will be put on the engine part and taped of so signals only get send to the local train. Lights are an option too of course. This way we can equip each train with it’s own unique NRF address and forget the whole Lego Power Functions channels and colors because the IR will only reach the one train.

On the Nano there will the NRF library, I like the RF24 library because of its ease of use, and the Lego Power Functions library.

One Arduino (also with an NRF will be in control of everything via Blynk app and the local Java server so minimum latency is guaranteed. Because the NRF also supports some sort of broadcast-to-all it’s easy to build in emergency stopping functionality, just in case. In the end this will probably be a Mega, but for now an UNO will probably be enough. This one will be connected via Ethernet (ENC28J60 SPI module).

Detection of the trains can be with reed contacts. This probably is the cheapest solution hardware-wise but you need to write a lot of software for that. Currently I’m investigating color-sensors. They have a tiny CCD with just a couple pixels for color detection. The huge advantage of this method is you can label each train at the bottom with unique colored bricks and have separate colors for different types of cars. You can measure length and direction of the train probably really easy:

ICE-LOC <> WAGON1 <> WAGON2 <> ICE-Close

(green) (blue) (blue) (red)


(purple) (yellow) (yellow) (red)

In this example we can differentiate between the freight train and passenger train and the start and end of the train. We can do all sorts of good stuff with this setup. For example have the passenger train get a higher priority, slow down the freight train at the station, read the train length etc.

Anyway, so far for the theory, we should put this in practice now, hopefully I’ll be able to show you some progress soon-ish. 🙂

ENC28J60 stability

In my quest for the exploration of the IOT I came across a piece of hardware for connecting the Arduino to ethernet, namely this thing: ENC28J60. It’s a very cheap alternative for the Arduino shields and works, almost as good as them.

I combined this port with a couple of LDR’s (see my LDR debounce posts) to measure light and graph it in the wonderful app of Blynk which now features history graphing and raw data logging. This is an end to a means of automating lights somehow.

Anyway, the ethernet library I use is the UIPethernet ethernet. This is the most practical in use and works very good with this adapter. However, I do noticed some stability issues. The board it self is based on the SPI protocol and therefor is really easy to attach to your device.

I modified the ENC28J60Network.cpp file according to some source on the internet als follows. There is a procedure in there called “sendPacket()” with the following piece of  code in it:

Somehow that doesn’t do the trick and my ethernet disconnects after a day or two, sometimes sooner. The “Internet” suggested I change this like so:

And that’s it, for now it runs smoothly so I have good hopes it will stay running so I can record my light levels in the living room, normalize them and attach a relay or LED lamp to it!