Skip to content
FlintMindsMiddle School
Middle 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

0 of 5 build milestones complete

What are we building?

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.

The mechanism

How it works

  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.

What you'll pick up

  • Combine a sensor input with a motor output in one program
  • Use state-based logic to track whether the barrier is open or closed
  • Add a hold delay so the barrier stays open long enough
  • Design a small automation from two simpler building blocks

What you need

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 it

  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.

Does it work?

  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.

Make it yours

What can you change?

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.

If something's off

The usual causes, in the order they're worth checking.

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.

Do you know why it works?

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?

What to build next

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.