ATmega8 HC-SR04 Ultrasonic Sensor Interface Demo

Ultrasonic Rangefinder HC-SR04 Interfacing with ATmega8

In this article I will describe how to interface a Ultrasonic Range Finder Module with a AVR ATmega8 microcontroller. I will provide a HEX file which you can burn into your ATmega8 directly to quickly test this whole setup.

Ultrasonic range finder modules helps find the exact distance to an object. This can have vast application in robotics.

These are more expensive than IR based obstacle detectors but the advantage is that they give accurate distance to the obstacle and also their performance is not affected by the colour of obstacle.

Ultrasonic Range Finder HC-SR04
Ultrasonic Range Finder HC-SR04

 

Things Required

 

S. No. Item Image
1

28 PIN AVR Dev Board

Contains the core AVR circuit including 5v regulator, reset, ISP.

AVR Development Board
2

16×2 LCD Board

Alphanumeric LCD Module subsystem on board.

16x2 LCD Board
3

Single Pin Female to Female Burg Wires

Used to interconnect the two boards. And the sensor.

 

Burg Wires
4

USB AVR Programmer

To upload the program to the development board.

USB AVR Programmer
5 Ultrasonic Range Finder Module
Ultrasonic Range Finder
6

 

12V 1A Power Adapter.

 

12V Adapter

Connect LCD Board

Please read this article on interfacing alphanumeric lcd module with AVR microcontrollers.

Connect the LCD Board with the development board using single pin f/f burg wires according to the table given below.

  LCD Board AVR Dev Board
  1 R/W PB0
  2 RS PB2
  3 LED PWM NOT CONNECTED
  4 VCC 5V OUT (VCC)
  5 GND GND
  6 DB7 PD6
  7 DB6 PD5
  8 DB5 PD4
  9 DB4 PD3
  10 E PD7

 

lcd interface with atmega8

Fig. LCD Board and Dev Board Connected

Connect the Ultrasonic Sensor Module.

Connect the Ultrasonic Sensor Module with AVR Dev Board according to the table given below.

  Sensor AVR Dev Board
  1 VCC VCC
  2 Trig PC0
  3 Echo PC1
  4 GND GND

Connection for xBoard MINI v2.1

You can also use the xBoard MINI v2.1 development board to run this demo. The connection is same as the 28 PIN Development board with a difference that you don’t need the LCD board because xBoard MINI v2.1 has built in LCD Module. So you just need to connect the sensor module on PORTC pins and supply the module from 5 volt output pins. See the image below.

xBoard MINI interfaced with HC-SR04

Fig. xBoard MINI and HC-SR04 Sensor

 

Supplying External Boards

External boards such as the LCD Board and Ultrasonic Range Finder Module requires 5v supply. You can easily supply them from the extra power output from the development board. The image below shows their position.

AVR28DEV Extra 5v Output

Fig. Extra Power Output

 

Burn the Program

Download the ready made program given below and burn it into your development board.

to burn the program you need a USB AVR Programmer as shown below. This will help you transfer the program from your PC to your development board.

USB AVR Programmer

Fig. AVR Programmer

Connect the programmer with the development board using a 10 pin FRC cable, then connect the programmer with your PC using USB Cable. On PC, launch eXtreme Burner – AVR, it is the software interface to command the programmer.

AVR28DEV Programming

Fig. Programming Setup

 

eXtreme Burner AVR

Fig. eXtreme Burner – AVR

Verify the Performance

To verify the performance, keep the sensor horizontally over a flat surface. Put a measuring scale in from of the sensor on the same surface. Then bring your palm in from of the sensor. You can know the distance of your palm from the sensor by the measuring scale. This same reading (in cm) should be shown on the LCD. This verifies that the demo is working perfectly !

ATmega8 HC-SR04 demo

Fig. Testing HC-SR04

 

ATmega8 ultrasonic interface demo

Fig. Testing HC-SR04

Schematic

ATmega8 LCD HC-SR04 Schematic

Fig. Schematic for HC-SR04 interfaced with ATmega8

Part List

S. No. Reference Value Part Qty
1 R1 100 Ohms Resistor 1/4 watt 1
2 R2 330 Ohms Resistor 1/4 watt 1
3 R3 4.7K Ohms Resistor 1/4 watt 1
4 C1 470uF Electrolytic Capacitor 25V 1
5

C2,C5,C6,C7

