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. The infrared proximity sensor will be connected to a digital input pin.

Schematic:

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 the johhny-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);