Skip to content
FlintMindsHigh School
High School

Middle School

Automatic Parking Barrier

Combine a sensor and a servo to build a gate that opens automatically when a car arrives.

Level
Builder
Time
1 hr
Parts
6
Hardware-checked
Not yet

What this build assumes

  • Measuring distance
  • Calibrating a threshold

0 of 5 build milestones complete

What this build is

Real parking garages open their gate automatically once a sensor detects a car waiting.

This project combines the distance sensing from your last build with the servo motion from your first to create one automated machine.

How it works

System architecture

  1. Input

    The sensor detects a car has arrived.

  2. Decision

    Your code checks its state so it doesn't reopen an already-open barrier.

  3. Output

    The servo raises the barrier, then lowers it after a delay.

Parts

Microcontroller board × 1

Reads the sensor and decides when to move the barrier.

Micro servo motor × 1

Lifts and lowers the barrier arm.

Ultrasonic distance sensor × 1

Detects when a car (or hand) has arrived at the gate.

Barrier arm (craft stick or included arm piece) × 1

Acts as the physical gate attached to the servo.

Breadboard × 1

Holds the sensor and servo wiring in place.

Jumper wires × a handful

Wire the sensor and servo to the board.

Build

  1. 1

    Attach the barrier arm to the servo

    Fix the barrier arm to the servo horn so it swings up when the servo rotates.

    Barrier arm attachment diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  2. 2

    Mount the distance sensor at the gate

    Position the sensor so it faces where a car (or your hand) will approach.

    Sensor placement diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  3. 3

    Wire the sensor and servo

    Connect the sensor's and servo's power, ground, and signal pins to your board.

    Sensor and servo wiring diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  4. 4

    Upload the automation code

    Connect your board to your computer and upload the code below.

  5. 5

    Test the open/close timing

    Trigger the sensor and confirm the barrier opens, holds, then closes on its own.

Wiring

Automatic Parking Barrier wiring diagram

Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

The distance sensor's power/ground connect to 5V/GND, with trigger and echo on two digital pins. The servo's power/ground also connect to 5V/GND (or a separate supply if it draws more current than your board can provide), with its signal wire on a PWM-capable digital pin — match all pins in your code below.

Code

arduino
1#include <Servo.h>2 3Servo barrier;4const int servoPin = 9;   // update to match your wiring5const int trigPin = 10;   // update to match your wiring6const int echoPin = 11;   // update to match your wiring7const int openThresholdCm = 10;8 9bool isOpen = false;10 11void setup() {12  barrier.attach(servoPin);13  pinMode(trigPin, OUTPUT);14  pinMode(echoPin, INPUT);15  barrier.write(0);16}17 18void loop() {19  digitalWrite(trigPin, LOW);20  delayMicroseconds(2);21  digitalWrite(trigPin, HIGH);22  delayMicroseconds(10);23  digitalWrite(trigPin, LOW);24 25  long duration = pulseIn(echoPin, HIGH);26  long distanceCm = duration * 0.034 / 2;27 28  if (distanceCm < openThresholdCm && !isOpen) {29    barrier.write(90);30    isOpen = true;31    delay(3000);32    barrier.write(0);33    isOpen = false;34  }35}36 
  • Line 9: isOpen tracks the barrier's current state, so the code only opens it once per car instead of repeatedly.
  • Line 31: This delay is the hold-open time — how long the barrier stays open before automatically closing.

Expected behaviour

  1. 1Upload the code and bring your hand close to the sensor, like a car arriving.
  2. 2The barrier should swing open and hold for a few seconds, then close automatically.
  3. 3Confirm it doesn't try to reopen while already open.

Diagnosis

Symptoms and their usual causes. Work down the list — each check rules out a subsystem.

The barrier doesn't open.
Verify trigPin and echoPin match your sensor wiring, and that openThresholdCm fits your test distance.
The barrier opens and closes rapidly.
This usually means isOpen isn't being tracked correctly — make sure that logic wasn't accidentally removed.
The servo strains or resets the board.
The servo may need its own power source rather than pulling current from the same board as the sensor.

Open problems

Extensions

Small change

Add a hold-open delay

Keep the barrier open for a few seconds so a car has time to pass fully.

Add something

Add a warning blink

Blink an LED for a moment before the barrier closes.

Design it yourself

Count how many cars pass

Keep a running count each time the barrier opens and closes.

You decide how it works. No steps for this one.

Check your understanding

1. What two building blocks does this project combine?

2. Why does the barrier need to track whether it's currently open or closed?

3. What is automation, in the context of this project?

Where this leads

You now know:

  • Reading a sensor's input
  • Controlling a motor with code
  • Using conditional (if/else) logic
  • Tracking state across a program
  • Combining sensor input with motor output
  • Designing automated behavior
  • What this leads to

    Line-Following Robot

    Automatic Parking Barrier leads straight into this one.

  • What this leads to

    Smart Home

    Automatic Parking Barrier leads straight into this one.

The engineering brief for this project has not been written yet.

The problem statement, the constraints, the design decisions and the validation procedure are the parts of a project that make it worth an engineering student’s time, and this one does not have them yet. Writing them means building it with real components and measuring what it does — so rather than approximate, we are telling you they are missing. Why we do this.