Introduction: Improve Ultrasonic Range Sensor Accuracy
Hello again Instructables community!
This instructable will teach you how to improve the accuracy of an ultrasonic range sensor. It is based on the principal that sound will move through air at different speeds depending on the temperature. Since ultrasonic range sensors use the time it takes for the triggered sound to echo off an object and return to the sensor, the ambient temperature can play a role in the calculated distance.
Potential Applications:
• Improving the accuracy of any ultrasonic range sensor
• Adaptation for similar sensors placed in liquids or gases other than air
• Education
Required Materials:
• Ultrasonic Range Sensor (I use the 4 pin versions, however this will work with a 3 pin version as well)
• Arduino UNO (or other programmable microcontroller)
• TMP36 Temperature sensor (or similar)
• Jumper Cables
• Breadboard
Required Tools:
• Computer with Arduino IDE
• Your Brilliant Self
Step 1: Wire the Components
I have provided a Fritzing diagram to show how to hook up the temperature sensor. Be sure that you look at the datasheet for your particular temperature sensor since some sensors have flipped positive and negative ends. If you plug in your project and the temperature sensor gets really hot really fast, be sure to switch the polarity.
For the temperature sensor, connect the positive and negative wires to the correct terminals on the temperature sensor. Then connect the data terminal to analog pin A0 on the arduino.
I am using a four pin Ultrasonic Range Sensor because I believe it is easier to use than a three pin sensor because with a three pin sensor you have to code the data pin to be both an input and an output. The three pin sensors still work just fine though.
Connect the positive and negative wires to the correct terminals of the range sensor. Connect the trig data pin to digital pin 10 on the Arduino and the echo data pin to digital pin 9 on the Arduino.
Step 2: Code It!
In order to improve the range sensor's accuracy, the code will calculate what the speed of sound is according to the ambient temperature. The formula for the speed of sound through air at a certain temperature is: Vair = (331.3 + 0.606 * Tc) m/s where Vair is the speed of sound and Tc is the temperature in Celsius according to http://en.wikipedia.org/wiki/Speed_of_sound. Once the speed of sound has been calculated, the distance is measured the same way it is in other ultrasonic range sensor codes. The time is multiplied by the speed and then halved leaving you with units of distance to the object.
If you plan on using this in extreme temperatures, be sure to check the temperature sensor's datasheet as they will give a recommended temperature range. Sometimes the temperature sensors will work just fine in a large range but you might need to change the code a little bit to make up for variations of the temperature sensor's output.
Finally, as with all of my code, I am using a simple debug trick my friend taught me. Using a boolean and if statements, you can easily turn serial communication on or off by changing debug to true or false.
I attached the original code file, otherwise the code can be copied directly from here:
/*******************************************************************************************************
********************************************************************************************************
Improved Ultrasonic Range Sensing Created by Calvin Kielas-Jensen
Using an Arduino UNO, connect a TMP36 temperature sensor data pin to A0 and a 4 pin ultrasonic range sensor with trig pin on digital pin 8 and echo pin on digital pin 9.
This script improves the accuracy of an ultrasonic range sensor by measuring the ambient temperature. Sound moves through air at a speed dependant on the ambient temperature according to the following equation: Vair = (331.3 + 0.606 * Tc) m/s where Vair is the speed of sound and Tc is the temperature in celcius.
Anyone is welcome to use and modify this code as long as I am given credit. Thank you for respecting the open source movement!
******************************************************************************************************** *******************************************************************************************************/
//The majority of this code is taken and modified from the ARDX temperature sensor code and the PING
//sensor code.
int temperaturePin = A0; //Set the temperature pin input float temperature; //Set temperature variable
boolean debug = true; //For serial communication set debug to true, for faster code set debug to false
long duration, cm; //Set time and cm for distance sensing
int trig = 10, echo = 9; //Set pins for trig and echo
void setup() { if (debug) { Serial.begin(9600); } }
void loop() { temperature = (getVoltage(temperaturePin) - 0.5) * 100;
if (debug) { Serial.println(temperature); }
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(trig, OUTPUT); digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(5); digitalWrite(trig, LOW); duration = pulseIn(echo, HIGH);
cm = microsecondsToCentimeters(duration, temperature); if (debug) { Serial.println(cm); Serial.println("cm"); } }
float getVoltage(int pin) { return (analogRead(pin) * .004882814); //Converting from 0 to 1024 to 0 to 5v }
long microsecondsToCentimeters(long microseconds, long temp) { return (microseconds * (331.3 + 0.606 * temp)) / 2; //Multiplying the speed of sound through a certain temperature of air by the //length of time it takes to reach the object and back, divided by two }
Step 3: Conclusion
In conclusion this instructable improved the accuracy of an ultrasonic range sensor. I uploaded a graph of a my tests to prove that it does indeed increase the accuracy in different temperatures. As you can see, the control varied quite a bit depending on the temperature. Using the temperature sensor, the greatest variation from 30cm was 0.03cm.
Troubleshooting:
- If you aren't getting any serial readings, make sure that debug is set to true and that all your hardware connections are good.
- If the temperature sensor is getting really hot, unplug the Arduino right away and switch the positive and negative connections.
- If you are getting absurd distances, be sure to test the temperature sensor. A faulty temperature sensor can greatly change the distance output.
Future Plans:
- Integrate this system with more projects
- Create an underwater ultrasonic range sensor
- Improve other sensors using similar techniques
My aim of this instructable is to not only teach you how to improve the accuracy of an ultrasonic range sensor, but to open your mind to more possibilities. Even though an ultrasonic range sensor is a simple and well understood sensor, it can still be modified to work even better. When creating projects you should always think about the outside variables. In this case I thought about the temperature, but why not think about exposure to the elements, human interaction, or even extreme conditions out in space. A lot of times people like to create projects that have been designed in a vacuum, but that is not the way the real world works. I hope that you enjoyed my instructable and perhaps learned something too!
If you enjoyed this instructable, please be sure to favorite it and vote for it. Feel free to leave constructive comments!