Built-in project · Arduino Uno R3

Parking barrier on an Arduino Uno

An HC-SR04 sees a car pull up to the line, the traffic light goes green and an MG996R lifts the barrier, then lowers it once the car has gone. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

Parking barrier · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Parking barrier: an HC-SR04, a traffic light module and an MG996R servo.
//
//   pins 9, 8     the HC-SR04's TRIG and ECHO
//   pins 6, 5, 4  the light's R, Y and G (resistors on its board)
//   pin 3         the MG996R's signal; the servo has its own 6 V supply,
//                 sharing ground with the Uno
//
// The barrier starts down and red. A car pulling up to within 20 cm turns
// the light yellow, lifts the arm to 90 degrees and turns it green. Once the
// lane has been clear (over 30 cm) for two seconds the light goes yellow,
// the arm comes down and it is red again. The gap between 20 and 30 cm keeps
// a car sitting right at the line from flapping the barrier.

#include <Servo.h>

const int TRIG = 9, ECHO = 8;
const int RED_PIN = 6, YELLOW_PIN = 5, GREEN_PIN = 4;
const int ARM = 3;

const long ARRIVE_CM = 20;
const long CLEAR_CM = 30;
const unsigned long CLEAR_MS = 2000;

Servo arm;
bool open = false;
unsigned long clearSince = 0;

void lights(bool r, bool y, bool g) {
  digitalWrite(RED_PIN, r ? HIGH : LOW);
  digitalWrite(YELLOW_PIN, y ? HIGH : LOW);
  digitalWrite(GREEN_PIN, g ? HIGH : LOW);
}

long distanceCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000UL);
  if (us == 0) return 400;
  return (long)(us / 58);
}

void swing(int from, int to) {
  int step = to > from ? 1 : -1;
  for (int a = from; a != to; a += step) {
    arm.write(a);
    delay(8);
  }
  arm.write(to);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: parking barrier");
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  arm.attach(ARM);
  arm.write(0);
  lights(true, false, false);
  Serial.println("barrier down");
}

void loop() {
  long cm = distanceCm();
  if (!open && cm <= ARRIVE_CM) {
    Serial.print("car at ");
    Serial.print(cm);
    Serial.println(" cm: lifting");
    lights(false, true, false);
    swing(0, 90);
    lights(false, false, true);
    open = true;
    clearSince = millis();
    Serial.println("barrier up");
  } else if (open) {
    if (cm <= CLEAR_CM) {
      clearSince = millis();
    } else if (millis() - clearSince > CLEAR_MS) {
      Serial.println("lane clear: lowering");
      lights(false, true, false);
      swing(90, 0);
      lights(true, false, false);
      open = false;
      Serial.println("barrier down");
    }
  }
  delay(100);
}

Parts list

4 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

How it is wired

9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 9; Parking barrier pin TRIG
  • Arduino Uno R3 pin 8; Parking barrier pin ECHO
  • Arduino Uno R3 pin 6; Parking barrier pin R
  • Arduino Uno R3 pin 5; Parking barrier pin Y
  • Arduino Uno R3 pin 4; Parking barrier pin G
  • Arduino Uno R3 pin 3; Parking barrier pin ARM
  • Arduino Uno R3 pin 5V; Parking barrier pin VCC
  • Ground: Arduino Uno R3 pin GND; Parking barrier pin GND
  • 6 V: Parking barrier pin V+

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.