Project tutorial

Arduino and LEGO® Power Functions © GPL3+

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

  • 10,957 views
  • 0 comments
  • 19 respects

Components and supplies

About this project

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);
  if (digitalRead(stationPin) == 0)
  {
   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);
  if (digitalRead(stationPin) == 0)
  {
   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

Comments

Similar projects you might like

Take Control Over Lego Power Functions

Project tutorial by Patrick Müller

  • 41,587 views
  • 2 comments
  • 57 respects

BTBox – LEGO Power Functions Bluetooth Controller

Project showcase by PlastiBots.com

  • 471 views
  • 0 comments
  • 1 respect

Simple Arduino Based Lego Power Function Receiver

Project tutorial by Arduino_Scuola

  • 3,419 views
  • 1 comment
  • 4 respects

Lego® Power Functions IR Sender For Micro:bit

Project tutorial by Philipp Henkel

  • 3,794 views
  • 1 comment
  • 15 respects

Solar Power Unit for Low Power Devices (Reinvented)

Project showcase by Patel Darshil

  • 1,199 views
  • 0 comments
  • 12 respects

Lego Compatible Name Display

Project tutorial by Lunchbox Electronics

  • 1,250 views
  • 0 comments
  • 6 respects
Add projectSign up / Login