Projects / Ultrasonic room intrusion

Ultrasonic room intrusion

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the sensor and buzzer

    Connect as in the table.

  2. NANO

    Test the distance

    Print distances and check them against a tape measure.

  3. NANO

    Upload the sketch

    Press the button and leave the room in the 10 seconds.

  4. NANO

    Check the baseline

    Read it on the serial monitor.

  5. NANO

    Walk in

    The alarm should start after about a second.

  6. NANO

    Disarm

    Press the button.

  7. NANO

    Tune the change

    Adjust CHANGE_CM for your room.

In short

How do you build an ultrasonic room intrusion?

An ultrasonic sensor learns the room's usual distance and alarms on change. You need Arduino Nano, HC-SR04 Ultrasonic Sensor, Active buzzer 5 V and Tactile push button 6 mm. It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
4

Parts list

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

Wiring

FromToWhy
HC-SR04 VCCNano 5VPower
HC-SR04 GNDNano GNDGround
HC-SR04 TRIGNano D9Trigger pulse
HC-SR04 ECHONano D10Echo pulse
Active buzzer +Nano D6Alarm sound
Active buzzer -Nano GNDGround
ButtonNano D2 and GNDArm and disarm

Code

The sensor measures the distance to the nearest object in a narrow cone, so point it across the doorway or at the opposite wall. After you press the button there is a 10 second exit delay, then the sketch takes the middle of 15 readings as the baseline. A change of more than 20 cm on three checks in a row sounds the alarm, which avoids false alarms from a single bad reading. Soft furnishings and angled surfaces can absorb the sound, so test with the real room. This is a learning project and not a replacement for a proper alarm system. Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones).

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

// Learns the room distance when armed, alarms if it changes by a lot.
const byte TRIG = 9, ECHO = 10, BUZZ = 6, BTN = 2;
const int CHANGE_CM = 20;              // how big a change counts
const unsigned long EXIT_MS = 10000;   // time to leave the room after arming

enum State { DISARMED, ARMING, ARMED, ALARM };
State state = DISARMED;
unsigned long armStart = 0;
float baseline = 0;
byte hits = 0;
bool lastBtn = HIGH;

float distanceCm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(3);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long d = pulseIn(ECHO, HIGH, 30000UL);
  if (d == 0) return 400;              // no echo, treat as far away
  return d * 0.0343 / 2.0;
}

float median15() {
  float v[15];
  for (byte i = 0; i < 15; i++) { v[i] = distanceCm(); delay(40); }
  for (byte i = 0; i < 15; i++) for (byte j = i + 1; j < 15; j++) if (v[j] < v[i]) { float t = v[i]; v[i] = v[j]; v[j] = t; }
  return v[7];
}

void beep(int ms) { digitalWrite(BUZZ, HIGH); delay(ms); digitalWrite(BUZZ, LOW); }

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  pinMode(BUZZ, OUTPUT); pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  bool b = digitalRead(BTN);
  if (b == LOW && lastBtn == HIGH) {
    delay(30);
    if (state == DISARMED) { state = ARMING; armStart = millis(); beep(100); Serial.println("arming"); }
    else { state = DISARMED; digitalWrite(BUZZ, LOW); beep(300); Serial.println("disarmed"); }
  }
  lastBtn = b;

  if (state == ARMING && millis() - armStart > EXIT_MS) {
    baseline = median15();
    hits = 0; state = ARMED; beep(100); delay(100); beep(100);
    Serial.print("armed, baseline cm "); Serial.println(baseline);
  }
  if (state == ARMED) {
    float d = distanceCm();
    if (fabs(d - baseline) > CHANGE_CM) hits++; else hits = 0;
    if (hits >= 3) { state = ALARM; Serial.println("ALARM"); }
    delay(300);
  }
  if (state == ALARM) {
    digitalWrite(BUZZ, (millis() / 250) % 2);   // pulsing alarm until the button is pressed
  }
}

Test checklist

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

CheckExpected
Distance printedWithin a few cm of the tape measure
Walking across the beamAlarm after three checks
Button during alarmAlarm stops
Small movement far from the beamNo alarm

If it does not work

Always alarms

Check the sensor sees a solid surface and raise CHANGE_CM.

Never alarms

Aim the sensor across the path and lower CHANGE_CM.

Distance jumps

Soft or angled surfaces; aim at a wall.

Buzzer silent

Check it is an active buzzer on D6.

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

Ask Lark AI

Common questions

What parts do I need for the ultrasonic room intrusion?

Arduino Nano, HC-SR04 Ultrasonic Sensor, Active buzzer 5 V and Tactile push button 6 mm.

How long does the ultrasonic room intrusion take to build?

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

How do I know the ultrasonic room intrusion works?

Distance printed: Within a few cm of the tape measure. Walking across the beam: Alarm after three checks. Button during alarm: Alarm stops.

Always alarms: what should I check?

Check the sensor sees a solid surface and raise CHANGE_CM.

Never alarms: what should I check?

Aim the sensor across the path and lower CHANGE_CM.

Distance jumps: what should I check?

Soft or angled surfaces; aim at a wall.

Buzzer silent: what should I check?

Check it is an active buzzer on D6.