Welcome to this dev log, where I’m documenting my journey into electronics. Today, I’m presenting a classic yet comprehensive project: an automated gate.
This project has allowed me to work with several key components: distance sensing, motion control via a servo motor, and the management of visual and audio signals.


Project Description
The goal is simple: when an object comes within 15 cm of the gate, it opens automatically.
Expected Behavior:
Idle: The gate is closed, the red LED is on.Detection: The ultrasonic sensor detects an object.Action: The gate lifts (servo at 180°), the green LED turns on, and a buzzer sounds to signal the opening.
List of Components
To build this setup, I used:
- Elegoo Mega R3: The brain of the project.
- HC-SR04 Ultrasonic Sensor: To measure distance.
- Servo Motor: To physically operate the barrier.
- LEDs (Red and Green): For the visual interface.
- Active Buzzer: For the audible alert.
- Resistors (220Ω) and breadboard
Schematic and Assembly
The assembly was designed and tested in Tinkercad before being built. Here is the wiring diagram:

Link to the interactive project: Tinkercad.com
\
Wiring Details:
The ultrasonic sensor is connected to pins 12 (Trig) and 11 (Echo). The servo motor is controlled by pin 9. The LEDs are connected to pins 2 (Red) and 4 (Green). The buzzer is connected to pin 7.
The Code
I used the SR04 library for the ultrasonic sensor and the Servo library for motor control. The code manages the barrier’s status, the LEDs, and the buzzer using simple logic based on the measured distance.
Including Libraries and Initial Declarations
#include <SR04.h>
#include <Servo.h>
Servo myservo;
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;- Inclusion of libraries to simplify management of the sensor (SR04 encapsulates trig/echo) and the servo.
- Pin definition and initialization of the sr04 object; a stores the distance (long to handle potentially large values).
Variables for Barrier State and Timings
int gateClose = 90;
int gateOpen = 180;
bool gateState = false;
bool lastgateState = false;
unsigned long lastTime = 0;
const unsigned long delayTime = 1000;- Servo angles: 90° closed, 180° open.
gateStateandlastgateStateto detect state transitions (prevents repeated commands to the servo).- Timings based on
millis()(non-blocking);delayTime= 1s to measure distance periodically.
Pin Assignment for LEDs and Buzzers
byte redLed = 2 ;
byte greenLed = 4 ;
byte buzzer = 7;- Pins assigned for LEDs and buzzers (byte to save memory).
Setup Fonction
void setup() {
Serial.begin(9600);
pinMode (redLed, OUTPUT);
pinMode (greenLed, OUTPUT);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
myservo.attach(9); // Control the servo motor en pin 9
myservo.write(gateClose); // On "charge" la position cible en mémoire
delay(1000);
}- Initialize serial port for debugging; set pins to OUTPUT.
- Initial state: red ON, green OFF, buzzer OFF, servo closed.
delay(1000)for stabilization (prevents boot glitches).
Fonction Loop (Boucle Principale)
void loop() {
a=sr04.Distance();
if (millis() - lastTime >= delayTime )
{
Serial.print(a);
Serial.println("cm");
lastTime = millis();
digitalWrite(buzzer, LOW);
}
if( a <= 15 )
{
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
gateState = true;
} else{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
gateState = false;
}
if(gateState == true && lastgateState == false )
{
myservo.write(gateOpen);
digitalWrite(buzzer, HIGH);
} else if (gateState == false && lastgateState == true ) {
myservo.write(gateClose);
}
lastgateState = gateState;
}Continuous distance measurement: a=sr04.Distance(); – Measures the distance at each iteration of the loop, for immediate response to the threshold.
Measures distance every 1 second using millis() (non-blocking, unlike delay()); displays the measurement on the Serial port; turns off the buzzer here to limit its duration to ~1 second.
15 cm threshold: updates LEDs and gateState (true=open).
Transition detection:
If changes from closed to open = opens servo + buzzer ON.
If changes from open to closed = closes servo.
State update: lastgateState = gateState. Prepares for the next change detection, key for anti-repeat logic.