Project tutorial

Arduino and LEGO® Power Functions © GPL3+

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

  • 8,882 views
  • 0 comments
  • 17 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

Web-Controlled LED Animations with Raspberry Pi and Arduino

Project tutorial by Bruno Portaluri

  • 2,831 views
  • 4 comments
  • 15 respects

Valentine Blink: Wireless LED Hearts

Project showcase by AdiK

  • 1,570 views
  • 13 comments
  • 16 respects

Programming ATtiny13 with Arduino Uno

by Tauno Erik

  • 1,744 views
  • 4 comments
  • 6 respects

Solar Panel Sun Tracker - Phone Charger

Project tutorial by FIELDING

  • 8,109 views
  • 3 comments
  • 37 respects

Bluetooth Controlled Pick and Place Robot

Project tutorial by Ahmed Ebrahem Ahmed

  • 3,736 views
  • 12 comments
  • 22 respects

LEGO Wall-E with Arduino

Project tutorial by monsterbacke

  • 8,793 views
  • 7 comments
  • 53 respects
Add projectSign up / Login