Improve Ultrasonic Range Sensor Accuracy

About: I am currently a student in South Dakota. All my life I have been interested in tinkering with electronic amd mechanical gizmos, however after working at an electronics shop and being a member of instructabl...

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!

Share

    Recommendations

    • Big and Small Contest

      Big and Small Contest
    • Toys Contest

      Toys Contest
    • First Time Author

      First Time Author

    27 Discussions

    0
    None
    steveinjava

    2 months ago

    Great project! I see you get the variability down to 0.03mm...Does that mean we could differentiate between distances of, say, 0.05mm? Or is the digital encoding limited to whole mm's? I should add, I am looking to measure very close up- around 2cm. I also looked into laser but they are ungodly expensive for any accurate models...

    0
    None
    MicroC6

    6 months ago

    It seems that in the air sound speed is mostly relied on tempeature. See the snapshot I took from the speed of sound article of wikipedia. Therefore the writer is correct as long as the sonsor does not fly so high :)

    Capture.PNG
    0
    None
    AdityaR39

    Question 10 months ago

    How can we increase the range and angle of transceiver simultaneously that makes use of ultrasonic sensor in reverse parking?

    0
    None
    cwarren205

    1 year ago

    Very nice work! I cannot wait to try this and pass it on to my son's robotic team.

    1 reply
    0
    None
    MagicByCalvincwarren205

    Reply 1 year ago

    Happy to hear you liked it! If you folks need some extra help for an advanced build, feel free to PM me.

    0
    None
    Huntbb

    3 years ago on Introduction

    Hey everyone, my name is Hunter Burns. I saw this tutorial and thought it was great to help improve my project. I am new to Arduino so I am still learning and would love some help. I copied this tutorial to my code, but it says that there is something wrong, no matter what I do. I will post some screenshots for you guys to look at. If anyone can help, I would much appreciate it. Thanks!

    Screen Shot 2015-09-28 at 12.10.33 PM.pngScreen Shot 2015-09-28 at 12.17.20 PM.pngScreen Shot 2015-09-28 at 12.18.30 PM.png
    4 replies
    0
    None
    MagicByCalvinHuntbb

    Reply 3 years ago

    Could you post the full code that you are using? From looking at it though, it appears that your functions were not declared correctly. I think you're missing braces for the get voltage function.

    0
    None
    HuntbbMagicByCalvin

    Reply 3 years ago on Introduction

    #define trigPin 13

    #define echoPin 12

    #define tempAir 24

    #define max_distance 91

    int Buzzer = 9;

    int temperaturePin = A0;

    long unsigned soundSpeed;

    boolean debug = true;

    float temperature = 30;

    void setup() {

    if (debug){

    Serial.begin (9600);

    }

    soundSpeed = 331300+606*tempAir;

    pinMode(trigPin, OUTPUT);

    pinMode(echoPin, INPUT);

    pinMode(Buzzer, OUTPUT);

    }

    void loop() {

    temperature = (getVoltage(temperaturePin) - 0.5) * 100;

    if (debug){

    Serial.println(temperature);

    }

    long duration, distance;

    digitalWrite(trigPin, LOW);

    delayMicroseconds(2);

    digitalWrite(trigPin, HIGH);

    delayMicroseconds(5);

    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);

    distance = (duration/2) / 29.1;

    cm = microsecondsToCentimeters(duration, temperature);

    if (debug){

    Serial.println(cm);

    Serial.println(" cm");

    }

    float getVoltage(int pin) {

    return (analogRead(pin) * .004882814);

    long microsecondsToCentimeters(long microseconds, long temp) {

    return (microseconds * (331.3 + 0.606 * temp)) / 2

    }

    }

    if (distance >= 91 || distance <= 61){

    Serial.println("Out of range");

    digitalWrite(Buzzer, LOW);

    }

    else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(Buzzer, 100);

    delay(300);

    noTone(Buzzer);

    }

    if (distance >= 61 || distance <= 30){

    Serial.println("Out of range");

    digitalWrite(Buzzer, LOW);

    }

    else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(Buzzer, 200);

    delay(300);

    noTone(Buzzer);

    }

    if (distance >= 30 || distance <= 0){

    Serial.println("Out of range");

    digitalWrite(Buzzer, LOW);

    }

    else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(Buzzer, 300);

    delay(300);

    noTone(Buzzer);

    }

    delay(500);

    }

    0
    None
    MagicByCalvinHuntbb

    Reply 2 years ago

    So I took a look at your code and tweaked it a little bit so that it didn't have a compile error. I am in a rush unfortunately so I didn't have the time to look at the logic of it. Hopefully it will work as expected! Here you go:

    #define trigPin 13

    #define echoPin 12

    #define tempAir 24

    #define max_distance 91

    int Buzzer = 9;

    int temperaturePin = A0;

    long unsigned soundSpeed;

    boolean debug = true;

    float temperature = 30;

    long cm;

    float getVoltage( int pin );

    long microsecondsToCentimeters(long microseconds, long temp);

    void setup() {

    if (debug){

    Serial.begin (9600);

    }

    soundSpeed = 331300+606*tempAir;

    pinMode(trigPin, OUTPUT);

    pinMode(echoPin, INPUT);

    pinMode(Buzzer, OUTPUT);

    }

    void loop() {

    temperature = (getVoltage(temperaturePin) - 0.5) * 100;

    if (debug){

    Serial.println(temperature);

    }

    long duration, distance;

    digitalWrite(trigPin, LOW);

    delayMicroseconds(2);

    digitalWrite(trigPin, HIGH);

    delayMicroseconds(5);

    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);

    distance = (duration/2) / 29.1;

    cm = microsecondsToCentimeters(duration, temperature);

    if (debug){

    Serial.println(cm);

    Serial.println(" cm");

    }

    if (distance >= 91 || distance <= 61){

    Serial.println("Out of range");

    digitalWrite(Buzzer, LOW);

    }

    else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(Buzzer, 100);

    delay(300);

    noTone(Buzzer);

    }

    if (distance >= 61 || distance <= 30){

    Serial.println("Out of range");

    digitalWrite(Buzzer, LOW);

    }

    else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(Buzzer, 200);

    delay(300);

    noTone(Buzzer);

    }

    if (distance >= 30 || distance <= 0){

    Serial.println("Out of range");

    digitalWrite(Buzzer, LOW);

    }

    else {

    Serial.print(distance);

    Serial.println(" cm");

    tone(Buzzer, 300);

    delay(300);

    noTone(Buzzer);

    }

    delay(500);

    }

    float getVoltage(int pin) {

    return (analogRead(pin) * .004882814);

    }

    long microsecondsToCentimeters(long microseconds, long temp) {

    return (microseconds * (331.3 + 0.606 * temp)) / 2;

    }

    0
    None
    MagicByCalvinHuntbb

    Reply 2 years ago

    I am happy to hear you are interested in my project! At first glance, I think that the code is missing the function prototype. This code was written in an older IDE which most likely did not require function prototypes. If this is the issue, you can solve the problem by writing the following line above void startup():

    float getVoltage(int pin);

    Let me know if that solves your problem!

    0
    None
    levieuxfini

    2 years ago

    same problem as huntbb

    0
    None
    yaly

    3 years ago

    I think that the 0.606 constant in the equation is for measuring at sea level, am I correct ? If so, a barometric pressure sensor to compensate would make it even better.

    1 reply
    0
    None
    MagicByCalvinyaly

    Reply 3 years ago

    You are very correct! That constant is for sea level. This could be changed if you know the general pressure in your local area, or as you suggested, add a barometric pressure sensor. thanks for your input!

    0
    None

    Ultrasonic range sensors can accurately measure distances from about 2 cm to about 3 m. So unfortunately it won't reach the 300 m that are required. I would recommend using a different measurement method. My best guess would be a laser measuring device, but after a very short google search it looks like you'll have to pay about $150 for a device that will measure about 30 m. I'd recommend attempting to make the distance shorter. If you do choose a laser measurement device, you could take into account the atmospheric changes which may bend the light a bit (I don't think that they would make much of a difference for these distances though).

    0
    None
    DanieliusV

    3 years ago on Introduction

    So how accurate does this get? I mean What's the maximum you can measure and does it loose accuracy measuring long distances? Could it measure smth like 300 m? I need it for my project :)

    0
    None
    ibenkos

    3 years ago

    Wow thanks for shearing this and good job finding out an way to improve it! Now my lcd ruler can be even better :D

    1 reply
    0
    None
    MagicByCalvinibenkos

    Reply 3 years ago

    Happy I can help! It's always fun finding new ways to improve sensors. There are many professional fields that are based entirely on improving sensors by writing code to make up for environmental changes.

    0
    None
    chestef

    4 years ago on Introduction

    Outstanding work! I am seeking such a sensor for measuring distance (non-contact) for measuring an object. The works I have found record the distances in CM's. Is there an Instructable, or modification of your work that can give me measurements in MM's?

    1 reply
    0
    None
    MagicByCalvinchestef

    Reply 4 years ago

    In the function microsecondstoCentimeters, instead of dividing by 2, divide by 0.2. This essentially multiplies your final number by 10 which converts CM to MM. If you need more help just let me know!

    0
    None
    ssrdi02

    4 years ago on Introduction

    An excellent piece of work, plus the Boolean debug is a nice touch. Well done.