Automatic Gate Open Using Arduino and HC SR04

Introduction

Hey techies, we are back with another new post. In this article, we are going to teach you that how you can make your own automatic gate open and close system using Arduino UNO and HC SR04 ultrasonic sensors. These types of automatic doors can be seen in offices, hotels, etc. The concept of an automatic door will work best where humans can not enter or work. If the distance is less than 60 cm (ideal distance) then the gate will open. Make the circuit according to the given diagram and then upload the code. You can check our articles on IoT and basic electronics.

Automatic Gate Open

Description

  • We are using an ultrasonic sensor here for sensing the incoming vehicles.
  • Once the vehicle will pass through the sensor, it will detect the motion and send corresponding signals to the Arduino.
  • It will calculate the distance between vehicle and sensor.
  • Then the gate will open and after a gap of a few seconds, it will close automatically.
  • We attach a servo motor to the gate.
  • The motor will move 180 degrees and come back to its initial position once again.
  • The working principle of this project is quite similar to the ultrasonic range finder project made by us.

Components Required

  • Arduino UNO
  • HC SR04 ultrasonic sensor
  • Servo motor
  • Jumper wires and a breadboard
  • USB cable for uploading the code

Circuit for Automatic Gate Open

  • If you do not know the working of an ultrasonic sensor with Arduino UNO then go through it first.
  • The ultrasonic sensor that we use has four pins, two out of them are used to provide power and the remaining two are for measuring the data.
  • Connect the VCC pin of the ultrasonic sensor with the 5 volts pin of the Arduino and the GND pin of the ultrasonic sensor with the GND pin of the Arduino.
  • Then join the TRIG pin of the sensor with the digital-10 pin of the Arduino. Attach the ECHO pin with the digital-11 pin of the Arduino.
  • Take a servo motor and connect its positive supply wire with the 5 volts pin of the Arduino. Join the negative supply wire of the servo motor with the GND pin of the Arduino.
  • At last, connect the signal wire of the servo motor with the digital-9 pin of the Arduino.
  • Then power your Arduino UNO so that the components start working. Your circuit is complete now and you can proceed with the next steps.

Code for Automatic Gate Open

NOTE: Please upload this code to the Arduino UNO. If it is showing any error then you need to install <Servo.h> library. Check here how to install a zip library in the Arduino IDE software.

#include <Servo.h>
Servo servoMain; // Define our Servo
int trigpin = 10;
int echopin = 11;
int distance;
float duration;
float cm;

void setup()
{
   servoMain.attach(9); // servo on digital pin 10
    pinMode(trigpin, OUTPUT);
    pinMode(echopin, INPUT);
}

void loop()
{ 
  digitalWrite(trigpin, LOW);
  delay(2);  
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);  
  digitalWrite(trigpin, LOW);  
  duration = pulseIn(echopin, HIGH);  
  cm = (duration/58.82);
  distance = cm;
  
  if(distance<60)
  {
    servoMain.write(180);  // Turn Servo back to center position (90 degrees)
   delay(3000); 
  }
   else{
     servoMain.write(0);
     delay(50);
   }
   
}

PCBWay PCB Prototyping Services

I have assembled the whole circuit on a breadboard. As you know breadboard assembly is not effective for this type of project. So, PCBWay offers Rapid PCB Prototyping for Your Research Work. I personally, recommend PCBWay because you can get your first-try boards right in 24 hours!

PCBWay website

The prototyping stage is the most critical period of time for engineers, students, and hobbyists. PCBWay not only makes your boards quick but also makes your job right as well as cost-effective. This greatly reduces your cost and shortens the time for developing your electronic

PCBWay can provide 2 Layer PCBs to highly advanced HDI and flex boards. Even though the PCBs they produce differ a lot regarding functionality and areas of use. I am impressed with the quality of the boards, the delivery time, and the cost-effectiveness

We hope that you understand this project and now try to make it on your own. If you have any doubts regarding this project then feel free to use the comments section which is given below. Also, do check out more articles on Arduino and Raspberry Pi written by us.

Thanks for reading!

Leave a Comment