Introduction: Arduino and LEGO Train

Picture of Arduino and LEGO Train

In the last year participated in a workshop and needed to submit a project. As I was learning to work with the Arduino and I love Lego. I decided to present a project that involved both.

The project consists having an Arduino controlling a LEGO train. The project objectives were:

  • The train had to stop at the station.
  • When is out of station had to sound a whistle.
  • After leaving the station, the train had to be transferred to the interior line.
  • When fulfilled two laps in the indoor line was again transferred to the outside line

Step 1: What You Need

Picture of What You Need

LEGO pieces

  • For the sensors mount
  • For the servo mount

Arduino and other components

Step 2: Assembling the Servo

Picture of Assembling the Servo

To assembly the sensors stand I needed the following Lego pieces:

  • 2x6 plates
  • 2x8 plates
  • 2x4 plates
  • 2x4 bricks
  • 2x3 bricks
  • 1x4 bricks
  • 1x2 bricks

Step 3: Assembling the Sensors

Picture of Assembling the Sensors

To assembly the sensors stand I needed the following Lego pieces:

  • 2x6 plates
  • 2x4 plates
  • 1x4 plates

Step 4: Arduino Connections

Picture of Arduino Connections

The list of connections is this:

Arduino pins:

  • Pin 2 - Connection to station Sensor
  • Pin 10 - Servo
  • Pin 11 - Buzzer
  • Pin 12 - Connection to lap Sensor
  • Pin 13 - IR
  • 5V Pin - Connection to the breadboard to power the sensors and servo.
  • Ground Pin - Connection to the breadboard to power the sensors and servo.

The sensors have three pins. One pin is ground, other is VCC and the third is the signal. If the sensors are blocked, they return 0. If not return 1.

The servo has also three pins.One ground, other VCC and the third is control.

The buzzer has two connections. One is ground and the other connects to the Arduino Pin. Between the Arduino and buzzer connection its needed the 220 kohms resistor.

because the size of the train track, the pin 13 is used by two IR leds. They are mounted in the station, pointing for both sides.

Step 5: Arduino Sketch

This is the Arduino sketch used to control the sensors, servos and train.

It's avaliable at my github repos: Github Repo

