Ultrasonic sensor (HC – SR04) and Piezo speakers measuring distance and playing a tone with Piezo

Last time I tested out a sensor, Joystick. This week I was given a HC – SR04 Ultrasonic sensor to test it out.

This sensor is best described as in its datasheet found here.The most essential part is this:

(3) IF the signal back, through high level , time o
f high output IO duration is
the time from sending ultrasonic to returning.
Test distance = (high level time×velocity of sound
(340M/S) / 2,

So we send a short pulse to trig pin to send of the sound. Then IF  it bounces back, the distance is the time of high output with conjuction with the above formula.

Requirements

  • Arduino
  • 2 LED
  • 1 HC-SR04
  • 1 resistor
  • jump cables

Sketch

Screenshot - 02092014 - 12:16:42 PM
Images made with Fritzing.
Screenshot - 02092014 - 12:16:19 PM
Images made with Fritzing.

http://fritzing.org/home/

I modified code from http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html and added things of my own. There are 2 leds on the board, and a piezo speaker. When HC-SR04 doesn’t pick anything up or is out of range, yellow led is on. When the sensor gets back a signal, Yellow led is turned off and another led is lit up to indicate target is in range of the sensor. This information is also printed to the serial monitor with

Serial.print(“Distance = ” );
Serial.print(distance);
Serial.println(” cm” );

Note println on last line. We want to print all the info on one line at a time. This is more readable on the serial monitor. If all the the lines would be Serial.println  the output would look like this

Distance =

10

cm

Distance =

15

cm

Instead of

Distance is = 10 cm

Distance is = 15 cm

Read more about Print here

In addition to leds turning off and off, I was given a speaker to test out with. These 3 pages from arduino.cc helped me a TON to understand tone:

http://arduino.cc/en/Reference/tone

http://arduino.cc/en/Tutorial/Tone

http://arduino.cc/en/Tutorial/Tone4

Lets look at the code below:

tone(soundPin, 800, 300);
delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones.
noTone(soundPin);

As you know after reading the 3 above links Tone syntax is tone(pin, frequency, duration). So in this code I play a tone in soundPin, defines as 12 in setup, freq 800 for 300ms. After this we delay the sound by the distance. Delay is in ms and our distance is in cm. The noTone is not necessarily needed, as we define the length of the sound earlier. I wanted to make sure it turns off though, so I added this for my the safety of my ears.

IDEA

The delay could have been distance*x because a bleeping tone reading a 1cm would be kinda annoying playing every 1ms + 50ms (endoftheloop delay).

The whole code is commented below, ask me question in the comments if I left anything unanswered.

/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012
Further modified by Peter Takacs on 04/02/04 https://patakacs.wordpress.com/2014/02/04/ultrasonic-sensor-hc-sr04-and-piezo-speakers-measuring-distance-and-playing-a-tone-with-piezo/
*/

int echoPin =  7;
int trigPin =  8;
int LEDPinYellow = 2; // Yellow LED
int LEDPinGreen = 4; // Green LED
int soundPin = 12; // Piezo

int maximumRange = 100;
int minimumRange = 0;
long duration, distance;

void setup() {
Serial.begin (9600); // Init communications to serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPinYellow, OUTPUT);
pinMode(LEDPinGreen, OUTPUT);
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
// Yellow led indicated out of maximumRange. Prints “Out Of Range” to serial if target is outside maximumRange.
Serial.println(“Out Of Range”);
digitalWrite(LEDPinYellow, HIGH);
digitalWrite(LEDPinGreen, LOW);
}
else {
// When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED.
Serial.print(“Distance = ” );
Serial.print(distance);
Serial.println(” cm” );
digitalWrite(LEDPinYellow, LOW);
digitalWrite(LEDPinGreen, HIGH);
tone(soundPin, 800, 300);
delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones.
noTone(soundPin);

}

// Mandatory delay
delay(50);
}

Sources:

Tero Karvinen’s lessons in Haaga-Helia University of Applied Sciences

NOTE: All images (if any) used on this post were self-taken. Fritzing photos are made with Fritzing and is the property of Fritzing.

“This document can be copied and modified under the conditions of GNU General Public License. http://www.gnu.org/licenses/gpl.html

2 thoughts on “Ultrasonic sensor (HC – SR04) and Piezo speakers measuring distance and playing a tone with Piezo

Leave a comment