Monday, October 19, 2015

Measuring The Speed Of Sound With An Arduino And A $3 Sensor


Faster than a speeding bullet, able to bounce off walls in a single bound!
   Look! Over by the Arduino.
      It’s Super sound!                                                      by Bill Johnson, KI4ZMV

So you got your first Arduino program working. Great, you now have a thirty-dollar blinking light. How impressive is that? Not very. Perhaps the best ways to show off the power of the Arduino micro controller is to have it do something awesome. Measuring the speed of sound fits that description.

The Project
You can measure the speed of sound using the same principles found in digital tape measures. The difference will be that you fix the distance to a reflecting surface, and measure the time it takes for sound to make the round trip from sender back to receiver. From this you can calculate its speed using the formula:
speed =Distance/time

The Device
The device that makes this all possible is the HCSRO4 transmitter/receiver. I found this one on eBay. Look around. Prices vary. I paid about three dollars.
 


 Figure 1. The sensor is relatively small. The left side labeled T is the transmitter; the right, labeled R, is the receiver.

These units have only four connections: ground, echo, trigger, and Vcc. The left side marked T transmits a ping, while the right side labeled R listens for its return. The total distance traveled from the transmitter to a reflecting object and back, divided by the time it takes to make the round trip, is the speed of sound.

Wiring is straightforward.  There are only four connections. Connect sensor ground to Arduino ground, Vcc to Arduino five volts, the Trig pin to Arduino pin 13, and the echo pin to Arduino pin ll. Other combinations are possible. How simple is that?


Figure 2. The illustration above from www.toptechboy.com. A slightly modified   version of the program, appearing below, also came from this site.

The Test Setup
This is what my six-inch setup looked like. Yes, that is a napkin holder and a box of stick matches. Most likely your setup will be different.  


Figure 3 This is the six inch setup. The two probes connect the trig and echo pins to a scope. More below.

To make a reading, the trigger pin is brought LOW with a digital write. A pause follows to let things settle. After the pause the trigger pin is first brought HIGH then LOW again in quick succession. This LOW HIGH LOW sequence initiates, after a fixed delay, a ping and the start of the timing cycle. The ping will travel outward, bounce off a target, and then return, where it is registered by the echo pin. As soon as the echo is received, the timer stops, and the total time is set into the sketch variable called pingTime. PingTime, or travel time is measured in microseconds. Fortunately, the Arduino has a built in pulseIn(pin,state) library command that can be used to accurately measure pulse length.  

To get a better picture of what is happening, look at the dual trace scope output below from my Rigol Oscilloscope. Trace 1 shows the short initializing pulse, (upper trace), while trace 2 shows the resultant ping travel time (lower trace).


Figure 4. The scope’s scale is 500-microseconds per division horizontal, and 5 volts per division vertical.

The short pulse on trace1 was 10-microseconds long. The delay between this start pulse and the beginning of the timing pulse was 460 microseconds. This time is fixed, and independent of both the length of the initial short pulse as well as the measured time. With a distance of six inches from the reflecting surface to the sensors, the travel time was around 870-microseconds at room temperature.

The Arduino Sketch
Start by setting some variables. First, we need variables to identify the trig pin and echo pin. These will be type int. We also need three additional variables, one to represent ping travel time, another to represent the calculated speed of sound, and a third to represent the distance to the target. These will be type float.

In setup we initialize the serial monitor. This will be used for output. This is also the place we set the pin mode for the trig and echo pins.

In loop we bring the trig pin low, wait two seconds, then bring it high for 10 microseconds, then low again. This initializes the pulse read process. (More below.) Next we set the ping time equal to the pulse length reported by the Arduino pulseln function.

Calculation of the speed of sound is a matter of distance traveled divided by time. Distance is twice the target distance in inches, and time is the returned value of pulse length in microseconds. The inches per microsecond must then be scaled to miles per hour. Here is the completed sketch:

The Arduino Sketch

int trigPin = 13; //set trig pin to Arduino pin 13
int echoPin= 11; //set echo pin to Arduino pin ll
float pingTime;  //a variable to hold elapsed travel time to and from the target
float speedOfSound; //a variable to hold the speed of sound
float targetDistance=6; //a variable to hold the target distance. This will differ by test condition.


