Layout Final

Arduino and LEGO® Power Functions

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
Arduino and LEGO® Power Functions
Layout Final

Used Material:

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

Arduino and LEGO® Power Functions
Arduino and buzzer

 

Arduino and LEGO® Power Functions
Servo

 

Arduino and LEGO® Power Functions
Sensor de obstaculos

 

Arduino and LEGO® Power Functions
Led’s IR

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 ( Comunidade 0937 ) 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 accout ( https://github.com/d4rks70rm/ArduinoLegoTrain ).
The tutorial that i used to configure the LEGO Power Functions is this -> Link

Leave a Reply

Your email address will not be published. Required fields are marked *