• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeArduinoTrain Crossing – Sensor, Servo and LEDs
Previous Next

Train Crossing – Sensor, Servo and LEDs

Posted by: Olva , September 30, 2016

Train Crossing – Sensor, Servo and LEDs

train crossing

Train Crossing

The train is coming and we need to protect our cars from getting in the way! Let’s detect if the train is coming and block the road with a train crossing signal.

Goal:

– Detect train, lower crossing arm and blink lights.

Challenges:

– Construct the traffic crossing signal.

  • Servo to control crossing arm.

– Alternating blinking LEDs.

– Infrared proximity sensor for train detection.

  • Delay the crossing for long trains.

Components:

  • Raspberry Pi (or any NodeJS host)
  • Arduino (I used the Mega 2560 but any will do)
  • 2x 1.8v LEDs
  • servo
  • breadboard
  • 2x 220 Ohm resistors

– IR proximity sensor

Circuit:

There will be two LEDs connected to individual pins since they will blink independently.

The servo will need to be connected to a PWM pin so that we can “pulse” the correct position to it. I will be using a 5v hobby servo with 180 degrees of rotation.

hobby servo

The infrared proximity sensor will be connected to a digital input pin.

keyes IR sensor

Schematic:

TrainCrossing Circuit

Code:

We will need to initialize the board as usual and then define the two LED pins for our lights and the servo for the crossing arm. Then we need to define the two states that the crossing will be in; on or off. Each state will represent the current lights and position of the servo.

Then we will define a sensor listener similar to a button, to trigger the action. The only difference is that the sensor has an adjustable sample rate.

We will then need to adjust for duplicate events before the train has finished crossing. There are gaps between the train coaches which send “change” signals every time one passes. If you don’t take this into consideration, the crossing will act crazy.

To understand what position we set the servo at will take a lot of trial and error. We will use the REPL to dial in the servo angle to get the crossing arm where we want it.

 

Basic Flow

  • Set initial state “off”
  • Listen for button sensor activation
  • Toggle servo and LEDs to proper state

Filename

traincrossing.js

 

Now save and close your file.

Note: If this is your first project, you will need to initialize the folder and add thejohhny-five module.
Run the following two commands before launching your new app.
npm init
npm install johnny-five --save
Now run the code
node traincrossing.js
Now drive a train in front of the sensor to see it work! You should see the following on your screen and the crossing should be working its magic. You will likely need to adjust the servo positions and the placement of the IR sensor for it all to work well.
root@thethingbox:~/IoL# node traincrossing.js

1442769369170 Device(s) /dev/ttyACM0
1442769369254 Connected /dev/ttyACM0
1442769372201 Repl Initialized
>> crossingSensor raw: 1
crossingSensor raw: 0
crossingActivate()
crossingSensor raw: 1
crossingSensor raw: 0
crossingDelayed()
crossingSensor raw: 1
crossingDeactivate()

 

Notes:

If you are using the Lego IR power functions, be sure to avoid pointing your remote control at the IR sensor or it will trigger the crossing! I had to strategically place my sensor behind a barrier to solve this. You may find it better to use other types of sensors such as photo resistors, ultrasonic or even NFC. The core logic is basically the same either way.

REPL

To dial in the servo angle, type the following in the REPL command prompt where the number is the desired angle.

crossingServo.to(111);

 Construction:

IMG_4807  IMG_4812  IMG_4813  IMG_4814

The train is crossing!

the original post is from http://www.internetoflego.com/train-crossing-sensor-servo-and-leds/

Share!
Tweet

Olva

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors