Components Used
Components Specification Quantity
- Arduino Nano 1
- Servo Motor G9 `2
- Buzzer 1
- IR LED 4
- Photodiode 4
- LM358 2
- Preset 10K 4
- Adapter 12V 1
- LED Red 4
- Resistors 10K 4
- Resistors 330 Ohms 8
Output of all the sensors are connected to A0, A1, A2 and A3 pins of Arduino.
The pins D9 and D10 of the Arduino are PWM pins. These pins are connected to the Servo motor. Servos are controlled by sending an electrical pulse of variable width, or pulse width modulation (PWM), through the control wire.
IR pair is made with dual Op-amp IC LM358. Only one IC is required for a pair. A preset is used for calibration.
If you are using ready-made IR sensors please replace “<” into “>” and “>” into “<” in the code, because output of ready-made IR sensor is invert of the sensor pair used in the project.
Four sensors are used in the project as two pairs of two sensors; these sensors are kept in the both side of level crossings gate as shown in Fig 1. All the sensors are connected to Arduino.
When train arrives from any side, it first cross the sensor1 after that cross the sensor2, in this way Arduino close the gate by sending the signal to servomotor. When train departure from any side it first cross the sensor2 after that cross the sensors, in this way Arduino open the gate.
Servomotors are used in the gate because it is very easy to use and does not require any driver IC or circuit. Servo motor has three pins. The first pin is PWM, second is Vcc and third is GND. Servo motor receives the PWM signal from Arduino and rotates the motor at fixed angle according to duty cycle of signal.
\
Program Code
#include <EEPROM.h>
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos = 0;
int sensor1 = A0;
int sensor2 = A1;
int sensor3 = A2;
int sensor4 = A3;
int RLED1 = 8;
int GLED1 = 9;
int RLED2 = A4;
int GLED2 = A5;
int buzzer = 13;
void setup() {
//Serial.begin(9600);
myservo1.attach(5);
myservo2.attach(6);
pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);
pinMode(sensor3,INPUT);
pinMode(sensor4,INPUT);
pinMode(buzzer,OUTPUT);
EEPROM.write(0, 0);
EEPROM.write(1, 0);
}
void loop() {
if (EEPROM.read(1)==0 || EEPROM.read(0)==0){
while (analogRead(sensor1)>500){
if (analogRead(sensor2)>500){
if (EEPROM.read(1)!=1){ EEPROM.write(1, 1);}
if (EEPROM.read(0)!=1){ EEPROM.write(0, 1);}
// Serial.println("X");
delay(100);
}}
while (analogRead(sensor3)>500){
if (analogRead(sensor4)>500){
if (EEPROM.read(1)!=1){ EEPROM.write(1, 1);}
if (EEPROM.read(0)!=1){ EEPROM.write(0, 1);}
// Serial.println("Y");
delay(100);
}}
}
if (EEPROM.read(1)==1 || EEPROM.read(0)==0){
while (analogRead(sensor2)>500){
if (analogRead(sensor1)>500){
if (EEPROM.read(1)!=0){ EEPROM.write(1, 0);}
if (EEPROM.read(0)!=1){ EEPROM.write(0, 1);}
//Serial.println("Z");
delay(100);
}}
while (analogRead(sensor4)>500){
if (analogRead(sensor3)>500){
if (EEPROM.read(1)!=0){ EEPROM.write(1, 0);}
if (EEPROM.read(0)!=1){ EEPROM.write(0, 1);}
//Serial.println("A");
delay(100);
}}
}
if (EEPROM.read(1)==1){ //Gate open
if (pos != 90){
for (pos = 0; pos < 90; pos += 1){
myservo1.write(pos);
myservo2.write(pos);
// Serial.println(pos);
digitalWrite(buzzer,HIGH);
delay(10);
digitalWrite(buzzer,LOW);
delay(10);
}}
//Serial.println("Gate Opend");
digitalWrite(buzzer,LOW);
if (EEPROM.read(0)!=0) {EEPROM.write(0, 0); /*Serial.println("OK");*/}
}
if (EEPROM.read(1)==0){ //Gate close
if (pos != 0){
for (pos = 90; pos > 0; pos -= 1){
myservo1.write(pos);
myservo2.write(pos);
digitalWrite(buzzer,HIGH);
delay(10);
digitalWrite(buzzer,LOW);
delay(10);
}}
//Serial.println("Gate Closed");
if (EEPROM.read(0)!=0) {EEPROM.write(0, 0); Serial.println("OK");}
}
}
No comments