Automatic Railway Gate


Description

        This is a project in which a Railway Gate is automatically controlled by using Arduino. For this we are using Ultrasonic Sensor and Servomotor. The train which reaches the railway gate is sensed by using Ultrasonic Sensor and the gate opens and closes by the help of a servomotor. This project also includes a red light which will turn on when the train reaches the railway gate and a green light which will turn on when the train passes the railway gate.
      
Components Required

 1. Arduino UNO * 1
 2. Ultrasonic Sensor(HC-SR04)  * 1
 3. Servomotor * 1
 4. LED(Red)  * 1
 5. LED(Green)   * 1
 6. Jumper Wires * 1

Circuit Diagram



Program
 
#include <Servo.h>
Servo servo1;
int trigPin = 9;
int echoPin = 8;
int greenLed = 11;
int redLed = 12;
float distance;
float duration;
 
void setup() 
{
servo1.attach(7); 
 pinMode(greenLed, OUTPUT);
 pinMode(redLed, OUTPUT);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
void loop() 
{
  if(distance < 8)
  {
  servo1.write(90);
  digitalWrite(redLed,HIGH);
  digitalWrite(greenLed,LOW);
   }
  else
  { 
   servo1.write(0);
   digitalWrite(redLed,LOW);
   digitalWrite(greenLed,HIGH);
  }
  digitalWrite(trigPin, LOW);
  delay(2);
  digitalWrite(trigPin, HIGH);
  delay(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) /29.1;
  delay(100);
}