0.1uF(100nF) Ceramic disk type capacitor 4
6 C3,C4 22pF Ceramic disk type capacitor 2
7 D1 1N4007 Diode 1
8 D2 5mm LED – Round – RED 1
9 U1 ATmega8 ATmega8 MCU 1
10 U2 7805 5V Voltage Regulator 1
11 L1 10uH Inductor 1
12 RV1 10K Ohms Preset (Variable Resistor) 1
13 SW1 On/Off Switch 1
14 X1 DC Socket 1
15 X3 16MHz Crystal 1
16 X4   HC-SR04 Sensor 1
17 28 PIN IC Socket 1
18 16 PIN Female Connecter 1
19 4 PIN Female Connecter 1
20 16 PIN Male Connecter 1
21 DISP1 LCD Module 16×2 GREEN B/L 1

 

 

Downloads

Video Demo

Buy this project

Buy this project fully assembled and tested !

ATmega8 HC-SR04 Distance Measure Project

Fig. Made on custom PCB

Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.

Avinash

Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

72 thoughts on “Ultrasonic Rangefinder HC-SR04 Interfacing with ATmega8

  • Pingback: Interfacing Ultrasonic Rangefinder with AVR MCUseXtreme Electronics

  • By Precious - Reply

    Hello, this is good.I haven’t tried the code yet but from looking through it, I can tell it works fine. A few days ago I built mine using multiplexed 7 segment display because I had problems connecting my 20×4 lcd with your lcd library which I have being using for my 16×2 lcd projects.I am really grateful for all the time you spend in creating these projects.
    I will now try this one hoping that the lcd library in the project will work for my 20×4 lcd. Thank you very much….

    • By vishal - Reply

      Thank you.

    • By manac21 - Reply

      I would like to use 3 seven segment displays instead of LCD.. could this be easily modified and how?

    • By bhavsar gaurav - Reply

      i am final year student,my project is on ultra sonic distance meter….so,i want programming code for this……but without using arduino kit,plese reply me fast

      • By Avinash -

        @Bhavsar,

        This project don’t use Arduino ! baby! Please read bit carefully before vomiting !

    • By Sourour - Reply

      Bonjour ,

      je veut savoir le code de la carte atmega8. Pouvez vous m’aidez s.v.p

  • By Precious - Reply

    I just built it using a 20×4 lcd and it works just fine. thank you

  • By Liem Do - Reply

    Hello , i’ve got an idea to build stairs light using this ultrasonic sensor. This sensor will tell me quite exactly the position of anyone stepping up or down the stairs , then controller will decide which stair light will be turned on. Thank you for your demo.

  • By Kumar Gyanesh - Reply

    can u give us tutorial on smart cards .. it’s design
    or u can even give me the link where i can study about it

  • By Kamal Babu - Reply

    Good Job Abhinash !!!!!

  • By Tante Bohay - Reply

    Awesome website Sir…

  • By Kumar Gyanesh - Reply

    Sir, I want to know how the Echo Sensor works
    and want o/p it produces And how Atmega 8 is able to recognize it.
    I want the whole method Not jst the code so that i can connect to somewhere else except PC0 , PC1

  • By Entropy - Reply

    Hi Avinash, if you can see this question and answer it as soon as you could it will be mostly appreciated! i have a univeristy project due right 2days fron now and was using this sensor to do some distance measurements, but I am using a plain Atmega 32 without 16MHz Cystal. i tried running this code but it only keeps displaying 112 cm on screen this seems to me like some sort of bad timming so i tried to change TCNT1 to 30000 insteady of 60000 in my case but i still get LCD displays error! plz help me as it’s a matter of time before it’s too late.

    Many Thanks.

  • By µOnkel - Reply

    Hello,
    it’s a very fine projekt and works well!

    Is it possibel to get the quellcode ??? I hope so

    Because i’d like to use this projekt on an Atmega32-Board for an additional applikation.

    • By Avinash - Reply

      @µOnkel

      Thanks for feedback !

  • By sherjeel - Reply

    i have to buy this project how much is its price??

    kindly inform me..
    and reply

  • By sherjeel - Reply

    kindly send me the programme ist

    please

  • By Gaurav - Reply

    Hey,
    i wanted to know whether i can build this whole project with the 6 things that you listed above in “things required” or do i need some other parts along with these…..

    please reply fast as i have to order these things latest by tomorrow….and also can you please tell me how much time would it take you guys to deliver these to my address in Delhi.

    • By Avinash - Reply

      @Gaurav,

      Yes, you need only those six parts only.

      delivery in New Delhi will take about 4-6 working days.

  • By nirmal kumar - Reply

    can u provide the c code for reference?

    • By Avinash - Reply

      @Nirmal,

      You should have had a look at the download section (that means read the article “PROPERLY”).

      If you should have looked their.

      Their was the “Complete Atmel Studio 6 Project”

      that means the whole project can be loaded on AS6 (Atmel Studio 6)

      That project will contain the C code of the application and the libraries.

      • By nirmal kumar -

        sorry thanks 🙂

  • By tasneem - Reply

    hi avinash,
    can i burn the program using avr studio 4??
    if not than what changes should i make in the program to burn into the ic using as4.
    thanks in advance

  • By yulvianto - Reply

    Sir..can you tell me if use glcd 128×65..and progaraming with cvavr…

  • By Kshitij Bajracharya - Reply

    Can you please tell me how i can get the HC-SR04 in proteus for simulation?

  • By abhishek - Reply

    which one will you prefer to do with atmega 16 or 8.. becuase we are trying to incorporate this code with line follower code and we r using atmega 16.. thank you 🙂

  • By aditya kushwaha - Reply

    sir,
    I am using gh 311 rt ultrasonic sensor, 3 pin sensor, giving pwm output, can u please help me at finding distance using same.

  • By saloni - Reply

    can u plz tell how to multiplex the leds

  • By samim - Reply

    Plz give the working principle of the sensor and explanation of the c code.
    thanks 🙂

    • By Kim - Reply

      Hey!
      you get a working principle since july?
      greetings.
      Kim

  • By Quely - Reply

    hello
    I have a wonder that employ a loop of 600,000 in the first ‘for’ ??
    and the second?
    and because you expect the timer is greater than 60000?

    thanks

  • By SAEED MAHDAVI - Reply

    Dear sir:
    Thanks a lot for your useful circuit offered to us in your web page. I hope I could make it and use it in the back of my car to see how far I have to run into the back car parked behind mine. Again, so cordially I thank you specially for the HEX file attached to your web page. It was very kind of you
    I hope to see such circuits more and more in your web site.
    Your Sincerely Saeed M.

  • By SAEED MAHDAVI - Reply

    Dear Avinash:
    fortunately I managed to build your project, however to my surprise I saw the distance shown on the LCD screen is 1/16 of the real distance, would you please let me know what the reason is and how I can fix this problem?
    Cordially thankful
    Saeed Mahdavi

    • By Mehmet - Reply

      I am having a similar problem. I didn’t use a development board. I just burned the hex to Atmega8 using AVRDUDE. It shows 0000cm until about 15 cm and starts showing 0001cm. When you move the object 10 – 15 cm further it shows 0002cm. Move another 10-15 cm further and it shows 0003cm and so on. These changes take about 5 seconds to update the LCD so the response is very slow as well. What am I doing wrong? Or could there be something to do with the code? I have tried the circuit with 3 different HCSR04 modules and all the same.

      • By Avinash -

        @Mehmet, ok we will help you with that. But please tell us from how long you are reading our site?

      • By sairam -

        i think the problem is with your external clock.please use 16 MHz clock to get the exact results,as the timer pre scaler and other things are optimised for 16Mhz clock.by default the atmega8 will be clocked at 1MHz.which is 16 times slower than 16 Mhz.

  • By SAEED MAHDAVI - Reply

    Mr. Avishal
    i am still awaiting your guidance.

  • By fardin azadi - Reply

    Hello dear.
    I have used the site and your content so thank you. Please leave a tutorial about how ARM microcontroller thanks

    • By SAEED MAHDAVI - Reply

      salam agha fardin afzali:
      man saeed hastam ahle esfahan, kheili alagheh mand be microcontroller ha hastam. va az kar kardan ba oon ha lazzat mibaram.
      shoma ahle koja hastid ?
      dar barehe micro controller ha cheghadr midoonid ?
      rastesh in madar ro man montaj kardam vali meghdar neshoon dadeh shodeh dar rooye LCD 1/16 meghdare vaghehi bood fekr konam dar neveshtane barnameh eshtebehi kardeh bashand.
      montazere javabetoo nhastam
      saeed mahdavi

    • By SAEED MAHDAVI - Reply

      salam agha fardin azadi:
      man saeed hastam ahle esfahan, kheili alagheh mand be microcontroller ha hastam. va az kar kardan ba oon ha lazzat mibaram.
      shoma ahle koja hastid ?
      dar barehe micro controller ha cheghadr midoonid ?
      rastesh in madar ro man montaj kardam vali meghdar neshoon dadeh shodeh dar rooye LCD 1/16 meghdare vaghehi bood fekr konam dar neveshtane barnameh eshtebehi kardeh bashand.
      montazere javabetoo nhastam
      saeed mahdavi

  • By Mehmet - Reply

    Hi Avinash, My interest in electronics started a few years ago. I must have come across your site but probably didn’t spend much time as I was just beginning and most of this site’s content is about MCUs. When I was searching about distance measurement projects a few weeks ago, I have seen this project. I already had the HCSR04 module so I ordered an Atmega8 and set it up on breadboard. Thanks

  • By mehmet - Reply

    Hi Avinash, I sorted it out. In the forum of this website, I found the fusebits settings for Atmega8 for 16mhz external crystal ( lowfuse 0xFF , highfuse 0xC9 ), applied them to Atmega8 and voi la , it works perfectly now. I found everything I need here. Thanks for everything.

  • By raju yadav - Reply

    hello
    Thanks for this tutorial. its very useful to beginners.
    i have update your code in Atmega16 micocontroller thanks once again .

  • By Sandeep - Reply

    Instead of atmega 8 m using atmega 16. so pls provide me the hex file for it.

  • By mahendra - Reply

    Dear Avinash sir i am working with ultrasonic module HC-SR04 can u please provide the code for interfacing atmega32 with this ultrasonic sensor module………..

    thanking you sir,
    yours faith fully,
    mahendra.

  • By Tasos Gkiouzepas - Reply

    I read your tutorials, and took a clear idea for AVR. Great great work!!!
    Thank’s a lot Avinash and rest of the team.

  • By rahul - Reply

    its really good
    can I get the code that I can use on atmega 8 oor 16

  • By Horpeyemmie Temmietope Gold - Reply

    Thanks for the manual and files, The program worked but it’s slow in response and the measurement is inaccurate e.g a distance of over 1m will show 7cm. Please you assist on what to do

    • By Avinash - Reply

      Your MCU is running slow, this is because either you are using a slower crystal oscillator or have NOT configured the MCU to use the external oscillator.

  • By Pieter - Reply

    Is it possible to give me a new link where I can download the program, because the link in this article doesn’t work any longer.
    Thanks, Pieter

  • By Neerendra - Reply

    Hello Avinash,

    Can you guide to interface LCD,LED & Buzzer on the response of Ultrasonic sensor?
    How can i make a program ?

    Guide me please.

    Thank you,
    Neerendra Singh

  • By Nikhil - Reply

    sir i am unable to download the code..it says account is banned. what to do?
    my email id is nikhilpnp@yahoo.com

    • By Avinash - Reply

      @Nikhil,

      Where do you live? Address?

      • By Nikhil -

        Mumbai, Maharashtra

      • By Avinash -

        @nikhil,

        Sorry but we only give files to our friendly customers.

  • By Devi - Reply

    Hii, I need a program code to detect crack on railway track using atmega8 and hc sr04.Please send me

    thank you

  • By ninny - Reply

    hello sir,
    when I go on your review page link and try to publish my review, the site says “we are temporarily unable to publish your review”
    What to do?
    I really found your code extremely helpful.

  • By TUSHAR GANDHI - Reply

    I have used 8MHz crystal in ATMEGA32 so where i have to change the code because it shows wrong distance.

  • By gagandeep - Reply

    please attach the lcd file of the above program

  • By Muhammad Faris - Reply

    wow this good modules thank you for the share

  • By Areeha - Reply

    Thanks for the assistance but i aint getting few of the things like why are we taking the factor of 6000000? and that what is this line doing in the loop?
    if(!(US_PIN & (1<<US_POS))) continue; else break;
    i mean what are we checking here?
    Secondly,why and how are this 0xffff and 0xfffe used for here?

  • By Andrei - Reply

    Hello, I tried to make this project in Proteus 8, but it’s not working. And if I have ATMEGA328P-PU, the hex file will work on my Microcontroller? Thanx!

  • By Sidharth - Reply

    Hi avinash,
    There are lot of articles about avr vs arduino. Would required your suggestion in this?

  • By Nagesh - Reply

    very useful article it was very helpul

  • By Nagesh - Reply

    what factor can i use for 8MHZ crystal instead of 6000000 Thanx in advance

    • By Avinash - Reply

      @Mr. Nagesh,

      May I know what is the reason to use a 8MHz crystal in place of a 16MHz?

Leave a Reply

Your email address will not be published. Required fields are marked *


three − = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>