Arduino Project
Barrier Gate 3.0
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.
1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE
Wiring

Common Connections:
LED
Connect the LEDs cathode to a 1kΩ resistor then to the negative power bus on the breadboard, and the LEDs anode 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 WIFI) and the correct port before clicking the Upload button.
#include
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
}
