Project tutorial

# Arduino and LEGO® Power Functions © GPL3+

Project for controlling a LEGO Train with Arduino and LEGO® Power Functions.

• 20,170 views
• 26 respects

## Components and supplies

 Arduino UNO
×1

On june 10 i attended a workshop about electronics and computer (http://ei.aicb.pt) with a project bases on LEGO Power Functions and Arduino.

The project involves a LEGO Train that was controller by an Arduino. The goals were:

• The train must stop at the station every time that passes.
• When starting from the station a whistle as to sound
• After leaving the station, the train must change to the inside track
• After accomplish two laps on the outside track, the train must be transferred to the outside track

### Used Material:

To build the project, beside the LEGO blocks and train, was used:

• UTP cable (to make connections between components)
• One 220kohm resistor

To control the train at the station were needed two IR leds due to the range and angle of these. One controls the station entry and the other the station exit. To control the position of the train were used two obstacle sensors. One controls the number of laps on the inside track and the other controls the station entry. One difficulty that arises was the train control. LEGO offers the code to interact with their Power Functions (more info here) and there are four ways to interact with the train. Initially i used the Combo PWM but the train stops when exits the IR led range. After some help from the Portuguese lego Community (Comunidade0937) change the functions to the Single Output and the problem was solved.

### Code:

This is the code used. It’s also available on my GitHub account.

``````/*
* Arduino and LEGO Power Functions
* dark_storm@groundzero.com.pt
*/
#include <Servo.h>
#include <legopowerfunctions.h>
Servo servoMain; // Define our Servo
int curSpeed = 0;
// IR led on port 13
LEGOPowerFunctions lego(13);
int irPin = 12;
int count = 0;
int buttonState=0;
int fwdRev=0;
int stationPin = 2;
int buzzerPin = 11;
//irPin - conta as voltas
//count - variavel que conta as voltas
//stationpin - para o comboio na estacao
//buzzerpin - apito de saida de estacao
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)
{
lego.SingleOutput( PWM, PWM_FWD5, RED, CH1);
delay(100);
{
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
count = 0;
}
}
``````

The obstacle sensors return ‘1’ there isn’t an object in range and ‘0’ if there is.

### Final result:

The final result is this:

The onboard view:

The servo that changes the track:

### Update:

Final edited version:

The code is also available on my GitHub account (https://github.com/d4rks70rm/ArduinoLegoTrain). The tutorial that i used to configure the LEGO Power Functions is this -> Link.

## Code

##### Code snippet #1Plain text
```/*
* Arduino and LEGO Power Functions
* dark_storm@groundzero.com.pt
*/
#include <Servo.h>
#include <legopowerfunctions.h>
Servo servoMain; // Define our Servo
int curSpeed = 0;
// IR led on port 13
LEGOPowerFunctions lego(13);
int irPin = 12;
int count = 0;
int buttonState=0;
int fwdRev=0;
int stationPin = 2;
int buzzerPin = 11;
//irPin - conta as voltas
//count - variavel que conta as voltas
//stationpin - para o comboio na estacao
//buzzerpin - apito de saida de estacao
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)
{
lego.SingleOutput( PWM, PWM_FWD5, RED, CH1);
delay(100);
{
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
count = 0;
}
}
```
##### Github
https://github.com/d4rks70rm/ArduinoLegoTrain

• 1 project
• 5 followers

#### Published on

December 21, 2016

#### Members who respect this project

and 20 others

See similar projects
you might like

#### Take Control Over Lego Power Functions

Project tutorial by Patrick Müller

• 61,390 views
• 76 respects

#### Simple Arduino Based Lego Power Function Receiver

Project tutorial by Arduino_Scuola

• 6,497 views
• 11 respects

#### Lego Compatible Name Display

Project tutorial by Lunchbox Electronics

• 3,330 views
• 6 respects

#### Autobot Using Lego NXT Motors and Sensor

Project tutorial by Mark Tashiro

• 19,523 views
• 27 respects

#### Lego Stepperbot

Project tutorial by Fredrik Stridsman

• 3,865 views