Measures the distance to an object and sounds an alarm noise that changes based on the distance…

What will I learn?

You will learn how to read digital values from a connected sensor and perform some action based on the value read from the sensor. You also learn about how to use a buzzer and turn on an LED.

Prerequisites

A Computer with a configured Arduino IDE tested and working. If you don’t know how to get the basic set up working, check out my guide for Getting started (1/5)

Important notes

  • This project contains components that cannot handle 5V, so take care to use resistors (1 KOhm) with the LEDs.
  • Respect minus and plus, otherwise you may damage the components.
  • Start out by figuring out what components can handle 5V and what components can’t. If you connect a component that cant handle 5V you may destroy the component.

What you need

  • Breadboard
  • Dupont wires & jumper wires
  • CP2102
  • Arduino Pro Mini
  • HC-SR04 Ultrasonic sensor
  • 1 KOhm resistor
  • 1 Color LED
  • 5V Buzzer

Connecting instructions

  • Arduino VCC -> HC-SR04 VCC
  • Arduino GND -> HC-SR04 GND
  • Arduino D6 -> HC-SR04 Echo
  • Arduino D7 -> HC-SR04 Trig
  • Arduino D9 -> Positive pin of Red LED
  • LED negative pin -> 1KOhm resistor and then to GND
  • Arduino D10 -> 5V Buzzer positive
  • Buzzer Neg. -> GND

 

Fritzing

distance-sensor_bb

 

Code

/**
* Alarm w. distance sensor - Registers the distance between the distance
* sensor and an object and makes an alarm sound that depends on the distance.
*
* Connections:
* - Arduino VCC -> HC-SR04 VCC
* - Arduino GND -> HC-SR04 GND
* - Arduino D6 -> HC-SR04 Echo
* - Arduino D7 -> HC-SR04 Trig
* - Arduino D9 -> Positive pin of Red LED
* - LED negative pin -> 1KOhm resistor and then to GND
* - Arduino D10 -> 5V Buzzer positive
* - Buzzer Neg. -> GND
*
* Author: Jakob Maaloe
* https://wackydeveloper.wordpress.com
*/
#define trigPin 7
#define echoPin 6
#define led 9
#define buzzer 10

int sound = 250;
void setup() {
Serial.begin(115200); // Sets the baud rate for logging

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);

}

void loop() {
long duration, distance;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the echo signal
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance <= 30) {
digitalWrite(led, HIGH);

if (distance <=10) {
sound = 10;
} else if (distance <20) {
sound = 200;
} else if (distance < 30) {
sound = 300;
}

Serial.print("James bond got caught! Distance: ");
Serial.print(distance);
Serial.println(" cm");

tone(buzzer, sound);
} else {
digitalWrite(led, LOW);
Serial.println("Out of range");
noTone(buzzer);
}

delay(500);
}

Next steps

Change the set up to contain any number of LEDs that shows the distance to the object, add a larger buzzer or transform it in to a wireless alarm system… or try the next project, the Motion PIR sensor alarm.

Advertisements