
There are barriers between a railroad train and a pedestrian or car crossing.
In this project we will realize a system that simulate the operation of automatic barriers for railways trains.
We will design this system using:
two ultrasonic sensors hc-sr04 for the detection of the arrival of the train.
two servomotors to lower or raise the barriers
HC-SR04 sensor
Supply power module
test plate
connecting wires
toy train
To do the assembly, we connected:
For the first sound sensor HC-SR04:
the VCC pin to the 3.3V pin of the Arduino
the Trig pin to pin 3 of the Arduino
the ECHO pin to pin 4 of the Arduino
the GND pin to the GND pin of the Arduino
For the second sound sensor HC-SR04:
the VCC pin to the 3.3V pin of the Arduino
the Trig pin to pin 8 of the Arduino
the ECHO pin to pin 9 of the Arduino
the GND pin to the GND pin of the Arduino
For the first servomotor:
red wire: power supply wire to be connected to the 5V terminal of the power supply module
brown wire: wire to connect to the GND pin of the Arduino
Yellow: Positioning signal wire connected to pin 3 of the Arduino
For the second servomotor:
red wire: power supply wire to be connected to the 5V terminal of the power supply module
brown wire: wire to connect to the GND pin of the Arduino
Yellow: Positioning signal wire connected to pin 7 of the Arduino
For LEDs:
the terminals (-) of the LEDs to GND of the Arduino
the (+) terminal of the first red LED to pin 2 of the Arduino
the (+) terminal of the second red LED to pin 7 of the Arduino
Here are the micropython programs that allow you to:
calculate the distance between the HC-SR04 sensor and the detected object
raise or lower barriers
turn LEDs on or off
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <HCSR04.h> #include <Servo.h> Servo monServomoteur1; Servo monServomoteur2; const int LED1=2; const int servo1Pin=3; const int trig1Pin = 4; const int echo1Pin = 5; const int LED2=6; const int servo2Pin=7; const int trig2Pin = 8; const int echo2Pin = 9; int position1 = 90; int position2=90; // initialisation du 1er capteur avec les broches utilisees. UltraSonicDistanceSensor distanceSensor1(trig1Pin, echo1Pin); // initialisation du 2eme capteur avec les broches utilisees. UltraSonicDistanceSensor distanceSensor2(trig2Pin, echo2Pin); void setup(){ monServomoteur1.attach(servo1Pin); monServomoteur2.attach(servo2Pin); pinMode(LED1,OUTPUT); //règle la borne numérique numéro 7 de la carte Arduino en mode sortie pinMode(LED2,OUTPUT); //règle la borne numérique numéro 8 de la carte Arduino en mode sortie } void loop(){ if((((distanceSensor1.measureDistanceCm()+1)<6)&&(distanceSensor1.measureDistanceCm()+1)>0)) {// If the hc-sr04 sensor detects the arrival of the train digitalWrite(LED1,HIGH); // Turn on the red LED while (position1>0){ monServomoteur1.write(position1); // lower the barrier position1--; delay(10); } }else { digitalWrite(LED1,LOW); // Turn off the red LED while (position1<90){ monServomoteur1.write(position1); // raise the barrier position1++; delay(10); } } if((((distanceSensor2.measureDistanceCm()+1)<6)&&(distanceSensor2.measureDistanceCm()+1)>0)) {// If the hc-sr04 sensor detects the arrival of the train digitalWrite(LED2,HIGH); // Turn on the red LED while (position2>0){ monServomoteur2.write(position2); // lower the barrier position2--; delay(10); } }else { digitalWrite(LED2,LOW); // Turn off the red LED while (position2<90){ monServomoteur2.write(position2); // raise the barrier position2++; delay(10); } } } |