Tuesday, 1 September 2026

Practical build

An object-detection alarm with an ultrasonic sensor and buzzer

A beginner IoT circuit: an HC-SR04 distance sensor watches the space in front of it, and a buzzer sounds the moment something enters range.

SENSOR ARDUINO BUZZER

What you'll need

  • 01 Arduino Uno (or compatible board)
  • 02 HC-SR04 ultrasonic distance sensor
  • 03 Active buzzer
  • 04 Breadboard
  • 05 Jumper wires

Wiring

Keep the sensor and buzzer on separate pins so their signals don't interfere.

Component pinConnects to
Sensor VCCArduino 5V
Sensor GNDArduino GND
Sensor TRIGDigital pin 9
Sensor ECHODigital pin 10
Buzzer +Digital pin 8
Buzzer −Arduino GND

Build steps

  1. Wire the circuit

    Place the sensor and buzzer on the breadboard and connect them to the Arduino following the pin table above.

  2. Install the Arduino IDE

    Download it from arduino.cc, then connect your board over USB.

  3. Select your board and port

    In the IDE, set Tools > Board to your Arduino model, and Tools > Port to the port it's connected on.

  4. Upload the sketch

    Paste in the code below and click Upload. Wait for "Done uploading" in the status bar.

  5. Test it

    Open the Serial Monitor to watch live distance readings, then wave a hand in front of the sensor — the buzzer should sound.

The code

#define TRIG_PIN 9 #define ECHO_PIN 10 #define BUZZER_PIN 8 #define THRESHOLD_CM 15 // distance that triggers the buzzer void setup() { pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(BUZZER_PIN, OUTPUT); Serial.begin(9600); } void loop() { long duration; float distanceCm; // send a 10us pulse to trigger a reading digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure the echo and convert to centimeters duration = pulseIn(ECHO_PIN, HIGH); distanceCm = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm"); if (distanceCm > 0 && distanceCm < THRESHOLD_CM) { digitalWrite(BUZZER_PIN, HIGH); // buzzer ON } else { digitalWrite(BUZZER_PIN, LOW); // buzzer OFF } delay(100); }
Using a passive buzzer instead? Swap the HIGH/LOW lines for tone(BUZZER_PIN, 1000) and noTone(BUZZER_PIN) to get an audible pitch.

Wrap-up

That's the whole circuit: the sensor pings continuously, the Arduino measures how long the echo takes to bounce back, and the buzzer fires the instant something crosses the threshold distance. From here it's easy to extend — log detections over time, add an LED, or swap the buzzer for a relay to trigger something bigger.

No comments:

Post a Comment

Popular IoT Sensors & Actuators — Price Guide (India) Component reference Popular IoT sensors & actuators, wit...

Popular Posts