Introduction: Arduino and LEGO Train

In the last year participated in a workshop and needed to submit a project. As I was learning to work with the Arduino and I love Lego. I decided to present a project that involved both.

The project consists having an Arduino controlling a LEGO train. The project objectives were:

  • The train had to stop at the station.
  • When is out of station had to sound a whistle.
  • After leaving the station, the train had to be transferred to the interior line.
  • When fulfilled two laps in the indoor line was again transferred to the outside line

Step 1: What You Need

LEGO pieces

  • For the sensors mount
  • For the servo mount

Arduino and other components

Step 2: Assembling the Servo

To assembly the sensors stand I needed the following Lego pieces:

  • 2x6 plates
  • 2x8 plates
  • 2x4 plates
  • 2x4 bricks
  • 2x3 bricks
  • 1x4 bricks
  • 1x2 bricks

Step 3: Assembling the Sensors

To assembly the sensors stand I needed the following Lego pieces:

  • 2x6 plates
  • 2x4 plates
  • 1x4 plates

Step 4: Arduino Connections

The list of connections is this:

Arduino pins:

  • Pin 2 - Connection to station Sensor
  • Pin 10 - Servo
  • Pin 11 - Buzzer
  • Pin 12 - Connection to lap Sensor
  • Pin 13 - IR
  • 5V Pin - Connection to the breadboard to power the sensors and servo.
  • Ground Pin - Connection to the breadboard to power the sensors and servo.

The sensors have three pins. One pin is ground, other is VCC and the third is the signal. If the sensors are blocked, they return 0. If not return 1.

The servo has also three pins.One ground, other VCC and the third is control.

The buzzer has two connections. One is ground and the other connects to the Arduino Pin. Between the Arduino and buzzer connection its needed the 220 kohms resistor.

because the size of the train track, the pin 13 is used by two IR leds. They are mounted in the station, pointing for both sides.

Step 5: Arduino Sketch

This is the Arduino sketch used to control the sensors, servos and train.

It's avaliable at my github repos: Github Repo

/** Arduino and LEGO Power Functions
* Tiago Santos
* dark_storm@groundzero.com.pt
* <a href="http://space.groundzero.com.pt" rel="nofollow">  http://space.groundzero.com.pt

</a>
* Free to share
*/
#include <Servo.h>
#include <legopowerfunctions.h>

Servo servoMain; // Define our Servo
int fwdSpeed[] = {PWM_FLT, PWM_FWD1, PWM_FWD2, PWM_FWD3, PWM_FWD4, PWM_FWD5, PWM_FWD6, PWM_FWD7};
int revSpeed[] = {PWM_FLT, PWM_REV1, PWM_REV2, PWM_REV3, PWM_REV4, PWM_REV5, PWM_REV6, PWM_REV7};
int curSpeed = 0;
// IR led on port 13
LEGOPowerFunctions lego(13);
int setSpeed = 3;
int irPin = 12;
int count = 0;
int buttonState=0;
int fwdRev=0;
int stationPin = 2;
int buzzerPin = 11; 
//irPin - Lap Sensor
//stationpin -Station Sensor
//buzzerpin - Buzzer pin</p><p>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)
  {
      Serial.print("Estacao: ");
      Serial.println(digitalRead(stationPin))
      Serial.print("contador:");
      Serial.println(count);
      if(setSpeed>7) setSpeed=7;
      if (fwdRev==0)
      {
        curSpeed=fwdSpeed[setSpeed];
      } 
      lego.SingleOutput( PWM, PWM_FWD5, RED, CH1);
      delay(100);
      if (digitalRead(stationPin) == 0)
      {
        Serial.print("teste");
        delay(1000);
        Serial.print("teste2");
        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
    Serial.print("contador:");
    Serial.println(count);
    count = 0; 
  }
}

Step 6: Final Result

And the final result is this.

Step 7: Lego Library Explained

Update:

I add a link to the Lego Power Functions tutorial that i use.

PDF tutorial