Diorama Realism: Automated Rail Crossing
The Automated Rail Crossing System (popularized by makers like Sheekar Banerjee) transforms a static model railroad diorama into a living, responsive world. It integrates servo positioning with asynchronous LED flashing logic to mimic real-world warning signals flawlessly.
Circuit Planning:
Arduino to Ultrasonic Sensor Arduino -----> HC-SR04 Vin -----> Vcc GND -----> GND 2 -----> trigPin 3 -----> echoPin
Arduino to Servo Motor Arduino -----> Servo Motor Vin -----> Red wire (+) GND -----> Black or Dark wire (-) Digital pin 9 -----> Yellow/Orange wire (data)
EXPANDED TECHNICAL DETAILS
Non-Blocking Asynchronous Flashing
The hardest part of this project is the Warning Lights.
- A real railroad crossing has two red lights that flash in an alternating pattern: left, right, left, right.
- If you code this using
delay(500);, the Arduino processor completely freezes. It wouldn't be able to listen to the sensors to know if the train had passed! - You must use the
millis()logic structure (The "Blink Without Delay" technique). if (currentMillis - previousMillis >= 500) { toggleLights(); }- This allows the Arduino to flash the massive warning LED stanchions flawlessly while simultaneously calculating the approach speed of the train.
Actuating the Boom Gates
- The Approach: A TCRT5000 IR Sensor is buried under the track, 3 feet before the road crossing.
- The Arduino detects the train. It immediately starts the asynchronous LED flashing sequence.
- It commands two SG90 Micro Servos (hidden under the road) to slowly rotate 90 degrees, dropping the boom gates.
- The Departure: A second IR Sensor is buried 3 feet after the road crossing. When the train's caboose clears the second sensor, the Arduino raises the boom gates and turns off the warning flashers!
Scenery Component List
- Arduino Uno/Nano: The dispatch center.
- IR Obstacle Sensors (x2) or Light Dependent Resistors.
- Micro Servos (SG90 x2).
- Red LEDs and 220-ohm Resistors (x4) integrated into plastic 3D-printed crossing totems.
- MP3 Sound Module (Optional: To play the loud "Ding-Ding-Ding" crossing bell from a micro-SD card!).
Code:
//the code is entirely created by: SHEEKAR BANERJEE (at December 2017)
//Dept. of CSE, IUBAT
//AI-ML-IOT Solution Engineer and Researcher
#include<Servo.h> //This calls the Header of the repository containing the library files of Servo Motor Driver Modules
#define trigPin 2 //This initializes the Trig pin of HC-05 Ultrasonic Sensor to the pin no. 2 of Arduino Microcontroller
#define echoPin 3 //This initializes the Echo pin of HC-05 Ultrasonic Sensor to the pin no. 3 of Arduino Microcontroller
Servo sm; //Servo is a Class which is providing an independent data type // sm is a variable which is representing the servo motor
int sound=250;
void setup() {
Serial.begin(9600); //Streaming 9600 bits of sensor data per second
pinMode(trigPin,OUTPUT); //Here, Trig pin triggers the electrical impulses into the sensor as an output
pinMode(echoPin,INPUT); //Here, Echo pin inputs the reflective signals into the sensor
sm.attach(9); //Servo motor data pin is attached to Arduino Digital Pin 3
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1; //Calculation of distance
if(distance<10)
{
sm.write(80);
}
else {
sm.write(0);
}
}
Results:
Automated Rail Crossing System | Sheekar Banerjee