Next level ultrasonic sensor

How to get better results with HCSR04.

Jul 26, 2020

21037 views

20 respects

Components and supplies

1

Jumper wires (generic)

1

Arduino UNO

1

Ultrasonic Sensor - HC-SR04 (Generic)

Project description

Downloadable files

Wirings of HCSR04

Wirings of HCSR04

Comments

Only logged in users can leave comments

fatfenders

a year ago

Disabling interrupts. Bingo.

Anonymous user

2 years ago

MY GOD MAN! THANK YOU, THANK YOU, THANK YOU! You're a godsend. I just almost gave up on it! Please know that you are greatly appreciated. A suggestion: Please consider changing the name of the article. I tried searching for all sorts of ways to improve the accuracy of this sensor, but couldn't find your piece. Your comment on https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380 finally showed me this lifesaver! I think more people might benefit from this if you made it easier/more likely to be found by google searches. Something like "improving accuracy of ultrasonic sensor", or something. Kudos man! Thanks again!

dmytrosavchuk

2 years ago

Very nice project - thanks! Did you try to use the minimum or the median of several consequential readings instead of the mean to fix 25ms peaks issue? From your graph it appears that most of the readings correspond to the first peak, so the median will probably be more reliable. Or one can use some statistical tools to detect the smallest mode in the data.

Anonymous user

2 years ago

Very useful finding indeed! Besides the code to improve the accuracy of HC-SR04, do you also find a solution to improve the reliability of distance measurement? Sometimes the HC-SR04 sensor can detect an object at a certain distance, but sometimes it doesnot.

panagorko

2 years ago

I don't know if there is any option to do this. If the wave didn't return HCSR04 times out and return ~14000 µs. This means that an object is too far away or was too small etc. Throwing away timed out results would improve that if that's the case.

Anonymous user

2 years ago

Thanks :) I was going to throw mine away soon. Procesing the results is entirely code based, and I can take it from there.

Anonymous user

2 years ago

I THOUGHT ECHO PIN WAS INPUT AND TRIG PIN WAS OUTPUT.

Anonymous user

2 years ago

I thought timers were independant of the interrupts, except for the timer interrupt of course. Thanks.

panagorko

2 years ago

Yes, there is an option to use hardware to measure time. The problem is that nobody says that in tutorials. Everyone is using the pulseIn function which is basically a loop that checks if signal is low/high once per µs. PulseIn can be replaced with hardware timer or upgraded with noInterrupts/Interrupt, this method is simpler and I wish that every tutorial in a short time will use this.

Anonymous user

2 years ago

I love your explanation! Thanks for the detailed post!

Anonymous user

2 years ago

Im pretty new to arduino and coding. Is the above code complete? I am running into some problems.

eyetengu

4 years ago

Im pretty new to arduino and coding. Is the above code complete? I am running into some problems.

mikethepariah

4 years ago

I love your explanation! Thanks for the detailed post!

Anonymous user

4 years ago

MY GOD MAN! THANK YOU, THANK YOU, THANK YOU! You're a godsend. I just almost gave up on it! Please know that you are greatly appreciated. A suggestion: Please consider changing the name of the article. I tried searching for all sorts of ways to improve the accuracy of this sensor, but couldn't find your piece. Your comment on https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380 finally showed me this lifesaver! I think more people might benefit from this if you made it easier/more likely to be found by google searches. Something like "improving accuracy of ultrasonic sensor", or something. Kudos man! Thanks again!

dmytrosavchuk

4 years ago

Very nice project - thanks! Did you try to use the minimum or the median of several consequential readings instead of the mean to fix 25ms peaks issue? From your graph it appears that most of the readings correspond to the first peak, so the median will probably be more reliable. Or one can use some statistical tools to detect the smallest mode in the data.

Anonymous user

4 years ago

I THOUGHT ECHO PIN WAS INPUT AND TRIG PIN WAS OUTPUT.

Anonymous user

4 years ago

I thought timers were independant of the interrupts, except for the timer interrupt of course. Thanks.

panagorko

2 years ago

Yes, there is an option to use hardware to measure time. The problem is that nobody says that in tutorials. Everyone is using the pulseIn function which is basically a loop that checks if signal is low/high once per µs. PulseIn can be replaced with hardware timer or upgraded with noInterrupts/Interrupt, this method is simpler and I wish that every tutorial in a short time will use this.

Anonymous user

4 years ago

Very useful finding indeed! Besides the code to improve the accuracy of HC-SR04, do you also find a solution to improve the reliability of distance measurement? Sometimes the HC-SR04 sensor can detect an object at a certain distance, but sometimes it doesnot.

panagorko

2 years ago

I don't know if there is any option to do this. If the wave didn't return HCSR04 times out and return ~14000 µs. This means that an object is too far away or was too small etc. Throwing away timed out results would improve that if that's the case.

Anonymous user

4 years ago

Thanks :) I was going to throw mine away soon. Procesing the results is entirely code based, and I can take it from there.

ShreyanR

4 years ago

A very interesting project! This is a wonderful way to improve the accuracy of an HC-SR04 Ultrasonic Sensor!