» Electronics » Arduino »Very accurate ultrasonic rangefinder

Very accurate ultrasonic rangefinder



This HC-SR04 ultrasonic rangefinder has been enhanced with the LM35 temperature sensor.

From school you can recall that the speed of sound depends on the density of air, and the density of air depends on temperature.
We will calculate the temperature using this formula
temp2 = (temp / 1023.0) * 5.0 * 1000/10;

Where temp2 is the finished temperature value after the formula,
And where temp is the raw value from the sensor.

What we need:
  • Ultrasonic Rangefinder HC-SR04
  • Arduino
  • Bread board
  • Jumpers
  • Thermistor LM35 (Temperature Sensor)


Keep in mind that HC-SR04 comes in two forms -
Here I have a good one and it stably measures distance.
They both measure from 4 cm to 400 cm.
Powered by 3.3 to 5 v


To begin with, we mock it all up on a breadboard.

Ultrasonic Sensor:
  • GND - gnd
  • Echo - 3 pins
  • Trig - 2 pins
  • Vcc - 5v

Thermistor LM35:
  • 1 - 5v
  • 2 - A0
  • 3 - GND

Very accurate ultrasonic rangefinder

It should be like this:

Next we fill in the sketch.
Download library for HC-SR04 sensor -
iarduino_hc-sr04.zip [299.69 Kb] (downloads: 207)

Download sketch -
sketch_mar23f.zip [338 b] (downloads: 157)

#include  // connect the library to work with the ultrasonic sensor HC-SR04
iarduino_HC_SR04 hcsr (2,3); // denote contacts Trig and Echo
int temp = 0; // raw temperature
float temp2 = 0; // ready temperature value

void setup () {// run 1 time
  Serial.begin (9600); // start the port monitor
  pinMode (A0, INPUT); // denote contact A0 as input
}

void loop () {// repeats an infinite number of times
  temp = analogRead (A0); // read the value of contact A0 and assign the variable temp to it
  temp2 = (temp / 1023.0) * 5.0 * 1000/10; // calculate the raw value according to the formula to get the finished temperature value
  Serial.println (hcsr.distance (temp2)); // write the ready range in the port monitor
  delay (1000); // delay 1 second
}

And here is a sketch for an LCD display-
Download sketch -
sketch_mar23f-1.zip [861 b] (downloads: 139)


#include  // connect the library to work with the ultrasonic sensor HC-SR04
#include  // connect the library to work with the display on the I2C bus

LiquidCrystal_I2C lcd (0x27, 16, 2); // denote (address, columns, lines)
iarduino_HC_SR04 hcsr (2,3); // denote contacts Trig and Echo

int temp = 0; // raw temperature
float temp2 = 0; // ready temperature value

void setup () {// run 1 time
  lcd.init (); // initialize the display
  lcd.backlight (); // turn on the display backlight
  pinMode (A0, INPUT); // denote contact A0 as input
}

void loop () {// repeats an infinite number of times
  temp = analogRead (A0); // read the value of contact A0 and assign the variable temp to it
  temp2 = (temp / 1023.0) * 5.0 * 1000/10; // calculate the raw value according to the formula to get the finished temperature value
  lcd.setCursor (2, 0); // put the cursor on 2 column and 0 row
  lcd.print (temp2); // write the distance
  lcd.setCursor (5,0); // place the cursor on the 5th column and 0th row
  lcd.print ("cm"); // write cm
  delay (1000); // delay 1 second
}


Write comments and questions, I will answer everything!
10
10
10

Add a comment

    • smilesmilesxaxaokdontknowyahoonea
      bossscratchfoolyesyes-yesaggressivesecret
      sorrydancedance2dance3pardonhelpdrinks
      stopfriendsgoodgoodgoodwhistleswoontongue
      smokeclappingcraydeclarederisivedon-t_mentiondownload
      heatirefullaugh1mdameetingmoskingnegative
      not_ipopcornpunishreadscarescaressearch
      tauntthank_youthisto_clueumnikacuteagree
      badbeeeblack_eyeblum3blushboastboredom
      censoredpleasantrysecret2threatenvictoryyusun_bespectacled
      shokrespektlolprevedwelcomekrutoyya_za
      ya_dobryihelperne_huliganne_othodifludbanclose
13 comments
Guest Alexander
Where does this formula come from:
temp2 = (temp / 1023.0) * 5.0 * 1000/10
Could you tell me more, where are these values ​​taken from?
I would also add point 4. Do not use for measurements, only for automation devices!
I understand (judging by the above parameters) that the instructions for it look something like this:
1. Grab the HC-SR04 in your left hand, and the Atoruchka, or marker, in your right.
2. Estimate the distance by eye and write it down.
3. Put the HC-SR04 back into the box.
ok
Quote: “Features of the ultrasonic rangefinder HC-SR04
Measurement range: from 2 cm to 400 cm.
Measurement accuracy: ± 1 cm (at maximum range ± 3 cm).

Question: How "weighty" is the temperature correction in the total error?
I believe that the homemade product was not implemented, there is no photo of the finished device and its operation.
I consider this homemade product as "childish", but the article does not show or chew how to connect the display. For beginners, our sites are unclear, but experienced ones do not need it. Chew and show all the home-made.
Quote: Anton Martyanov
In fact, they differ not only in inscription but also in accuracy!

According to the documentation or did you come across this?
Author
In fact, they differ not only in inscription but also in accuracy!
But is that not enough ??? boss For example, I also do not like it when the markings are written below half a meter !!! smiles dontknow
And why is it still bad, except for the height of the marking?
Author
The bad one is the inscription HC-SR04 0.5 cm lower
Very accurate
And what is the accuracy? What was used as a reference? What is the temperature error?
Keep in mind that HC-SR04 comes in two forms -
I have a good one
And which one do you think is bad?
I’ll tell you from my own experience that an ultrasonic rangefinder, like “contactless roulette” is a pampering !!!
One is lying around as unnecessary ...
And not because of insufficient accuracy, but because they can only measure the distance to a sufficiently large wall of solid material ... But to the back wall of a niche in this wall - no way !!! And it also won’t work if this wall is littered with furniture and other things, and there is only a small “window” free from clutter ... (In this case, it’s easier not to free the wall from all this, but just use regular roulette).
Again, try measuring the distance to a peg driven into the ground !!!

And to the greenhouse?)))
And to the wall covered with fabric?)))
And to the facade, already pasted with mineral wool slabs, but still without the final coating?))))
And before each of the ten rafters arranged in a row?
And from the wall to the end of the board already nailed to the ceiling to cut off the required length and close this "hole" ???

None of the above will work out for you !!!

And then what to measure with it during repair, or at a construction site, for example ??? Only the size and area of ​​the premises, while free of furniture ???

In short, I threw it on the shelf a couple of years ago, and, remembering the proverb about the "avaricious who pays twice", I decided that I need to buy a laser ....

We advise you to read:

Hand it for the smartphone ...