Barrier Gate 3.0
Note
🌟 Welcome to the SunFounder Facebook Community! Whether you’re into Raspberry Pi, Arduino, or ESP32, you’ll find inspiration, help ideas here.
✅ Be the first to get free learning resources.
✅ Stay updated on new products & exclusive giveaways.
✅ Share your creations and get real feedback.
👉 Need faster updates or support? Click [here] join our Facebook community
👉 Or join our WhatsApp group: Click [here]
Kit purchase
Looking for parts? Check out our all-in-one kits below — packed with components, beginner-friendly guides, and tons of fun.
Name |
Includes Arduino board |
PURCHASE LINK |
|---|---|---|
Ultimate Sensor Kit |
Arduino Uno R4 Minima |
|
Elite Explorer Kit |
Arduino Uno R4 WiFi |
|
3 in 1 Ultimate Starter Kit |
Arduino Uno R4 Minima |
|
Universal Maker Sensor Kit |
× |
Course Introduction
In this lesson, you’ll use a button, a traffic light LED module, and a servo motor with the Arduino to simulate a manual barrier gate system.
When the button is pressed, the red light turns off, the green light turns on, and the gate opens briefly to let a car pass.
Note
If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.
Required Components
In this project, we need the following components:
SN |
COMPONENT INTRODUCTION |
QUANTITY |
PURCHASE LINK |
|---|---|---|---|
1 |
Arduino UNO R4 Minima/Arduino UNO R4 WIFI |
1 |
|
2 |
USB Type-C cable |
1 |
|
3 |
Breadboard |
1 |
|
4 |
Wires |
Several |
|
5 |
Button |
1 |
|
6 |
Digital Servo Motor |
1 |
|
7 |
LED |
4 |
|
8 |
1kΩ resistor |
4 |
Wiring
Common Connections:
LED
Connect the LEDs cathode to the negative power bus on the breadboard, and the LEDs anode to a 1kΩ resistor then to 3, 4 on the Arduino.
Digital Servo Motor
Connect to breadboard’s positive power bus.
Connect to breadboard’s negative power bus.
Connect to 11 on the Arduino.
Button
Connect to breadboard’s negative power bus.
Connect to 2 on the Arduino.
Writing the Code
Note
You can copy this code into Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 Minima/WIFI) and the correct port before clicking the Upload button.
#include <Servo.h>
const int buttonPin = 2; // Button connected to digital pin 2
const int greenLed = 3; // Green LED connected to digital pin 3
const int redLed = 4; // Red LED connected to digital pin 4
const int servoPin = 12; // Servo signal connected to digital pin 12
Servo barrierServo;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up on button pin
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
barrierServo.attach(servoPin); // Attach servo to pin 12
barrierServo.write(0); // Start with barrier closed (0°)
digitalWrite(redLed, HIGH); // Turn on red LED initially
digitalWrite(greenLed, LOW); // Turn off green LED initially
Serial.begin(9600); // For debugging
}
void loop() {
// Check if button is pressed (active LOW)
if (digitalRead(buttonPin) == LOW) {
Serial.println("Button pressed: Opening barrier");
digitalWrite(redLed, LOW); // Turn off red LED
digitalWrite(greenLed, HIGH); // Turn on green LED
barrierServo.write(90); // Raise barrier to 90°
delay(2000); // Wait for 2 seconds
barrierServo.write(0); // Lower barrier back to 0°
digitalWrite(greenLed, LOW); // Turn off green LED
digitalWrite(redLed, HIGH); // Turn on red LED
}
delay(100); // Short delay to debounce
}