Back to Resources
Intermediate

Ultrasonic Distance Sensor Alarm

Intermediate Arduino Project
1-2 hours

Create a proximity alarm system using an ultrasonic sensor. Features adjustable sensitivity and both visual and audio alerts.

Ultrasonic Distance Sensor Alarm

Project Overview

Build a sophisticated proximity alarm system using ultrasonic technology! This project teaches you how to work with sensors, process analog data, and create responsive alert systems.

The HC-SR04 ultrasonic sensor works like sonar, sending out sound waves and measuring the time it takes for them to bounce back. You'll learn how to convert this timing data into distance measurements.

This project includes both visual (LED) and audio (buzzer) alerts, with adjustable sensitivity using a potentiometer. It's perfect for security applications, parking sensors, or robotics projects.

What You'll Learn

  • Understanding digital pin outputs
  • Using delay() functions for timing
  • Creating loops and sequences
  • Basic circuit building
  • Arduino IDE fundamentals
  • Troubleshooting common issues
  • Analog sensor reading and processing
  • Sensor calibration techniques
  • Data validation and error handling
  • Serial monitor debugging

Key Features

Real-time sensor data display
Data validation and error handling
Serial monitor output
Adjustable refresh rates
Adjustable sensitivity control
Visual and audio alerts
Distance measurement display
Serial monitor output

Step-by-Step Instructions

1

Wire the Ultrasonic Sensor

Connect VCC to 5V, GND to ground, Trig to pin 7, and Echo to pin 6. The sensor needs clean power connections.

Wire the Ultrasonic Sensor
2

Add Alarm Components

Connect the buzzer to pin 8, LED to pin 13 with a resistor, and potentiometer to A0 for sensitivity adjustment.

const int trigPin = 7;     // Trig pin of the ultrasonic sensor
const int echoPin = 8;     // Echo pin of the ultrasonic sensor
const int ledPin = 13;     // Pin for the LED indicator
const int delayTime = 100;


void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Configure pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
}
3

Write Distance Measurement Code

Create a function to measure distance using the ultrasonic sensor timing method.

void loop() {
  float duration;
  float distance;

  // Trigger the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the echo signal
  duration = pulseIn(echoPin, HIGH);

  // Convert duration to distance
  distance = (duration * 0.0343 ) / 2;

  // LED indicator for close objects
  if (distance < 5) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  // Print results to the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);

  // Delay for stability
  delay(100);
}

Interactive Demo

Tags

#arduino#beginner#electronics#sensor#monitoring

Materials Needed

  • Arduino Uno R3
  • Breadboard
  • Jumper Wires
  • USB Cable
  • HC-SR04 Ultrasonic Sensor
  • Red LED
  • 100Ω Resistor

Project Details

Difficulty:Intermediate
Estimated Time:1-2 hours
Category:arduino projects
Est. Cost:$25-30
benyeogorosenwe@gmail.com

© Osenwe Benyeogor.