In this lesson we explore use of the HC-SR04 sensor to measure the speed of sound. The hookup and programming are pretty simple. The Elegoo Kit includes this sensor, so if you have the kit, you will be using the same hardware we are using. This project builds on the work we did in Lesson 53.
For this build we will be using an Arduino Nano, which allows the project to be built on a single breadboard. This allows cleaner build, and one less likely to have problems from intermittent connections. The build neatness is also facilitated by using small straight jumper wires, which you can get HERE.
You can connect the sensor up according to this schematic:
The connection pins are the same when connecting to a Nano.
The video below explains how to measure speed of sound from data coming from this sensor.
Now that we know the basics of Arduino from the first 15 lessons, we can begin to focus on more and more cool projects.
I think you will be surprised to see how many sophisticated things you can do with the simple arduino skills you have already learned. In this project, we are going to measure the speed of sound using the arduino and an ultrasonic sensor. We will use the Virtuabotix ultrasonic sensor which you can get HERE for about nine bucks. This sensor measures the time it takes an ultrasonic ping to go out, bounce off a target, and come back. The time it takes for the ping to leave and come back to the sensor depends on the speed of sound and the distance to the target. This sensor works in the same way a bat uses high pitched tones to navigate in the dark. It is also the same principle used in submarine sonar. The Arduino circuit for this project is very simple, and shown in the following schematic:
This sensor is fairly easy to use. To hook it up, we take the sensor VCC pin and hook it to the arduino 5V pin. We take the sensor GND and connect to Arduino GND. The Trig pin on the sensor we take to pin 13 on the arduino and the Echo pin on the sensor we connect to the arduino pin 11.
The sensor works as follows. You take the trigger pin LOW with a digital write. You then pause, take the trigger pin HIGH, pause, and then take the trigger pen LOW again. This LOW-HIGH-LOW sequence creates a high pitched ultrasonic tone, or ping, which is sent out from the sensor. This ping will go out and bounce off the first thing in front of it, and bounce back to the sensor. The sensor will output a HIGH pulse on its echo pin, and the length of the pulse in microseconds indicates the time it took the ping to travel to the target and return. We can measure the length of this pulse using the pulseIn command we learned in lesson 15.
Once you make the pulseIn measurement, and know the time it took for the ping to travel to the target and return, you can use that to calculate the speed of sound. Since
distance = rate * time.
Distance is the distance traveled by the ping, time is how long it took the ping to travel to the target and return. With this we can rearrange the equation to solve for rate, which would be the speed of sound:
rate = time/distance
Since pulse in returns the ping travel time in microseconds, and if you measure the distance to the target in inches, the units on the rate will be in inches per microsecond. We would need to use dimensional analysis to convert this to miles per hour as follows:
(rate in inches/mircrosecond)*(1000000 microsecond/second)*
(3600 seconds/hour)*(1 mile/63360 inches)
This will then give rate in miles per hour, as you can see as the units cancel out. The video explains this in more detail.
Key thing to remember in doing this calculation is that the ping has to travel to the target and then return. If the target is 6 inches from the sensor, how far did the ping travel? It traveled 12 inches. Six to get to the target and six to return from the target.
Try and put it all together into a program that will measure the speed of sound. I have included the code below. Try and write the program yourself, but if you get stuck you can look at the code below. It is important not to copy and paste this code, but only use it as a guide. You need to type your programs in yourself.
Arduino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
inttrigPin=13;//Sensor Trip pin connected to Arduino pin 13
intechoPin=11;//Sensor Echo pin connected to Arduino pin 11
floatpingTime;
floatspeedOfSound;
inttargetDistance=6;//Distance to Target in inches
voidsetup(){
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
}
voidloop(){
// put your main code here, to run repeatedly:
digitalWrite(trigPin,LOW);//Set trigger pin low
delayMicroseconds(2000);//Let signal settle
digitalWrite(trigPin,HIGH);//Set trigPin high
delayMicroseconds(10);//Delay in high state
digitalWrite(trigPin,LOW);//ping has now been sent
pingTime=pulseIn(echoPin,HIGH);//pingTime is presented in microceconds
speedOfSound=(targetDistance*2)/pingTime*(1000000)*3600/63360;//converts to miles per hour
Serial.print("The Speed of Sound is: ");
Serial.print(speedOfSound);
Serial.println(" miles per hour");
delay(1000);
}
Check this out and see how close it is to published values for the speed of sound. Pretty Cool!
Making The World a Better Place One High Tech Project at a Time. Enjoy!