Arduino and LEGO Train


Introduction: 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

LEGO pieces

  • For the sensors mount
  • For the servo mount

Arduino and other components

Step 2: 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

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

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

Step 4: 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



    • Casting Contest

      Casting Contest
    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest

    We have a be nice policy.
    Please be positive and constructive.




    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.

    1 reply

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

    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>

    int fwdSpeed[] = {PWM_FLT, PWM_FWD1, PWM_FWD2, PWM_FWD3, PWM_FWD4, PWM_FWD5, PWM_FWD6, PWM_FWD7};

    int revSpeed[] = {PWM_FLT, PWM_REV1, PWM_REV2, PWM_REV3, PWM_REV4, PWM_REV5, PWM_REV6, PWM_REV7};

    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);



    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?

    1 reply

    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.

    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?

    1 reply

    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.

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

    5 replies

    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);

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

    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.

    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?

    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.

    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?

    5 replies

    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.

    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 :)


    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)

    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?

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