Day 01

Begineer

Day 24 :- Automatic Gate System

Sept 06, 2026

Completed

Wiring Schematic

Arduino Code

ARDUINO / C++

Day 24 Core Training Documentation

Project Overview

The Automatic Gate System project teaches you how to create an intelligent entrance solution that automatically opens when vehicles approach and closes when they pass through completely. This project combines sensor technology with mechanical control.

In this project, an HC-SR04 ultrasonic sensor detects approaching vehicles. A Servo Motor controls the gate position. When the sensor detects a vehicle at a set distance, the gate opens automatically. After the vehicle passes, the gate closes automatically.

When no vehicles are present, the gate remains closed for security. As a vehicle approaches within detection range, the sensor triggers the gate opening sequence. After the vehicle passes and is no longer detected, the servo closes the gate automatically.

This project teaches Vehicle Detection, Automated Gate Control, Distance-Based Decision Making, Timing Sequences, and Smart Entrance Systems.

By finishing this project, you’ll know how automatic gate systems work in parking lots, driveways, secure facilities, and commercial entrances.

Components Required

To build this circuit, collect these specific items from your kit:

Arduino Uno × 1, Breadboard × 1, HC-SR04 Ultrasonic Sensor × 1, Servo Motor (MG996R) × 1, Status LEDs × 3, 220Ω Resistor × 3, Buzzer × 1, Jumper Wires × 10, USB Cable × 1

Circuit Connections

Step 1: Connect Ground Rail

Connect the Arduino GND pin to the negative rail of the breadboard. This creates a shared ground connection for all components in your gate control system.

Step 2: Connect HC-SR04 Sensor VCC

Connect the VCC pin of the HC-SR04 ultrasonic sensor to Arduino 5V. This provides power to the distance measurement sensor module for vehicle detection.

Step 3: Connect HC-SR04 Sensor GND

Connect the GND pin of the HC-SR04 to the breadboard negative rail. This completes the sensor power circuit and establishes proper ground reference.

Step 4: Connect HC-SR04 TRIG Pin

Connect the TRIG pin of the HC-SR04 to Arduino Pin 9. This pin sends the trigger signal for distance measurement to detect vehicles.

Step 5: Connect HC-SR04 ECHO Pin

Connect the ECHO pin of the HC-SR04 to Arduino Pin 10. This pin receives the echo duration measurement from the ultrasonic sensor.

Step 6: Connect Servo Motor Ground

Connect the brown ground wire of the Servo Motor to the breadboard negative rail. This provides the ground connection for Servo Motor operation.

Step 7: Connect Servo Motor Power

Connect the red power wire of the Servo Motor to Arduino 5V. This supplies power to the servo for gate opening and closing movements.

Step 8: Connect Servo Motor Signal

Connect the orange signal wire of the Servo Motor to Arduino Pin 8. This pin sends PWM control signals to position the gate servo.

Step 9: Connect Status LEDs

Connect the red LED to Pin 3, yellow LED to Pin 4, and green LED to Pin 5 through 220Ω resistors. Connect all cathodes to ground for status indication.

Step 10: Connect Buzzer

Connect the buzzer positive terminal to Arduino Pin 7. Connect the negative terminal to ground. This provides audio notification for gate events.

Step 11: Verify Connections

Check all sensor, servo, LED, and buzzer connections before uploading code. Ensure the servo can move freely to control the gate mechanism without obstruction.

Important: The DHT11 sensor requires a pull-up resistor on the data line to function properly. The sensor communicates using a single-wire digital protocol that needs this resistor for stable operation.

How the Arduino Code Works

Initialize All Sensors and Actuators

In setup(), Arduino initializes the servo on Pin 8, configures the distance sensor on Pins 9 and 10, and sets all LED pins as outputs. The gate starts in the closed position.

Measure Distance to Approaching Vehicles

Inside the main loop, Arduino continuously reads the HC-SR04 sensor to measure distance to any approaching objects. The distance calculation uses the physics formula from Day 9.

Detect Vehicle Approach

When the distance reading drops below a detection threshold of eighty centimeters, Arduino recognizes that a vehicle is approaching and triggers the gate opening sequence.

Open Gate Automatically

When a vehicle is detected, Arduino sends the servo command to move to 90-180 degrees, opening the gate. The yellow LED illuminates and buzzer sounds warning.
 

Wait for Vehicle to Pass

The system monitors distance continuously while the gate is open. If no vehicle is detected within the gate area for ten seconds, the gate closing sequence begins immediately.

Expected Output

After uploading your code, here is what happens.

At startup, the servo closes the gate and the red LED illuminates. When a vehicle approaches within 80cm, the yellow LED turns on and buzzer sounds. The gate opens smoothly. After the vehicle passes and no movement is detected for 10 seconds, the gate closes. The red LED illuminates again.

Operational Troubleshooting

Sensor Does Not Detect Approaching Vehicles :- Verify the HC-SR04 connections to Pins 9 and 10. Check that the sensor faces the expected vehicle approach direction. Ensure the detection threshold distance is appropriate for your gate installation..

Gate Opens But Does Not Close :- Check the timeout duration in your code and verify it equals ten seconds. Ensure that the servo closing command is included after the timeout period. Test with shorter duration.

Gate Moves Erratically or Jitters :- Check for loose servo connections and ensure stable power supply. Add small delays between servo commands to smooth movement. Verify the servo horn is securely attached.

LEDs Do Not Change Color Appropriately :- Verify each LED connects to the correct pin through a resistor. Test each LED individually to ensure functionality. Check that digitalWrite() commands match correct pins.

Buzzer Produces No Sound :- Verify the buzzer positive terminal connects to Pin 7 and negative connects to ground. Check that the tone() or digitalWrite() command is included in your gate control code.

What You Learned

By finishing this project, you now know :-

How to integrate multiple sensors and actuators in complex systems. You understand how to create state-based automation sequences. You learned how to implement safety detection for autonomous gates. You can combine distance sensing with mechanical control effectively. You mastered building practical automated entrance systems.

Next Lesson

In Day 25, you’ll learn how to build an Obstacle Avoiding Robot using the HC-SR04 sensor with motor control to create a fully autonomous robot. You will integrate distance sensing with motor coordination for autonomous navigation.