⭐⭐⭐ Challenge ⏱️ 30 minutes

Automatic barrier

The smart version of the barrier! The distance sensor sees that a car is coming. Then the buzzer beeps and the barrier opens by itself. Is the car gone? Then it closes again.

First you log in for a moment. Then your project is ready right away.

🧰 What do you need?

🗺️ Wiring diagram

Follow the wires from the Arduino to each part. You can choose the wire colors yourself.

From To
Arduino UNO · D9 Distance sensor HC-SR04 · Trig
Arduino UNO · D10 Distance sensor HC-SR04 · Echo
Arduino UNO · 5V Distance sensor HC-SR04 · Vcc
Arduino UNO · GND Distance sensor HC-SR04 · Gnd
Arduino UNO · D5 Servo motor SG90 · PWM
Arduino UNO · 5V Servo motor SG90 · VCC
Arduino UNO · GND Servo motor SG90 · GND
Arduino UNO · D8 Active buzzer · S
Arduino UNO · 5V Active buzzer · +
Arduino UNO · GND Active buzzer · -

🛠️ This is how you build it

  1. Connect the HC-SR04: Trig to pin 9, Echo to pin 10, Vcc to 5V and Gnd to GND.
  2. Connect the servo: orange to pin 5, red to 5V and brown to GND.
  3. Connect the buzzer: S to pin 8, + to 5V and − to GND.
  4. Stick a straw or popsicle stick on the servo as the barrier and put the sensor in front of it.

💻 The code

// Automatic barrier
// Is a car coming (something closer than 15 cm)? Then the buzzer beeps and the barrier opens.

#include <Servo.h>

const int TRIG_PIN = 9;       // Trig of the HC-SR04 on pin 9
const int ECHO_PIN = 10;      // Echo of the HC-SR04 on pin 10
const int SERVO_PIN = 5;      // Orange/yellow wire of the servo on pin 5
const int BUZZER_PIN = 8;     // S of the buzzer on pin 8
const int CLOSE_BY = 15;      // From this distance (cm), the barrier opens

const int CLOSED = 0;         // Angle when the barrier is closed
const int OPEN = 90;          // Angle when the barrier is open

Servo barrier;

long measureDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long time = pulseIn(ECHO_PIN, HIGH, 30000);
  if (time == 0) return 999;
  return time / 58;
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  barrier.attach(SERVO_PIN);
  barrier.write(CLOSED);
}

void loop() {
  long distance = measureDistance();

  if (distance < CLOSE_BY) {
    Serial.println(F("Car seen! Barrier open."));
    digitalWrite(BUZZER_PIN, HIGH);   // Short beep
    delay(100);
    digitalWrite(BUZZER_PIN, LOW);

    barrier.write(OPEN);
    delay(1000);
    // Stay open while the car is still there
    while (measureDistance() < CLOSE_BY) {
      delay(200);
    }
    delay(2000);                      // Wait a bit more until the car is really gone
    barrier.write(CLOSED);
    Serial.println(F("Barrier closed."));
  }
  delay(100);
}

🧠 What does the code do?

  • The HC-SR04 keeps measuring the distance. Closer than 15 cm? Then there is a car.
  • The buzzer beeps shortly and the servo turns to 90 degrees: open.
  • As long as the car is there, the barrier stays open. Then we wait 2 seconds and it closes.

🎯 Try this…

  • Count how many cars have gone past and show it in the serial monitor
  • Add a red and a green LED as a traffic light by the barrier
  • Only let the barrier open when a button is also pressed

🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/automatic-barrier and click MAKE THIS. Then the code and the wiring diagram are ready right away.

First you log in for a moment. Then your project is ready right away.
⬇️ Download this page as a PDF 📚 Download the complete tutorial (PDF)

Codey Online is a product of OTRONIC

Build your Arduino or ESP32 project with AI

Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.

Start for free