Skip to content
FlintMindsElementary

Automatic Parking Barrier

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

About 1 hr

0 of 5 steps done

What are we making?

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.

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.

Let's 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.

How it all connects

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.

The 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.

Try it!

  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.

Not working yet?

Nothing works the first time for everyone. Here's what to check.

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.

Try changing it

Add a hold-open delay

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

Add a warning blink

Blink an LED for a moment before the barrier closes.

Count how many cars pass

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

Do you know how 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 should you make next?

You just learned how to:

  • 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
  • Line-Following Robot

    Build a robot that steers itself by continuously reading sensors and following a taped line.

  • Smart Home

    Build a scaled-down smart home model that combines sensors, status indicators, and an automated entry barrier into one coordinated system.