Your Cart

Automatic Toll Gate Using Arduino and HC-SR04 sensor

Hey guys.

This article will guide you through the process of creating an automated toll gate utilizing an Arduino and the HC SR04 distance sensor. You may have encountered toll gates on toll roads, bridges, tunnels, and other locations. We’re introducing a system that allows the toll gate to function automatically around the clock, 24/7. To determine the distance from the vehicle, we employ an ultrasonic sensor.

Automatic Toll gate Using Arduino and HC SR04 sensor 1Make the connection using the HC-SR04 sensor according to the given diagram and then upload the code to the Arduino.

So without wasting any more time, let us proceed.

Description of Arduino Based Toll Gate

  • Automatic Toll gate Using Arduino and HC SR04 sensor 2We are using a servo motor here to move the barricade up and down.
  • You can use a cardboard strip to make it but make sure that it is light, and the servo motor shaft can easily move it.
  • Fix this strip with the servo shaft.
  • When vehicles come near the entry point or the checkpoint, then the toll will move up automatically and let them go.
  • You can also check the smart incentive spirometer and ultrasonic sensors made by us.

Automatic Toll gate Using Arduino and HC SR04 sensor 3

Automatic Toll gate Using Arduino and HC SR04 sensor4Components Required

You need these components for making this project. Here is a list of the components, please make sure that you have used the correct and working components.

Basically, Arduino UNO is a microcontroller that performs various tasks. We can write our code in the Arduino IDE software and then upload it to the Arduino board. Using a USB port in the Arduino, through which we can power it and program it. You can also connect a DC adapter of 5 to 9 volts to power the board.

we are utilizing a DC servo motor. This allows us to control the range of movement within the motor’s shaft.

This is an ultrasonic sensor that releases ultrasonic waves from one end and receives it from the other end. We can calculate the distance between two marks by calculating the time and converting it. This is like the principle of a SONAR mechanism.

Jumper wires are used for making connections between two pins, and ports. There are three types, female to female jumper wires, male to male jumper wires, and male to female jumper wires. A breadboard is a mounting board that has so many slots for making the connections.

  • USB cable for uploading the code

Connect this cable to the USB port in your PC or laptop and then plug it into the Arduino such that the power LED on the Arduino goes on.


Whenever your car comes near the toll gate, the toll gate will open automatically. There we are using a sensor named ultrasonic sensor. When an ultrasonic sensor finds any object in front of the sensor then it will send the information to the Arduino which is the brain of this project. As Arduino gets the information it will compare and process the information and according to the condition, it will send the instruction to the servo motor to rotate. And the gate attached to the servo motor will move.

Circuit for Automatic Toll

  • Automatic Toll gate Using Arduino and HC SR04 sensor diagram 1
  • Above is the circuit diagram for the automatic toll plaza project.
  • You need to make all the connections according to it.
  • First of all, take the servo motor and connect its positive wire with the 5 volts pin of the Arduino.
  • Then join the negative wire of the servo motor with the GND pin of the Arduino.
  • Make sure that all the connections are correct and tight.
  • Attach the signal wire of the servo motor with the digital-9 pin of the Arduino.
  • After that, take the ultrasonic sensor, you can see the names of the pins mentioned on the surface of the sensor. So read them carefully and connect the VCC pin of the sensor with the 5 volts pin of the Arduino.
  • Then connect the GND pin of the ultrasonic sensor with the GND pin of the Arduino.
  • Notice that these two pins are for the power and for sending the data we have another two pins left in the sensor. Join the TRIG pin of the ultrasonic sensor with the digital-6 pin of the Arduino.
  • Attach the ECHO pin of the ultrasonic sensor with the digital-7 pin of the Arduino.
  • All the connections are complete now, and you can proceed with the next steps.

About the Code

Automatic Toll gate Using Arduino and HC SR04 sensor 6

Talking about the code, we declare two pins of the ultrasonic sensors as constant data types and assign their values as Arduino Digital pins. S1 is an object of type servo, we will use S1 for further servo operations. Then we declare a duration variable of long data type so that it can hold a large value. In the void setup function, we are declaring the pin modes for both of the pins and also assigning the signal pin for the servo using attach function. Then start the serial communication between the Arduino and the computer using begin() function. Specify the baud rate to 9600.


In the loop function gather the relevant data from ultrasonic sensors using appropriate functions and syntax. We are storing the time gap in the duration variable and using it for the distance calculation. You can get the distance in two units of measurement, i.e in cm and in inches. The print function is used to display the content of the distance variable. It will continuously print the values of the distance variable. Then we add a conditional statement that if the value of the distance variable is less than 10 cm then only the servo motor will rotate, else no movement is there. We can rotate the servo motor using the write() function and give the angle of movement as a parameter. The delay function will add a delay of x seconds to the program execution every time.

8 1

Code for Automatic Toll Gate using Arduino

NOTE: Please upload this code to Arduino. If the software is showing any error, then you need to add <Servo.h> library in your system. Check here how you can add any zip library to your Arduino IDE software.

#include <Servo.h>

Servo s1;

const int trigPin = 6;

const int echoPin = 7;

long duration;

int distanceCm, distanceInch;

void setup()



pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);



void loop() {

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distanceCm= duration*0.034/2;

distanceInch = duration*0.0133/2;

Serial.println(“Distance: “);


if (distanceCm < 10)











We hope that you enjoyed learning about this automated toll plaza and have a clear understanding of how it operates.

Check out our other projects:

To learn about The Best Arduino Starter Kits from Kunkune Click Here!

Happy Learning!