Projects / Model boom barrier

Model boom barrier

Beginner2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO59°

    Wire the servo

    Red to 5V, brown to GND, orange to D9.

  2. NANO

    Wire the sensor

    VCC and GND, Trig to D7, Echo to D6.

  3. NANO

    Wire the lights

    Red through 220 ohms to D4, green through 220 ohms to D5, short legs to GND.

  4. NANO59°

    Fix the arm

    Light stick on the servo horn, with the servo mounted on a card post.

  5. NANO

    Upload the sketch

    The arm is down and the red light is on.

  6. NANO

    Roll a toy car in front of the sensor

    The arm lifts and the light turns green.

  7. NANO

    Wait

    The arm lowers after the car has gone.

In short

How do you build a model boom barrier?

A servo lifts a model barrier arm when an ultrasonic sensor sees a toy car coming, shows red and green lights, and lowers the arm after the car has passed. You need Arduino Nano, SG90 Micro Servo, HC-SR04 Ultrasonic Sensor, LED 5 mm red, LED 5 mm green, Resistor 220 Ω × 2 and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.

Level
Beginner
Time
About 140 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Servo redNano 5VServo supply
Servo brown or blackNano GNDCommon ground
Servo orange or yellowNano D9Servo signal
HC-SR04 VCCNano 5VPower at 5 V
HC-SR04 GNDNano GNDCommon ground
HC-SR04 TrigNano D7Start a ping
HC-SR04 EchoNano D6Echo time
Red LED long leg220 ohm resistor to Nano D4Closed light
Green LED long leg220 ohm resistor to Nano D5Open light
Both LED short legsNano GNDReturn

Code

This is a small model of a car park or level-crossing barrier. The HC-SR04 sends out a burst of ultrasound and times the echo. The distance in centimetres is the echo time in microseconds divided by about 58. When something comes within 15 centimetres, the program lights the green lamp, turns off the red one and lifts the arm with the servo to 90 degrees, one degree at a time so it moves smoothly like a real barrier. The barrier stays open while the car is in front of the sensor, and when the way has been clear for two seconds, which is longer than a toy car takes to roll past, it lowers the arm and shows red again. The two distances, 15 and 25 centimetres, are different to stop the barrier chattering at the edge. Make the arm from a light stick or card, because an SG90 servo is small. This is a model for play and learning: do not use it to control a real gate, because a real barrier needs safety sensors and proper design. The servo is powered from the Nano's 5 volt pin, which is fine for one small servo over USB.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Servo.h>

const int TRIG = 7, ECHO = 6, SERVO_PIN = 9, RED = 4, GREEN = 5;
const int CLOSED_ANGLE = 0, OPEN_ANGLE = 90;
const int NEAR_CM = 15;                            // a car closer than this opens the barrier
const int CLEAR_CM = 25;                           // farther than this counts as gone

Servo arm;
int angle = CLOSED_ANGLE;
bool isOpen = false;
unsigned long clearSince = 0;

long distanceCm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long t = pulseIn(ECHO, HIGH, 25000);             // time out at about 4 m
  if (t == 0) return 400;
  return t / 58;                                   // microseconds to centimetres
}

void moveArm(int target) {
  while (angle != target) {
    angle += (angle < target) ? 1 : -1;
    arm.write(angle);
    delay(15);                                     // slow, smooth movement
  }
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  arm.attach(SERVO_PIN);
  arm.write(CLOSED_ANGLE);
  digitalWrite(RED, HIGH);
}

void loop() {
  long d = distanceCm();

  if (!isOpen && d < NEAR_CM) {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    moveArm(OPEN_ANGLE);
    isOpen = true;
    clearSince = 0;
  } else if (isOpen) {
    if (d > CLEAR_CM) {
      if (clearSince == 0) clearSince = millis();
      if (millis() - clearSince > 2000) {          // clear for two seconds: car has passed
        moveArm(CLOSED_ANGLE);
        digitalWrite(GREEN, LOW);
        digitalWrite(RED, HIGH);
        isOpen = false;
      }
    } else {
      clearSince = 0;                              // car still there
    }
  }
  delay(100);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
StartArm down, red light
Hand in front of the sensorArm lifts, green light
Keep the hand thereArm stays up
Take the hand awayAfter two seconds the arm lowers
Repeat ten timesSame result every time

If it does not work

Opens by itself

Aim the sensor away from walls and increase the near distance check.

Never opens

Check Trig on D7 and Echo on D6, and that the sensor has 5 V.

Servo jitters

Use a better USB port, and keep the servo wires short.

Arm hits the post

Change the two angles in the sketch.

Closes too soon

Lengthen the two-second wait.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the model boom barrier?

Arduino Nano, SG90 Micro Servo, HC-SR04 Ultrasonic Sensor, LED 5 mm red, LED 5 mm green, Resistor 220 Ω × 2 and Breadboard 830 Tie Points.

How long does the model boom barrier take to build?

About 140 minutes for a beginner maker, in 7 steps.

How do I know the model boom barrier works?

Start: Arm down, red light. Hand in front of the sensor: Arm lifts, green light. Keep the hand there: Arm stays up.

Opens by itself: what should I check?

Aim the sensor away from walls and increase the near distance check.

Never opens: what should I check?

Check Trig on D7 and Echo on D6, and that the sensor has 5 V.

Servo jitters: what should I check?

Use a better USB port, and keep the servo wires short.

Arm hits the post: what should I check?

Change the two angles in the sketch.