Arduino Parking Garage Barrier with Sensors
Print Profile(1)

Description
Project Description – Automatic Barrier System
This project is a fully automatic barrier system with an ultrasonic sensor, laser, and servo. If the ultrasonic sensor detects a car, the servo opens the barrier, and the LED switches from red to green. As long as the car is under the barrier and interrupts the laser, the barrier remains open. Once the vehicle drives away and the laser receiver is clear again, the barrier automatically lowers, and the LED switches back to red.
Parts List
• 2× LEDs (red & green)
• 1× ultrasonic sensor
• 1× servo motor
• 1× laser transmitter
• 1× laser receiver
• 1× Arduino UNO R4
• 1× breadboard
• various jumper wires
Setup Note
For the base, you can use a self-designed road, elements printed from Bambu Studio, or simply cardboard. This allows the project to be flexibly mounted anywhere.
Help & Support
For programming, pin assignment, or setup, I'm happy to help on Instagram:
👉 @layerbylayer.3d.cad
Program
The complete Arduino program follows below.
Refer to the code directly for the pin numbers or adjust them as needed.
#include <Servo.h>
// --- Pins ---
const int ULTRA_TRIG = 7; // Ultrasonic Trigger
const int ULTRA_ECHO = 6; // Ultrasonic Echo
const int LASER_RX = 4; // Laser Receiver
const int SERVO_PIN = 5; // Barrier Servo
const int LED_R = 8; // Red
const int LED_G = 9; // Green
// --- Servo Angles ---
const int SERVO_DOWN = 0;
const int SERVO_UP = 90;
// --- Ultrasonic Thresholds (cm) ---
const long ULTRA_THRESHOLD = 10; // Car detected
const long ULTRA_CLEAR = 15; // Car gone (Cycle 3)
// --- Servo ---
Servo barrier;
// --- Barrier State ---
enum GateState { CLOSED, OPEN };
GateState gateState = CLOSED;
// --- Ultrasonic Measurement ---
long getDistance() {
digitalWrite(ULTRA_TRIG, LOW);
delayMicroseconds(5);
digitalWrite(ULTRA_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRA_TRIG, LOW);
long duration = pulseIn(ULTRA_ECHO, HIGH, 30000);
long distance = duration * 0.034 / 2; // cm
if (distance == 0) return 999; // no signal
return distance;
}
void setup() {
pinMode(ULTRA_TRIG, OUTPUT);
pinMode(ULTRA_ECHO, INPUT);
pinMode(LASER_RX, INPUT);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
barrier.attach(SERVO_PIN);
// Start: Barrier down, LED red
barrier.write(SERVO_DOWN);
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, LOW);
gateState = CLOSED;
}
void loop() {
long distance = getDistance();
bool laserBlocked = (digitalRead(LASER_RX) == HIGH); // HIGH = Laser interrupted
// --- Cycle 1: Car detected in front of barrier ---
if (gateState == CLOSED && distance < ULTRA_THRESHOLD) {
gateState = OPEN;
barrier.write(SERVO_UP);
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
delay(300); // Stabilization
}
// --- Cycle 2: Car under barrier, laser blocked ---
if (gateState == OPEN && laserBlocked) {
// Barrier remains up, LED green
digitalWrite(LED_G, HIGH);
digitalWrite(LED_R, LOW);
return; // no change
}
// --- Cycle 3: Car drives away, laser clear again ---
if (gateState == OPEN && !laserBlocked && distance > ULTRA_CLEAR) {
gateState = CLOSED;
barrier.write(SERVO_DOWN);
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, LOW);
delay(300); // Stabilization
}
delay(50); // Short pause
}
License
You shall not share, sub-license, sell, rent, host, transfer, or distribute in any way the digital or 3D printed versions of this object, nor any other derivative work of this object in its digital or physical format (including - but not limited to - remixes of this object, and hosting on other digital platforms). The objects may not be used without permission in any way whatsoever in which you charge money, or collect fees.






Comment & Rating (2)