The HC-SR04 is an Ultrasonic ranging module. It provides 2cm – 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The module includes the ultrasonic transmitter, receiver and control circuit.
The basic principle is:
(1) Using IO TRIG Pin, trigger for at least 10us high level signal ,
(2) The Module automatically sends eight 40 kHz and detect whether there is a return signal.
(3) If the ultrasonic signal returns, the time elapsed is used to compute distance, knowing the sound speed in air: Test distance = (high level time×velocity of sound (340M/S) / 2
Datasheet: available here.
Here are some variants created by my readers
Glide built a nice PCB for this sonar. Here are his pics and the eagle files (SCH+PCB)
glide sonar eagle
Thanks to wzona. It comes with excellent details, make sure you check it out.
A few pics with the sonar used with a 5110 Nokia graphical LCD. Code for the LCD available here.
This article has 23 Comments
multumesc pentru tutoriale (foarte didactice si practice ), insa am o intrebare: a incercat cineva
varianta acvatica ( gen navigatie/pescuit ) ?
Mutumesc si cele bune!
Salut Livius, nu am vazut pe nimeni sa incerce asta, dar idea e foarte interesanta , si cred ca are sanse sa functioneze.
i have very little knowledge of the micro controllers and its related things so i have directly made circuit which u have provided here and burned the atmega8 with the code from the above links
but still i am not getting the results,
can u plz help me in this matter
thanks in advance
@Parag, you also need to set the fuses for the external Crystal.
thanks radu for the valueable information but i still have a small doubt.
the doubt is that the atmega8 ic has internally calibrated 16mhz oscillator and we are using 16 mhz oscillator separately
so do we still need to edit the fuse to run the program correctly???
and i have one ready made kit of a company which has connected 8mhz oscillator externally but in the demo programs given by them, they alwys define F_CPU 1000000, So can u plz tell me what actually they have done means what is the meaning of defining F_CPU separately.
thanks in advance
thank you so much for your advice.
i have read your article of fusing bits and now my range finder is working good.
thank you so much brother…
@parag: perfect, glad to hear that.
Hi, please someone to send me the Source code, I can not download it, my email adress is: firstname.lastname@example.org
ps: sunt si eu roman!!!
Ma bucur sa vad co-nationali de-ai mei pe aici. Am verificat atasamentul si merge. Te rog incearca din nou, click sub “Source code”, link direct: http://www.pocketmagic.net/wp-content/uploads/2012/11/Sonar-HC_SR04.zip
Da asa este , linkul merge, cred ca era o problema cu browserul.
Hi , can someone explain me what is the role of the capacitor beetween AGND(pin 22) and the connection(Avcc and ARef), I read about Agnd , avcc and aref in datasheet but why you placed the capacitor there, it has a some kind of filter role or what?
Salut Radu, multumesc pentru raspuns, as avea totusi o intrebare dar mai intai sa spun ce proiect am in momentul de fata, trebuie sa fac un modul senzorial format din: un uC, folosesc ATMEGA8-16PU , 5 senzori de ditanta ca cei pe care ii folosesti si tu in proiectul de mai sus, un senzor Accelerometru+Gyroscop si un senzor de culuare, modulul trebuie sa comunice prin I2C cu un alt uC care va avea rol de Master.
Intrebarea mea: Este indicat sa conectam direct senzorii direct la microcontroller, fara sa folosim rezistente in conformitate cu necesarul de curent pe care fiecare senzor il cere ca sa functioneze normal, sa nu trebuie sa folosim neaparat rezistente avand in vedere faptul ca lucram cu tensiuni mici, iar senzorii nu sunt sarcini inductive si ca un moment dat ar putea “absorbi” mai mult crt decat pot duce?
The role of the capacitor on pin 22 (GND) is to work as a filter against various interferences. You should place it as close as possible to the uC.
Sonarul poate fi folosit si fara rezistente ,dar pentru alti senzori poate avem nevoie , depinde de la caz la caz.
Hey, there is something going horribly wrong when there is no object in range of the sensor. The result variable is jumping around randomly. Is there anything that could be done about it? I only need about 1 meter of distance checking (garage parking sensor). The random values are making quite a mess. I have tried to ignore the reading when it’s lower than 1, but that didn’t help at all. I also fiddled around with the response time limits but had no success at all. Could you please help me?
Salut. Pe ce port ai legat lcd-ul? Am vazut ca folosesti libraria lcd/hd44780, eu foloseam una externa si acolo imi defineam portul.
Hi, please someone to send me the Source code, I can not download it, my email adress is: email@example.com
Salut, am o problema, am introdus codul in codeblocks si codevisionAVR si pe ambele imi da eroare ca nu gaseste #include
Am cautat compielere dar nu m-am descurcat ( sunt incepator), imi poti explica care e problema ,te rog?
#include avr/io.h *
Foloseste AVR Studio si WinAVR.
can u please send code for distance measurement for hc-sr04 with atmega16 in codevisionavr plzzzzzzzz with initialisations and everythingg
please i need it urgently
Can you please send me code to read ultrasonic sensor readings using atmega8.
Feel free to follow the instructions presented here. You have everything you need right before your eyes.