In this project, we'll create a simple train accident prevention system using an Arduino Uno and the components you've provided.
Here's the idea:
- Two LED bulbs and a buzzer operate as warning signals for an approaching train.
- The ultrasonic sensor detects the distance of the train from the crossing.
- When the train approaches, warning signals (LEDs and buzzers) are activated to warn the pedestrians and vehicles.
Step 1: Assemble Your Components
- 2 LEDs, a buzzer, and an ultrasonic sensor are placed at each side of a railway crossing.
- The 6 poles will be used for supporting the LEDs and buzzers.
- The ultrasonic sensor is to be placed on a platform that crosses the tracks or on a pole near the railway track.
Step 2: Connect Components to the Arduino Uno
- Connect the LEDs, buzzers, and ultrasonic sensors to the appropriate pins on the Arduino Uno.
- Use the jumper wires to connect the LEDs and buzzers to the digital pins on the Arduino Uno.
- Connect the 9V battery to the Arduino Uno for power supply.
Step 3: Write the Code for the Train Accident Prevention System
#include <NewPing.h>
const int triggerPin = 8;
const int echoPin = 9;
const int maxDistance = 200; // Maximum distance for the ultrasonic sensor to measure (in cm)
NewPing sonar(triggerPin, echoPin, maxDistance);
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int buzzer1 = 5;
const int buzzer2 = 6;
const int buzzer3 = 7;
void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(buzzer1, OUTPUT);
pinMode(buzzer2, OUTPUT);
pinMode(buzzer3, OUTPUT);
}
void loop() {
delay(50);
float distance = sonar.ping_cm();
if (distance <= 50 && distance >=1) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(buzzer1, HIGH);
digitalWrite(buzzer2, HIGH);
digitalWrite(buzzer3, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(buzzer1, LOW);
digitalWrite(buzzer2, LOW);
digitalWrite(buzzer3, LOW);
delay(100);
} else {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(buzzer1, LOW);
digitalWrite(buzzer2, LOW);
digitalWrite(buzzer3, LOW);
}
}
This code initializes the components connected to the Arduino Uno and runs a loop to continuously monitor the distance detected by the ultrasonic sensor (HC-SR04). When the train approaches within 50 cm of the crossing, the LED bulbs and buzzers will start flashing and beeping to alert the pedestrians and vehicles nearby.
Step 4: Testing and Adjustment
- Upload the code to the Arduino Uno.
- Observe the behavior of the LEDs and buzzers when the ultrasonic sensor detects the train's presence.
- If required, make adjustments to the distance threshold or positions of the components to ensure appropriate functioning.
By following these steps, you'll be able to create a simple yet effective train accident prevention system using an Arduino Uno and the components that you have.