CI

Automatic Toll Gate System Using Ultrasonic Sensor and Servo Motor

Description

In this project, we will build an Arduino-based Automatic Toll Gate System using an HC-SR04 ultrasonic sensor and a servo motor. The system automatically detects an approaching vehicle and opens the toll gate without requiring manual operation.

The ultrasonic sensor continuously measures the distance of nearby objects. When a vehicle comes within 15 cm of the sensor, Arduino detects it and rotates the servo motor to open the gate. After 3 seconds, the gate automatically closes and returns to its original position. This project is useful for understanding ultrasonic sensors, servo motor control, and automation using Arduino UNO.

Components Required

ComponentQuantity
Arduino UNO1
HC-SR04 Ultrasonic Sensor1
Servo Motor1
Breadboard1
Jumper WiresAs required

Circuit Diagram

Connect the HC-SR04 ultrasonic sensor and servo motor to the Arduino UNO according to the circuit diagram and connection tables below.

Arduino Automatic Toll Gate System Circuit Diagram
Download Circuit Diagram

Pin Connection Table

HC-SR04 Ultrasonic Sensor → Arduino UNO

Ultrasonic Sensor PinArduino UNO Pin
VCC5V
TRIGD4
ECHOD5
GNDGND

Servo Motor → Arduino UNO

Servo Motor WireArduino UNO Pin
VCC (Red)5V
GND (Brown/Black)GND
Signal (Orange/Yellow)D11

Working Principle

The HC-SR04 ultrasonic sensor sends ultrasonic waves towards an approaching vehicle and measures the time taken for the echo to return. Arduino calculates the distance using the measured echo duration.

When an object is detected within 15 cm, Arduino sends a control signal to the servo motor, rotating it to 90 degrees to open the toll gate. The gate remains open for 3 seconds and then the servo motor returns to 0 degrees, closing the gate automatically. If no object is detected within the specified distance, the gate remains closed.

Arduino Code

// Creative Inventions
// Subscribe to my YouTube channel

#include <Servo.h>

// Ultrasonic Sensor Pins
const int trigPin = 4;
const int echoPin = 5;

// Servo Motor Pin
const int servoPin = 11;

// Create Servo Object
Servo tollGate;

long duration;
float distance;

// Gate Positions
const int gateClosed = 0;
const int gateOpen = 90;

// Vehicle Detection Distance
const int detectionDistance = 15;

void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Attach Servo Motor
  tollGate.attach(servoPin);

  // Initially Close the Gate
  tollGate.write(gateClosed);

  Serial.begin(9600);
}

void loop() {

  // Send Ultrasonic Pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  // Read Echo Duration
  duration = pulseIn(echoPin, HIGH, 30000);

  // Calculate Distance
  if (duration == 0) {
    distance = 999;
  } else {
    distance = duration * 0.0343 / 2;
  }

  // Display Distance on Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Check Vehicle Detection
  if (distance <= detectionDistance) {

    // Open Toll Gate
    tollGate.write(gateOpen);
    delay(3000);

    // Close Toll Gate
    tollGate.write(gateClosed);
    delay(1000);
  }

  delay(100);
}