void setup() {
  Serial.begin (9600);  // start the serial monitor
  pinMode(trigPin,OUTPUT); //set the trigPin to OUTPUT
  pinMode(echoPin,INPUT); //set the echoPin to INPUT
}

void loop() {
  digitalWrite(trigPin, LOW);  //pull trig pin low
  delayMicroseconds(2000);  // delay to let things settle
  digitalWrite(trigPin,HIGH); //start initializing short pulse
  delayMicroseconds(10);  //pulse length
  digitalWrite(trigPin,LOW); //pull pulse low
  pingTime=pulseIn(echoPin,HIGH); //set pingTime to measured pulse length
  speedOfSound= 2*targetDistance/pingTime; //calculate speed of sound in inches per microsecond
  speedOfSound=speedOfSound *3600*1000000/63360; // convert to mph
  Serial.print("The speed of sound is "); //this line can be commented out for spreadsheet analysis
  Serial.print(speedOfSound); //print speed of sound
  Serial.println(" miles per hour"); //this line can be commented out for spreadsheet analysis
  delay (1000); //short delay for display purposes
}

Note, during testing I found it helpful to comment out the verbiage and print only values. This made transfer to a spreadsheet easier.

The Speed Of Sound And Ambient Temperature
The speed of sound varies with temperature. The following relationship is approximate, but accurate enough for our purposes. 

V=0.7341Tf +717.22
V is the speed of sound in mph
Tf  is the temperature in degrees Fahrenheit

From this we can generate a plot of the speed of sound in miles per hour versus temperature in degrees Fahrenheit, and a brief table to get a sense of the changes you might expect going from room temperature to either lower or higher temperatures.



Figure 5. Graphical and tabular results from the equation given above. These are the expected values for speed of sound at various temperatures.

From the graph and chart above we should expect the speed of sound to decrease at lower temperatures. Specifically, we would expect a 59 miles per hour decrease in the speed of sound going from an ambient temperature of 80F to 0F degrees.

To test this hypothesis, the apparatus was placed a refrigerator’s freezer section, whose temperature was zero degrees F. Though the data is noisy, it does suggest, that on the average, good agreement between the expected drop of 59 mph. The noise in the data cannot be accounted for at this time. One possible cause might be moisture buildup on the sensor.


  Figure 6. The drop in the speed of sound is obvious.

The Effect Of The Initial Pulse Length
Mentioned above was the apparent independence, within limits, of the initial pulse duration. Trigger pulse lengths of 10 to 200 microseconds were evaluated for their affect on the delay time between the end of the pulse and the start of the timing cycle. None was found.





Figure 7. The timing cycle, and the effect of initial pulse/trigger length

The Sensitivity Of Target Distance From the Sensor
The placement of the reflecting surface vis-a-vis the sensor is critical, especially at distances of around six inches. A series of measurements were made to determine the sensitivity of this parameter on the measured speed experimentally. Successive sheets of plywood were added to shorten the path. Each sheet was approximately 0.23 inches in thickness.


Figure 8. Testing the sensitivity to distance from the sensor at a nominal six inches.

Test results indicate the expected variation in apparent speed due to error in sensor to target distance. From the accompanying chart and its regression equation this is approximately 13.7 mph error for every increment of 0.1 inches. This is quite sensitive.


 
Figure 9. Sensitivity to distance from the sensor to reflector at nominal six inches.

It is assumed that placement of the sensor would be less sensitive if the distance were farther away. A test was run at thirty-six inches, and the results support the assumption.


Figure 10. This setup was used to measure the sensitivity of the sensor to target distance with a nominal distance of 36 inches.



Figure 11. Sensitivity to distance from the sensor to reflector at nominal thirty-six inches

From the chart,, regression equation, and table above it is clear that the sensitivity to distance from the target is much less. The regression equation suggests only about a 2.0 mph error for every 0.1 inches from nominal. We would expect that the sensitivity would be about six times as great for the nominal six inches versus the nominal thirty-six inch setups. Experimentally, the ratio was 6.9 ratio, or an error of approximately 15%.

Conclusion: We have demonstrated a practical way to measure the speed of sound using an inexpensive sensor and an Aduino. The sensitivity to ambient temperature and target distance were also explored.
A good friend of mine told me that if he ever needed to know the speed of sound he would Google it. This is a reasonable answer. You could also tell your grandson to Google it, or perhaps you could introduce him to the Arduino. How cool is that?


No comments:

Post a Comment