Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
140 min
Share

Make an Automatic Gate opener using Arduino

We are sure that you have seen an automatic door that opens and closes by itself. These kinds of gates are used in hotels, offices, and other places, and typically use special kinds of motors and sensors for their function. In this project, however, we will make an Automatic Gate opener using Arduino.

Project Video

Overview

In this tutorial, we will teach you how to make your own automatic door opener using an ultrasonic sensor, servo motor, and Arduino.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
2.8 inch Touch Screen Module
Get Item
SG90 Servo -Positional Rotation
Get Item
Ultrasonic Sensor (HC-SR04)
Get Item
1/4 Watt Resistor (20 Pack)
Get Item
Half-size Breadboard
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the TFT Display and the Arduino and the ultrasonic sensor and the servo motor as shown in the image below.


Connections from the Arduino to the breadboard:

  • Arduino GND pin → Breadboard ground line

  • Arduino 5V pin → Breadboard 5V line


Connections from the servo motor:

  • Servo GND pin→ Arduino GND pin

  • Servo VCC pin→ Arduino VCC pin

  • Servo signal pin → Arduino pin 4


Connections from the ultrasonic sensor:

  • ultrasonic sensor VCC pin → Breadboard 5V line

  • ultrasonic sensor GND pin → Breadboard ground line

  • ultrasonic sensor Trig pin → Arduino pin 3

  • ultrasonic sensor Echo pin → Arduino pin 2


Connections from the TFT Display to the breadboard:

  • TFT Display VCC pin → Breadboard 5V line

  • TFT Display GND pin → Breadboard ground line

 • TFT Display LED pin → Breadboard 5V line


Connections from the l298n motor driver to arduino :

  • TFT Display CS pin → Arduino pin 10

  • TFT Display RST pin → Arduino pin 8

  • TFT Display DC pin → Arduino pin 9

  • TFT Display MOSI pin → Arduino pin 11

  • TFT Display SCK pin → Arduino pin 13

  • TFT Display MISO pin → Arduino pin 12


Coding

/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:

Tutorial: Make an Automatic Gate opener using Arduino!
Wire Library:
Adafruit_GFX Library:
Wire Library:


The purpose of this sketch is to open the gate automatically when something passes in front of the
ultrasonic sensorand display a message on the TFT Display that says ‘object detected, gate will open’.

Connections from the Arduino to the breadboard:

• Arduino GND pin → Breadboard ground line

• Arduino 5V pin → Breadboard 5V line


Connections from the servo motor:

• Servo GND pin→ Arduino GND pin

• Servo VCC pin→ Arduino VCC pin

• Servo signal pin → Arduino pin 4


Connections from the ultrasonic sensor:

• ultrasonic sensor VCC pin → Breadboard 5V line

• ultrasonic sensor GND pin → Breadboard ground line

• ultrasonic sensor Trig pin → Arduino pin 3

• ultrasonic sensor Echo pin → Arduino pin 2


Connections from the TFT Display to the breadboard:

• TFT Display VCC pin → Breadboard 5V line

• TFT Display GND pin → Breadboard ground line

• TFT Display LED pin → Breadboard 5V line


Connections from the l298n motor driver to arduino :

• TFT Display CS pin → Arduino pin 10

• TFT Display RST pin → Arduino pin 8

• TFT Display DC pin → Arduino pin 9

• TFT Display MOSI pin → Arduino pin 11

• TFT Display SCK pin → Arduino pin 13

• TFT Display MISO pin → Arduino pin 12


*/

#include "Adafruit_GFX.h"    
#include "Adafruit_ILI9341.h"
#include "Wire.h"
#include  "SPI.h"
#include "Servo.h"
Servo servoMain; // Define our Servo
//Define pin numbers
#define TFT_DC 9              
#define TFT_CS 10            
#define TFT_RST 8            
#define TFT_MISO 12          
#define TFT_MOSI 11          
#define TFT_CLK 13          

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

char DISTANCEChar[10];

//Define pin numbers
#define trig 3
#define echo 2
//Defines variables and initialize their values
float distance =0;
int t =0;
 
void setup(){
 
Serial.begin(9600);
servoMain.attach(4);
tft.begin();                      
tft.setRotation(0);            
tft.fillScreen(ILI9341_GREEN);
//Sets the trig Pin as an output
pinMode(trig,OUTPUT);
//Sets the echo Pin as an intput
pinMode(echo,INPUT);
Wire.begin();


}

void loop()
{
//Clears the trig pin
digitalWrite(trig,LOW);
//delay for 5 micro seconds
delayMicroseconds(5);
//Sets the trig Pin HIGH for 10 micro seconds
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
//Reads the echoPin, returns the sound wave travel time in microseconds
t =pulseIn(echo,HIGH);
//Calculating the distance
distance =(t /57);
//Draw a Rectangle
tft.fillRect(0,175,240,35,ILI9341_RED);

if(distance<60)
 {
   //printing the state value on the TFT Display
   printText("object detected", ILI9341_WHITE,30,176,2);
   printText("gate will open ", ILI9341_WHITE,35,195,2);
   servoMain.write(180);  // Turn Servo back to center position (90 degrees)
  delay(3000);
 }
  else{
    //printing the state value on the TFT Display
    printText("Nothing detected ", ILI9341_WHITE,30,176,2);
    servoMain.write(0);
    delay(50);
  }
 


}
//The function of writing words on the TFT Display
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
 tft.setCursor(x, y);
 tft.setTextColor(color);
 tft.setTextSize(textSize);
 tft.setTextWrap(true);
 tft.print(text);
}

Testing it Out

Once you’ve uploaded the code to the Arduino board, you will find that the gate automatically opens when something passes in front of the ultrasonic sensor and displays a message on the TFT Display that says ‘object detected, gate will open’ as shown in the video below:

Resources

No items found.