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

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

BUY

4

Wires

Several

5

1kΩ resistor

4

6

Button

1

7

LED

4

8

Digital Servo Motor

1

Wiring

7.webp__PID:013bc042-c1b7-4418-9c95-392c761b6e17

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
}