กลับไปหน้ารวมไฟล์
proximity-alarm-and-distance-monitor-using-ultrasonic-sensor-1922ae-en.md

This project helps you create a system to sense and measure the distance to solid objects nearby. This is accomplished with an ultrasonic sensor. To scan for objects around it, we have fixed it to a servo motor, which sweeps around, enabling the sensor to scan in a semicircular region in front of it.

To make the project more fun, we have added a red LED and a buzzer. The code is such that if anything comes closer than 15 cm, the LED will light up dimly. If it is closer than 5cm, the LED will glow brightly and the buzzer will sound the alarm. You can edit the code and make changes to these distances if needed.

Technical Deep-Dive

This "Sonic-Sentinel" project represents a fundamental exploration into Active Acoustic Sensing. By combining an ultrasonic transceiver with a synchronized servo sweep, it creates a low-cost "Radar" (technically Sonar) system. It demonstrates the intersection of physics and firmware, where microsecond-level timing is used to calculate spatial coordinates.

  • Acoustic Time-of-Flight (ToF) Forensics:
    • The 40kHz Burst: The HC-SR04 sensor dispatches an 8-cycle burst of ultrasonic sound at 40kHz when triggered by a 10µs pulse from the Arduino.
    • Distance Translation: The system measures the "Echo" pulse duration. Using the speed of sound in air ($343m/s$ or $0.0343cm/µs$), the firmware calculates the distance via the formula: $Distance = \frac{Time \times 0.0343}{2}$. The division by two accounts for the sound wave's round-trip from the sensor to the object and back.
  • Servo-Synchronized Spatial Scanning:
    • Polar Coordinate Mapping: To scan a 180º field, the SG90 servo sweeps in discrete steps. The Arduino code ensures the ultrasonic "Ping" occurs only after the servo has reached its setpoint, preventing data smearing.
    • NewPing Library Advantage: Unlike standard pulseIn() methods, the NewPing library uses optimized, non-blocking timing logic, allowing for faster, more stable scan rates.
  • Haptic & Multi-Level Visual Feedback:
    • PWM Intensity Mapping: The red LED is connected to a PWM (Pulse-Width Modulation) pin. The software maps the distance (cm) to a brightness value (0-255). As an object moves from 15cm to 5cm, the duty cycle increases, creating an intensifying "Warning Glow."
    • Threshold Alert: Once the object breaks the 5cm "Critical Zone," the buzzer is triggered, providing an immediate auditory alarm.

Engineering & Implementation

Assemble the components according to the circuit diagram given below.

  • Mechanical Hub Integration: Attach the ultrasonic sensor to the servo motor such that the sensor sweeps 180 deg while the servo moves. This can be done with double-sided tape or a glue gun. Attach the servo firmly such that the sensor faces the space you want to scan. We have made a simple holder for the servo using cardboard. Securely mounting the HC-SR04 is critical; any wobble translates to significant angular errors in the distance readings.
  • Firmware Determinism: The project highlights two implementation methods: the raw bit-banging approach (manual Trig/Echo handling) and the library-based approach. The former provides clear "Hardware Forensics," while the latter offers a more robust framework.

Before entering the code, go to the Library manager (Tools->Manage Libraries) and download the NewPing (type it in the textbox, and click install when it appears in the list). You can do this project without this library, and the method is explained towards the end.

Upload the code to your Arduino board. Any Arduino board can be used, but if you change the pins to which you attach the components, be sure to change it in the code too.

View the results, i.e, the distance to the obstacle, in the serial monitor. You can also see this as a graph in the serial plotter tool.

  • Data Visualization: By outputting raw cm values to the Serial Plotter, the system provides a real-time "Oscilloscope" view of the surroundings, making it easy to identify atmospheric noise or sensor dropouts.
Click on these options to see the results

Test out your cool new setup!! You can use it to detect intruders in your room, as a cheap parking sensor or even to catch the thief who has been stealing the candy bars!!

We gave it two arms 💪😁

Adding a few tips;

The code given below uses the NewPing library. If you don't want to install the library, It can be done without this by replacing

int cm = sonar.ping_cm();

with

long duration, inches, cm;
pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm

As you can see, it is long and complicated. But this gives you a clear picture of how the ultrasonic sensor works. A signal is given to the trigpin for 10 seconds and the duration it takes to reach echoPin is calculated. Since two times the distance it travelled (from the sensor to the object and back again) is equal to speed (speed of sound i.e, 0.0343 cm/microsecond) X time (duration), we end up with the value of distance stored in cm.

For a simpler project, the LED and buzzer and even the servo can be avoided. Simply neglect them from the circuit and use the following code.

#include <NewPing.h>
const int TriggerPin = 6;
const int EchoPin = 7;
// 100 = maxDistance
NewPing sonar (TriggerPin, EchoPin, 100);

void setup() {
 Serial.begin(9600);
}
void loop() {
 int cm = sonar.ping_cm();
 Serial.println(cm);
 delay(50);
}

Conclusion

Sonic-Sentinel is a powerful demonstration of how simple sensors can be combined to form a sophisticated spatial awareness system. By mastering Acoustic Ranging and Servo Synchronization, developers gain the skills necessary to build advanced obstacle avoidance systems in the fields of autonomous robotics and IoT security.


Sonic Visualization: Mapping the invisible through acoustic forensics.

Happy building!!! Stay curious, stay inspired!!

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "Arduino IDE (Serial Monitor & Plotter)"
  - "NewPing Library (Enhanced Timing Core)"
author: "theriveroars"
category: "Sensors & Environment"
components:
  - "1x Arduino Uno R3 (Main Controller)"
  - "1x HC-SR04 Ultrasonic Sensor (Acoustic Transceiver)"
  - "1x SG90 Micro-Servo (Scanning Actuator)"
  - "1x Active Buzzer (Audio Alert)"
  - "1x Red LED (Visual Proximity Indicator)"
  - "1x 220 Ohm Resistor (Current Limiting)"
  - "1x Custom Cardboard Scanning Mount"
description: "An active spatial monitoring system that utilizes acoustic time-of-flight (ToF) forensics and servo-synchronized scanning to detect and visualize proximity across a 180º field."
difficulty: "Easy"
documentationLinks: []
downloadableFiles:
  - "https://create.arduino.cc/editor/thegreatpanda/93b5b663-c337-4cc0-9c13-ce004ad70c5d"
  - "https://create.arduino.cc/editor/thegreatpanda/93b5b663-c337-4cc0-9c13-ce004ad70c5d"
encryptedPayload: "U2FsdGVkX18zaScGeGxEwBkCVTbheJ/wuGa4CZ1bDw/9kVhCD8EoI3l2DtV5IIGMWhIm9i9AMJnzCvl7PMCIMHQCWeDQ9RWKQAC77LuS+fU="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/proximity-alarm-and-distance-monitor-using-ultrasonic-sensor-1922ae_cover.jpg"
lang: "en"
likes: 0
passwordHash: "ac62b3666a841f3278cd9a6a0eefb5184e435b3f165398ecbc140de2726bef84"
price: 1499
seoDescription: "Build a Proximity Alarm and distance monitor using an Ultrasonic Sensor with sound and light alerts for objects that are too close."
tags:
  - "sonar"
  - "ultrasonic-ranging"
  - "spatial-scanning"
  - "proximity-alarm"
  - "newping-forensics"
  - "hmi-feedback"
title: "Proximity Alarm and distance monitor Using Ultrasonic Sensor"
tools:
  - "Hot Glue Gun"
videoLinks:
  - "https://www.youtube.com/embed/Ll8vVZrt34Y"
views: 7357