Practical 9

Smart Obstacle Warning System

Multi-zone proximity alarm

Aim

To detect nearby obstacles with an ultrasonic sensor and warn the user through three graduated alert levels using LEDs and a buzzer.

Overview

The HC-SR04 measures the distance to whatever is in front of the sensor. The Arduino classifies the distance into three zones: Safe (green LED), Caution (yellow LED, slow beeps) and Danger (red LED, continuous buzzer). Zone thresholds are configurable, making the same sketch reusable for robots, reverse alarms or accessibility aids for visually impaired users.

Components Required

  • Arduino Uno
  • HC-SR04 ultrasonic distance sensor
  • Green, yellow, red LEDs + 220 ohm resistors
  • Active buzzer
  • Breadboard and jumper wires

Circuit Connections

FromToNote
HC-SR04 VCCArduino 5V-
HC-SR04 GNDArduino GND-
HC-SR04 TRIGArduino D9-
HC-SR04 ECHOArduino D10-
Green LED (via 220R)Arduino D4-
Yellow LED (via 220R)Arduino D5-
Red LED (via 220R)Arduino D6-
Buzzer +Arduino D7-

Interactive Circuit Diagram

Simulate the circuit: adjust the sensor inputs below and watch the outputs react according to the control logic.

Loading Wokwi components...

Working

  1. Distance is measured every 100 ms.
  2. dist > 100 cm: SAFE - green LED, buzzer off.
  3. 30-100 cm: CAUTION - yellow LED, buzzer beeps with interval proportional to distance.
  4. dist < 30 cm: DANGER - red LED, buzzer beeps continuously.
  5. Out-of-range readings (no echo) are treated as SAFE so they never cause false alarms.

Control Logic

Three-level state classification. Alert frequency = map(distance, DANGER, CAUTION_MAX, 100ms, 700ms) so the warning urgency rises smoothly as the obstacle approaches.

Arduino Code

1// Practical 9: Smart Obstacle Warning System
2const int TRIG_PIN = 9;
3const int ECHO_PIN = 10;
4const int GREEN_LED = 4;
5const int YELLOW_LED = 5;
6const int RED_LED = 6;
7const int BUZZER = 7;
8
9const float DANGER_CM = 30.0; // Below this = danger zone
10const float CAUTION_CM = 100.0; // Below this = caution zone
11
12float readDistanceCM() {
13 digitalWrite(TRIG_PIN, LOW);
14 delayMicroseconds(2);
15 digitalWrite(TRIG_PIN, HIGH);
16 delayMicroseconds(10);
17 digitalWrite(TRIG_PIN, LOW);
18 unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000);
19 if (duration == 0) return 9999.0; // No echo = treat as safe
20 return duration / 58.0;
21}
22
23void setup() {
24 pinMode(TRIG_PIN, OUTPUT);
25 pinMode(ECHO_PIN, INPUT);
26 pinMode(GREEN_LED, OUTPUT);
27 pinMode(YELLOW_LED, OUTPUT);
28 pinMode(RED_LED, OUTPUT);
29 pinMode(BUZZER, OUTPUT);
30 Serial.begin(9600);
31}
32
33void loop() {
34 float distance = readDistanceCM();
35 Serial.print("Distance: ");
36 Serial.println(distance);
37
38 if (distance < DANGER_CM) { // DANGER zone
39 digitalWrite(GREEN_LED, LOW);
40 digitalWrite(YELLOW_LED, LOW);
41 digitalWrite(RED_LED, HIGH);
42 digitalWrite(BUZZER, HIGH); // Continuous alarm
43 } else if (distance < CAUTION_CM) { // CAUTION zone
44 digitalWrite(GREEN_LED, LOW);
45 digitalWrite(YELLOW_LED, HIGH);
46 digitalWrite(RED_LED, LOW);
47 int interval = map((long)distance, DANGER_CM, CAUTION_CM, 100, 700);
48 digitalWrite(BUZZER, (millis() % (interval * 2L)) < interval);
49 } else { // SAFE zone
50 digitalWrite(GREEN_LED, HIGH);
51 digitalWrite(YELLOW_LED, LOW);
52 digitalWrite(RED_LED, LOW);
53 digitalWrite(BUZZER, LOW);
54 }
55 delay(100);
56}