LittleBits Automated Railroad #bitify

by rbender

Published on December 4, 2014

For my entry in the Bitify Your City contest, I automated a Lego railroad with a crossing gate and station stop since a good rail system is vital to a thriving city.

The crossing gate uses a Long LED and Light Sensor bit to detect when the train approaches a crossing. An Arduino Bit measures the light value from the sensor and when it drops below a threshold, it triggers a Servo Bit to lower the gate and blinks a pair of LEDs on the crossing sign in an alternating pattern. After a couple seconds, the Arduino raises the gate and turns off the LEDs.

I found that the cables for the Long LEDs were too short and a little stuff to work with for the crossing sign, so I made a pair of custom longer LED cables. I found some old wires with the same connector as the Long LED in a bin of parts I salvaged from broken electronics and soldered LEDs to them.

Connecting the Servo Bit to Lego was a bit of a challenge. I found that the mounting holes on the servo 
can clip onto Lego pole pieces to hold it in place. To connect the servo to a Lego gear, I used some thin wire to tie the round horn to the gear, but needed a small washer between them, otherwise the wheel would be slightly crooked and turn unevenly. The servo turns more than 90 degrees, so I had to use a pair of two differently sized gears to get the crossing gate to turn 90 degrees.

The station stop also uses a Long LED and Light Sensor Bit to detect the passing train. When triggered, the Arduino uses an IR LED Bit to stop the train at the station platform for a few seconds and then starts the train back up again. I found an Arduino library online for generating the right IR pulses to control a Lego Power Functions receiver.

My Arduino source code for the project is available online at: 

Credits: Rob Bender

How To Make It


Connect a Power Bit, Long LED Bit and Light Sensor Bit together attached to a Brick Adapter to make the approaching-train sensor. Attach it to a short Lego wall along the tracks and run the LED wire under the track and point it toward the Light Sensor. You may need to raise the tracks a little using some Lego plate pieces so the LED wire can fit under them.


Use a Wire Bit to connect the Light Sensor to the A1 input on an Arduino Bit.


LEDs can partially fit inside the hole of Lego Technic bricks. Run two Long LEDs from Arduino outputs D1 and D5 to the crossing gate. You can place red translucent colored 1x1 round Lego plates at the other end of the Technic brick holes to give the light a red color.


I found the Long LED wire to be too stiff for working into the crossing, so I made my own LED cables using extra scrap wires I had. (If you don't have wires with the same connector as the Long LED wire, you could also use regular wires with the Proto Bit instead)


Replace the horn on the Servo Bit with the circular one. Place a small washer around where the screw sticks out. Then place a Lego "Technic Wedge Belt Wheel" piece ( over the washer and use thin wire or string to tie the wheel piece to the round servo horn. You now have a way to attach various Lego pieces to the Servo Bit!


Use a short Lego Technic axle to attach a small Lego gear to the wheel on the Servo.


To hold the Servo Bit in place, you can clip the screw holders onto a pair of Lego bar/antenna pieces. I then built an electrical-transformer looking box around the Servo to better hide it.


Place the Servo by the railroad crossing so the small gear turns the larger gear.


Attach the Servo Bit to the Arduino output D9. Set the output switch to PWM instead of Analog.


Download the LittleBitsRailroadCrossing sketch from my GitHub repo, open it in the Arduino IDE and upload it to the Arduino.


If you open the Serial Monitor in the Arduino IDE, you will see the values read by the Light Sensor. Try blocking the Light Sensor and see if the crossing gate activates. If not, try tweaking the LIGHT_THRESHOLD value in the Arduino sketch or the sensitivity of the bit until it works properly.


Encase the Arduino Bit in a small Lego shed to hide it. Be sure to make spots for the wires to run out of it.


The Station stop os much easier to construct. First build a second approaching-train sensor as described in Step 1.


Connect it to the A0 input on the second Arduino.


Connect an IR LED Bit to output D5 on the Arduino. Be sure to switch the output type from Analog to PWM.


Download the Lego Power Functions Arduino library from and unzip it into your Arduino libraries folder (See for instructions on installing libraries)


Upload the LittleBitsStation sketch from my GitHub repo to the Arduino.


As with the crossing, the Arduino sketch outputs the light sensor reading to the Serial Monitor. Tweak the LIGHT_THRESHOLD or sensor sensitivity as needed.


Build a station platform for the train and hide the Arduino inside it.


Build a structure to go over the tracks and suspend the IR LED from it so that it points down toward the track.

Related Projects

Android Bit Control

Bit control is a simple way to get ON/OFF control of a cloudBit output.  It uses SMS or text messaging to trigger one of two ...

Party light!

lights up when it's dark for festive fun!