Project tutorial

Arduino and LEGO® Power Functions © GPL3+

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

  • 7,524 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 accout ( 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

Pac-Man LED Pixel Panel Costume

Project tutorial by Ben Muller

  • 4,296 views
  • 3 comments
  • 76 respects

LoRa Gateway for DeviceHive

Project tutorial by DeviceHive IoT team

  • 1,051 views
  • 2 comments
  • 16 respects

IoT Bird Feeder with Sigfox and Tweeter

Project showcase by Gaël Porté

  • 313 views
  • 0 comments
  • 7 respects

SmartWay

Project tutorial by Universum

  • 163 views
  • 0 comments
  • 4 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 17,507 views
  • 6 comments
  • 44 respects

Arduino-Based Automatic Guitar Tuner

Project tutorial by Ben Overzat

  • 2,875 views
  • 0 comments
  • 10 respects
Add projectSign up / Login