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
- 1 arduino
- 1 buzzer
- 2 obstacle sensors
- 1 servo
- 2 IR Led's
- jumper cables
- UTP cable
- 1 breadboard
- 1 220kohms resistor.
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 * dark_storm@groundzero.com.pt * <a href="http://space.groundzero.com.pt" rel="nofollow"> http://space.groundzero.com.pt </a> * Free to share */ #include <Servo.h> #include <legopowerfunctions.h> Servo servoMain; // Define our Servo 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}; 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() { Serial.begin(9600); pinMode(irPin, INPUT); pinMode(stationPin,INPUT); servoMain.attach(10); // servo on digital pin 10 pinMode(buzzerPin,OUTPUT); } void loop() { if ( digitalRead(irPin) == 0 ) { count++; delay(3000); } if (count <= 1) { Serial.print("Estacao: "); Serial.println(digitalRead(stationPin)) Serial.print("contador:"); Serial.println(count); if(setSpeed>7) setSpeed=7; if (fwdRev==0) { curSpeed=fwdSpeed[setSpeed]; } lego.SingleOutput( PWM, PWM_FWD5, RED, CH1); delay(100); if (digitalRead(stationPin) == 0) { Serial.print("teste"); delay(1000); Serial.print("teste2"); lego.SingleOutput( PWM, PWM_FLT, RED, CH1); servoMain.write(0); // Turn Servo Left to 45 degrees delay(2000); tone(11,2000,1000); delay(500); } } else { servoMain.write(90); // Turn Servo Left to 45 degrees Serial.print("contador:"); Serial.println(count); count = 0; } }
Step 6: Final Result
And the final result is this.
Step 7: Lego Library Explained

Participated in the
Automation Contest
42 Comments
1 year ago on Step 1
Do you have some instructies about the led signals by arduino just like this one.
I have All my servomotor switches by bluetooth and now i need some advice about my signals light
Question 3 years ago on Step 2
Hello.
I'm trying, for a long time, to connect the SG90 servo to the train track switch.
Still can't understand how to connect the servo 'handle' to the yellow handle of the switch so it will be moved both sides.
Many thanks for a great work!
Answer 3 years ago
Hello. I didn't connect the servo with the yellow handle physically. I place a Lego Technic Bush (https://www.bricklink.com/v2/catalog/catalogitem.page?P=3713#T=C) on the yellow handle pin to make it larger and the servo push it. You can see it in detail in this video https://www.youtube.com/watch?v=_1XNC5YFI90
The housing for the servo is make with bricks and plates.
Question 3 years ago
Something elementary I don't know how to acheive:
How do I connect (physically) the SG90 servo to the yellow handle of the switch?
I know there are guys who used 3D printed parts to mound the servo and to connect its horn to a kind of technic part, but I'm trying to connect it without 3d printing.
Thanks for that!
Answer 3 years ago
Hello. I put a Lego Technic bush (https://www.bricklink.com/v2/catalog/catalogitem.page?P=3713#T=C) on the handle to make it bigger. Then the servo pushes it when it turns. Check this video https://www.youtube.com/watch?v=_1XNC5YFI90
Question 4 years ago on Step 3
Hi Sri,
will you please share arduino connection diagram, i want to build this for my child.
thanks for your help
Answer 4 years ago
Hello. I don't have a connection diagram. On the Instructable is the information where the data pin of each sensor connects to the arduino. All the others are ground and vcc.
Reply 4 years ago
Thank you Sir
Answer 4 years ago
Thank you Sir
Question 4 years ago on Step 5
hi Sir,
i am getting error as unexpected unqualified-id before '{' token
{
Serial.begin(9600);
pinMode(irPin, INPUT);
pinMode(stationPin,INPUT);
servoMain.attach(10); // servo on digital pin 10
pinMode(buzzerPin,OUTPUT);
}
please advise
Answer 4 years ago
Hello . Use the GitHub file instead of the code on the Instructable (the link is near the code). On the GitHub Repositorie is a file with all the code and the library needed.
6 years ago
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.
Reply 6 years ago
Hi. I have the library on my GitHub Account (step 5). You can download from there.
6 years ago
Ok, I appreciate this.
Reply 6 years ago
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);
delay(1000);
lego.SingleOutput( PWM, PWM_FLT, RED, CH1);
delay(1000);
lego.SingleOutput( PWM, PWM_FWD2, BLUE, CH1);
delay(1000);
lego.SingleOutput( PWM, PWM_FLT, BLUE, CH1);
delay(1000);
}
6 years ago
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?
Reply 6 years ago
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.
6 years ago
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
delay(2000);
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?
Reply 6 years ago
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.
6 years ago
how can I control lego train using arduino?. Can you please tell the steps