Arduino-Based Automatic Gate with PIR Sensor & Servo Motor

Hello and welcome back! In this project, we will learn how to make an automatic gate control system using Arduino. For this, I have mainly used a PIR motion sensor to detect motion. When motion is detected, an SG90 servo motor automatically opens and closes the gate. To indicate the gate status, I have used a traffic light module, which makes it easier to know whether the gate is open or closed.
This project is easy to build with just a few steps and requires a low budget, making it perfect for beginners. I used an Arduino Nano board, but you can use any other Arduino board that fits your needs. Also, you can modify it by adding a buzzer for sound alerts.
Ok, let’s do this project step by step. The required components are given below.
- Arduino Nano board x 1 — Our Store / Amazon
- PIR sensor x 1 — Our Store / Amazon
- Traffic light module x 1 — Our Store / Amazon
- Jumper wires — Our Store / Amazon
- Servo motor x 1 — Our Store / Amazon
- Breadboard — Our store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.






Step 2
Secondly, place the Arduino Nano board and the traffic light module onto the breadboard.


Step 3
Then, connect the traffic light module and PIR sensor to the Arduino board using the circuit diagram below.







Step 4
Next, calibrate the servo motor and connect it to the Arduino Nano board. Afterward, connect the Arduino board to the computer.




Step 5
Now, copy and paste the following program into the Arduino IDE. Then, adjust the servo motor values to match your servo motor’s position.
- Code and Circuit Diagram – Download
#include <Servo.h>
#define R 2 // Red LED (Motion Detected)
#define Y 3 // Yellow LED (Closing Warning)
#define G 4 // Green LED (Gate Open)
#define sensor 5 // PIR Sensor Input
Servo servo;
void setup() {
Serial.begin(9600);
pinMode(R, OUTPUT);
pinMode(Y, OUTPUT);
pinMode(G, OUTPUT);
pinMode(sensor, INPUT);
servo.attach(9);
servo.write(110); // Initial position (Closed)
}
// Function to move servo smoothly
void smoothServoMove(int start, int end, int stepDelay) {
if (start < end) {
for (int pos = start; pos <= end; pos++) {
servo.write(pos);
delay(stepDelay);
}
} else {
for (int pos = start; pos >= end; pos--) {
servo.write(pos);
delay(stepDelay);
}
}
}
// Function to blink yellow LED before closing
void blinkWarningLED() {
for (int i = 0; i < 5; i++) {
digitalWrite(Y, HIGH);
delay(300);
digitalWrite(Y, LOW);
delay(300);
}
}
void loop() {
bool value = digitalRead(sensor);
Serial.println(value);
if (value == HIGH) {
delay(500); // Double-check motion to avoid false triggers
if (digitalRead(sensor) == HIGH) {
digitalWrite(R, HIGH); // Motion detected
smoothServoMove(110, 180, 30); // Open gate smoothly
digitalWrite(G, HIGH); // Gate is open
digitalWrite(R, LOW);
delay(3000); // Keep gate open for 2 seconds
digitalWrite(G, LOW);
blinkWarningLED(); // Blink yellow LED before closing
digitalWrite(R, HIGH);
smoothServoMove(180, 110, 30); // Close gate smoothly
digitalWrite(R, LOW);
}
}
delay(1000);
}
- Next, select the board and port, then click the upload button.




Step 6
Afterward, remove the USB cable and prepare the servo motor mount stand. For that, you can use the following sizes. I used a piece of foam board for this.







Step 7
Next, prepare the gate part using a piece of foam board. Then, I added a design to this part (lol).



Step 8
Now, install it on the servo horn. Finally, connect the external power supply to the system.



OK! You can now test this project using a Bluetooth-controlled car or a toy car. The full video guide is below. We hope to see you in the next project! Have a great day.
Troubleshooting tips
- Check the wiring.
- Adjust the PIR sensor sensitivity.
- Calibrate your servo motor.
- Enter the correct servo values.
- Select the correct board and port.
- Check the Power supply.
Arduino Based Automatic Gate with PIR Sensor & Servo Motor