HC-SR04 Sonar with Atmega8 microcontroller

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.

Circuit diagram:

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)
sonar merge 001 sonar merge 002 sonar merge 003
glide sonar eagle

Source code:
Sonar HC_SR04
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.
5110_lcd_1 5110_lcd_2

This article has 23 Comments

  1. Salut,
    multumesc pentru tutoriale (foarte didactice si practice ), insa am o intrebare: a incercat cineva
    varianta acvatica ( gen navigatie/pescuit ) ?

    Mutumesc si cele bune!

  2. Salut Livius, nu am vazut pe nimeni sa incerce asta, dar idea e foarte interesanta , si cred ca are sanse sa functioneze.

  3. hi radu,
    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

  4. 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

  5. hi radu,
    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…

  6. 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?

  7. 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?

  8. @Alex,

    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.

  9. 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?

    Thank you!

  10. Salut. Pe ce port ai legat lcd-ul? Am vazut ca folosesti libraria lcd/hd44780, eu foloseam una externa si acolo imi defineam portul.

  11. 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?

  12. can u please send code for distance measurement for hc-sr04 with atmega16 in codevisionavr plzzzzzzzz with initialisations and everythingg

  13. Feel free to follow the instructions presented here. You have everything you need right before your eyes.

Leave a Reply