AUTOMATIC GATE USING ULTRASONIC SENSOR AND DC MOTOR

by DESHBOT in Circuits > Arduino

11 Views, 0 Favorites, 0 Comments

AUTOMATIC GATE USING ULTRASONIC SENSOR AND DC MOTOR

WhatsApp Image 2026-09-25 at 10.12.34 AM.jpeg

The Automatic Gate project is an Arduino-based system that automatically opens and closes a gate when an object is detected nearby.

An HC-SR04 ultrasonic sensor is used to measure the distance of an approaching object. When the object comes within 20 cm, the Arduino activates the motor and opens the gate.

The gate remains open for 3 seconds and then the motor rotates in the opposite direction to return the gate to its original position.

An LED indicates that the gate is currently open.

Supplies

WhatsApp Image 2026-09-25 at 10.12.45 AM.jpeg
des 1.jpeg
uno.jpeg
m1.jpeg
ultra.jpeg

Components Required

ComponentQuantity

Deshboot shield

Arduino Uno/Nano

HC-SR04 Ultrasonic Sensor

DC Motor

Pin Connections


Ultrasonic Sensor

Sensor PinArduino

TRIG - A3

ECHO - A4

VCC - 5V

GND - GND

Motor Driver

Motor DriverArduino

Motor Input A - D6

Motor Input B - D7

The DC motor is connected to the motor driver's motor output terminals.

Important: The motor should use a suitable external power supply through the motor driver, and the Arduino GND and motor-driver GND should be connected together.

LED

LEDArduino

Positive - D11

Negative - GND through suitable resistor

Working Principle

WhatsApp Image 2026-09-25 at 10.12.34 AM.jpeg


The system works in the following sequence:

  1. The ultrasonic sensor continuously measures distance.
  2. The Arduino checks whether an object is within 20 cm.
  3. If an object is detected, the motor rotates in the opening direction.
  4. The motor runs for 1.5 seconds.
  5. The motor stops and the LED turns ON.
  6. The gate remains open for 3 seconds.
  7. The motor rotates in the opposite direction.
  8. The gate returns to its original position.
  9. The motor stops and the LED turns OFF.
  10. The system waits until the object moves away before detecting another object.


Distance Measurement

The ultrasonic sensor sends a sound pulse and measures the time required for the echo to return.

The program calculates the distance using:

int distance = duration * 0.034 / 2;

The system is configured to detect objects up to:

20 cm

#define DETECT_DISTANCE 20


Gate Opening

When an object is detected, the gateOpen() function is called.

digitalWrite(MOTOR_A, HIGH);
digitalWrite(MOTOR_B, LOW);

This makes the motor rotate in one direction.

The motor runs for:

1500 milliseconds = 1.5 seconds

After that, both motor outputs are turned OFF.


Gate Open Indicator

While the gate is open, the LED is turned ON:

digitalWrite(LED, HIGH);

This provides a visual indication that the gate is in the open position.

Gate Closing

After the gate remains open for 3 seconds, the gateClose() function is called.

The motor direction is reversed:

digitalWrite(MOTOR_A, LOW);
digitalWrite(MOTOR_B, HIGH);

The motor runs for another 1.5 seconds and then stops.

The LED is turned OFF after the gate closes.


. Object Detection Lock

After closing the gate, the Arduino waits until the detected object moves away.

while (getDistance() <= DETECT_DISTANCE) {
delay(100);
}

This prevents the gate from repeatedly opening and closing while an object is still in front of the sensor.


Serial Monitor

The Arduino also sends the measured distance to the Serial Monitor.

Example:

Distance: 45 cm
Distance: 32 cm
Distance: 19 cm
Distance: 18 cm

When the distance becomes 20 cm or less, the gate-opening sequence starts.

Conclusion


The Automatic Gate project demonstrates how an Arduino can combine an ultrasonic sensor, motor driver, DC motor and LED to create an automated system.

The ultrasonic sensor detects an approaching object, while the Arduino controls the motor to open and close the gate automatically. The LED provides a simple indication of the gate status.

This project demonstrates important embedded-system concepts including distance sensing, motor control, conditional logic, timing and automation.