/** Arduino and LEGO Power Functions
* Tiago Santos
* <a href="" rel="nofollow">

* Free to share
#include <Servo.h>
#include <legopowerfunctions.h>

Servo servoMain; // Define our Servo
int curSpeed = 0;
// IR led on port 13
LEGOPowerFunctions lego(13);
int setSpeed = 3;
int irPin = 12;
int count = 0;
int buttonState=0;
int fwdRev=0;
int stationPin = 2;
int buzzerPin = 11; 
//irPin - Lap Sensor
//stationpin -Station Sensor
//buzzerpin - Buzzer pin</p><p>void setup()
  pinMode(irPin, INPUT);
  servoMain.attach(10); // servo on digital pin 10
void loop()
  if ( digitalRead(irPin) == 0 ) 
  if (count <= 1)
      Serial.print("Estacao: ");
      if(setSpeed>7) setSpeed=7;
      if (fwdRev==0)
      lego.SingleOutput( PWM, PWM_FWD5, RED, CH1);
      if (digitalRead(stationPin) == 0)
        lego.SingleOutput( PWM, PWM_FLT, RED, CH1);
        servoMain.write(0);  // Turn Servo Left to 45 degrees
    servoMain.write(90);  // Turn Servo Left to 45 degrees
    count = 0; 

Step 6: Final Result

And the final result is this.

Step 7: Lego Library Explained


I add a link to the Lego Power Functions tutorial that i use.

PDF tutorial


Ken76 (author)2017-04-09

Thanks for your effort! May I ask where I can download the same legopowerfunction, which you have used in this project? Perhaps I am using slightly different version.

TiagoSantos (author)Ken762017-04-10

Hi. I have the library on my GitHub Account (step 5). You can download from there.

Ken76 (author)2017-04-06

Ok, I appreciate this.

TiagoSantos (author)Ken762017-04-09

Hi. I've try with two trains and one led and works well. One starts, then stops and starts the second. I use this code:

#include <legopowerfunctions.h>



LEGOPowerFunctions lego(13);

void setup() {


void loop() {

lego.SingleOutput( PWM, PWM_FWD2, RED, CH1);


lego.SingleOutput( PWM, PWM_FLT, RED, CH1);


lego.SingleOutput( PWM, PWM_FWD2, BLUE, CH1);


lego.SingleOutput( PWM, PWM_FLT, BLUE, CH1);



Ken76 (author)2017-04-05

It didn't help to change channel. I also tried to set RED and BLUE value for both trains, but that didn't either help. Is there something else to try?

TiagoSantos (author)Ken762017-04-06

Hi. I can only try on the weekend i can try with my trains to see a solution. I'll see if there is another solution.

Ken76 (author)2017-04-03

I am trying to remote 2 lego trains with arduino mega 2560 and lego power function library. The problem is that when the first train stop, it should not move again when to second train start moving after same delay.

Here is the code

if (detected == LOW){

lego.SingleOutput(0,PWM_FLT,RED,CH1): // first train


lego.SingleOutput(0,PWM_REV5,BLUE,CH1); // second train


Is it possible to remote properly two trains with same ir led or should I have an different ir led for each train?

TiagoSantos (author)Ken762017-04-04

Hi I still didn't test with two trains but one led should be enough. Try changing the channel of one train. It can be a problem with RED and BLUE parameters.

karthishashi (author)2016-12-20

how can I control lego train using arduino?. Can you please tell the steps

TiagoSantos (author)karthishashi2016-12-21

Hi. You need an arduino, an ir led and the LegoPowerFunctions library (see step 7). The command to control the Lego Train is: "lego.SingleOutput( PWM, PWM_FWD5, RED, CH1);" Before that you need to inicialize the library:

LEGOPowerFunctions lego(13);

karthishashi (author)TiagoSantos2016-12-21

Thank you so much. But, with lego power functions is it possible to control it using arduino?

TiagoSantos (author)karthishashi2016-12-22

Yes. Instead of using the Lego PowerFunctions Remote, you need an Arduino and a IR led. The ir led send the right commands to the Power Functions Receiver.

karthishashi (author)TiagoSantos2016-12-23

Dear Sir, thank you so much for your replies. Our budget is very low, so is it possible to do the same project with other cheap train models unlike LEGO? Can you please help us as this is a part of my college curriculam?

TiagoSantos (author)karthishashi2016-12-27

Hello. The other trains work in multiple ways. The Lego trains have a standart. The other don't. You need to choose one model/brand and see how it works. I can't help because i only work with Lego trains.

HeryxK (author)2016-06-30

Nice Project, one question. how do the train communicate with Adruino? Wire or Wireless? look like the train is an individual object in your system. how it follow the code in Adruino?

TiagoSantos (author)HeryxK2016-06-30

Hello. The train doen't communicate with the arduino. The arduino has an IR led that send the orders to the train. The orders are received by the Lego Power Function Receiver in the train but there isn't a comunnication train->arduino.
The version 2 that i'm working on has an arduino by train and a master and there is a bidireccional communication link.

HeryxK (author)TiagoSantos2016-07-07

I see, another question is, are you build the "LEGOPowerFunctions" library on your own? and below two code is from the same library?

1. LEGOPowerFunctions lego(13)

2. lego.SingleOutput

the last question is.. why i can't see any word after #include in the beginning of your code? that should be some word after that to include .h file.(etc #include XXX.h) i am new to play around arduino, thanks for answer my question :)

TiagoSantos (author)HeryxK2016-07-07


Was missing the include library code. Resolved.

I'm using a library from Roland Wiersma ( I haven't build the library.

The two lines are from the same library. First line creates a LEGOPowerFunctions object working on pin 13. The second line defines the working mode. ("lego" is the object name)

HeryxK (author)TiagoSantos2016-07-08

Hi, I am trying to understand and need to know how to use the "legopowerfunctions" library, may i know any document can be a good example or reference?

TiagoSantos (author)HeryxK2016-07-08

Hi. I've added the tutorial that i use to the last step.

Ken76 (author)2016-03-31

What kind of IR Led did use? I will get a try today with TSAL6200 IR LEDs

TiagoSantos (author)Ken762016-03-31

I use this leds:

I think they are the same if they have the same waveleght.

Ken76 (author)2016-03-31

Should an resistor be placed between Arduino and IR Led? Everything else is now clear to me.

TiagoSantos (author)Ken762016-03-31

Hello. No i didn't use any. The only resistor is between Arduino and the buzzer.

Ken76 (author)2016-03-28

What kind of sensors and IR leds did you used with this project?

TiagoSantos (author)Ken762016-03-28

Hello. The IR leds are 3mm 940nm waveleght. The sensors are from this arduino kit:

Fred1599 (author)2015-12-13

How did you get that train piece that has the intersecting rails?

TiagoSantos (author)Fred15992015-12-13

Hello. I get it on Bricklink. It's the number 7996-1 (complete set) or two 60128 (parts).

Fred1599 (author)TiagoSantos2015-12-13

Thanks! I should shop there!

tomatoskins (author)2015-04-11

This is cool! Nothing better than automating a train set! So great!

TiagoSantos (author)tomatoskins2015-04-11

Hi. Thanks

About This Instructable




More by TiagoSantos:Halloween CheerlightsLightbox With Controller and StorageAquarium Controller Using NodeMCU and MQTT
Add instructable to: