Arduino Sensor Activated Flashing Lights.

How to use Arduino to make level crossing sensor activated lights.

This page is part of our Arduino Projects series that you can find at this link.  Here is a link to our Arduino projects  YouTube video playlist. Playlist

Subscribers to our Video Channel get a special discount coupon to use in our shop. Click image to watch this video.

Please also note the subscribers discount to the left.

 

 

 

 

 

The sketch

 

/* Grade Crossing LED’s triggered by IR Sensors
Adapted from Geoff Bunza’s sketch Not using the Servo actuated Gate Arms
Modified by Gary Granai 25 September, 2020

Arduino Projects

*/
int sensor1 = 5; // IR sensor==Pin goes LOW when train detected
int sensor2 = 6; // IR sensor==Pin goes LOW when train detected
int led1 = 10; // Led 1 pin alternating flasher
int led2 = 11; // Led 2 pin alternating flasher
int led3 = 14; // Led 3 pin alternating flasher
int led4 = 15; // Led 4 pin alternating flasher
int entering_sensor = 5; // which sensor was triggered first
int leaving_sensor = 6; // which sensor shows train leaving
int sequence_started = 0; // this says if the crossing is active
int flash_state = 0; // variable for function flash_leds()
long flash_time = 0; // variable for function flash_leds()
long flash_interval = 900; // time in milliseconds between alternating flashes
int sensor_count = 0; // declare variable for while statement

void setup() {

pinMode(sensor1, INPUT); // sensor1 is an INPUT
pinMode(sensor2, INPUT); // sensor2 is an INPUT
pinMode(led1, OUTPUT); // led1 on pin 10 is an OUTPUT
pinMode(led2, OUTPUT); // led2 on pin 11 is an OUTPUT
pinMode(led3, OUTPUT); // led3 on pin 14 is an OUTPUT
pinMode(led4, OUTPUT); // led4 on pin 15 is an OUTPUT
digitalWrite(led1, LOW); // Start with flashers off
digitalWrite(led2, LOW); // Start with flashers off
digitalWrite(led3, LOW); // Start with flashers off
digitalWrite(led4, LOW); // Start with flashers off
flash_time = millis(); // for function flash_leds()
}

void loop() {
// The two if statements defines which sensor is which and
// changes sequesnc_started from FALSE to TRUE then starts
// the leds flashing
if ((digitalRead (sensor1) == LOW) && (sequence_started == 0)) {
sequence_started = 1;
leaving_sensor = sensor2;
flash_leds();
}
if ((digitalRead (sensor2) == LOW) && (sequence_started == 0)) {
sequence_started = 1;
leaving_sensor = sensor1;
flash_leds();
}
// First if== sequence started and continue flashing leds
// Next if== train is leaving the crossing
if (sequence_started) flash_leds();
if ((digitalRead(leaving_sensor) == LOW) && (sequence_started == 1)) {

// as long as the leaving sensor is active the train is still in the crossing
// while checks if train is gone yet
while (sequence_started == 1) {
sensor_count = 0;
for (int i = 1; i < 40; i++) {
if (digitalRead(leaving_sensor) == LOW) sensor_count++;
delay (30);
flash_leds();
}
if (sensor_count == 0) sequence_started = 0;
flash_leds();
}
// At this point train is no longer over the leaving sensor
digitalWrite(led1, LOW); // Turn OFF led
digitalWrite(led2, LOW); // Turn OFF led
digitalWrite(led3, LOW); // Turn OFF led
digitalWrite(led4, LOW); // Turn OFF led
}
}

void flash_leds() {
if (flash_time > millis()) return;
flash_state = ~flash_state;
digitalWrite(led1, flash_state);
digitalWrite(led2, ~flash_state);
digitalWrite(led3, flash_state);
digitalWrite(led4, ~flash_state);
flash_time = millis() + flash_interval;
}

 

Wiring instructions

There are 3 connections a sensor. The positive and negative connections go to + and – 5v. An external power supply is best.

Connect the data pin on Sensor 1 to pin 5 and the data pin on Sensor 2 to pin 6.

There are two led sets. Connect one set to pins 10 and 11. Insert resistors between the LEDs and the pins.

Connect the other set of LEDs to pins 14 and 15. Insert resistors between the LEDs and pins.

Look at the wiring diagram.

 

Notes

The LEDS for this animation program should be wired cathode to anode. Since your light stands will be small, using prewired LEDs is convenient. Make the connection after you have the LED in place. And then add the resistors.

 

Wiring Diagram

flashing light wiring diagram
click to enlarge

 

Video Demonstration

Tell others about this information by using one of these services. Thank you for the support.
Scroll to top