Click here for a free subscription to "HDE" our site newsletter.

Arduino Programming the HC-SR04 with Interrupts

First  2   3   Last   Next >>

How to make an ultrasonic echo range finder with an Arduino and HC-SR04

To get the full article and project details subscribe free to the HDE Magazine.

Arduino Uno with an HC-SR04 on mini breadboard

Have you ever wondered how they make the parking sensors that you seem to find on all new cars? You know, the ones that start beeping when you are getting close to an object you should be avoiding. The beeping gets faster and faster as you get closer until the beeps all merge into one and... too late you've hit it!

If you want to be able to examine the waveforms produced by this and other projects then get a copy of my book: Sound Card Oscilloscope and build yourself some very capable test gear.

I'm going to show you how you can make an ultrasonic range finder or parking sensor using an Arduino Uno, an HC-SR04 ultrasonic range finder module and a some simple software. To keep things simple and avoid having to purchase more components we will flash an LED on the Arduino board instead of sounding a buzzer.

Use interrupts to make your code better and faster

The software sketch that I'm going to give you makes use of interrupts. Don't worry, no matter what you might have been told, there is nothing scary aboout this. Interrupts are not magic but they can make your programs so much better if you use them properly.

When I looked around I found that all the examples of using the HC-SR04 with the Arduino used sketches that live exclusively in the main loop() of the sketch. This is ok if all you want to do is prove that the ultrasonic module is working but it isn't very convenient if you are expecting your software to do something smart with the information from the sensor. Putting all the code in the main loop makes it difficult to schedule your measurements and it introduces significant delays eats into the available processing time you have available. I thought that I would be different and show you a better way using interrupts.

If you want to learn more about using interrupts on the Arduino then take a look at this timer interrupt example.

How the HC-SR04 works

The HC-SR04 module has 4 pins:

  • VCC
  • Trigger
  • Echo
  • Ground

The module requires 5 volt power to operate so connect the Ground pin to one of the ground pins on the Arduino and the VCC to a 5V pin on the Arduino. That was easy.

When the HC-SR04 receives a short pulse on the trigger input the module begins a measurement. First it transmits a short ultrasonic pulse then it listens for the echo. The trigger pulse needs to be at least 10 µS long and will be delivered from pin 2 on the Arduino Uno.

When the ultrasonic burst of pulses has been trasmitted by the HC-SR04 the signal voltage on the Echo pin goes high. It will remain high for up to 25 mS depending on the distance from the object. If there is no object in view then the Echo signal will stay high for 38 mS. So all you have to do is measure the time during which the Echo signal is high. From that, together with the speed of sound, you can calculate the distance to the object.

This is where most examples of using this sensor fall short in my opinion. They all rely on the timing function pulseIn() to measure the duration of the echo pulse. To do this accurately in the main loop it can do nothing else until the pulse has completed which effectively wastes up to 38 mS every measurement waiting for a pin to change state. If you are doing this 5 times a second then that is 190 mS or 19% of the time wasted waiting for signals to change. You can do reduce that overhead by at least a factor of 10 by using interrupts to handle the pulse measurement.

You can also use one of the timer interrupts on the Arduino to ensure that your scheduled measurements take place when you want them to no matter what the code in the main loop is doing. This would be very difficult to achieve if the measurement was taking place in the main loop.

First  2   3   Last   Next >>


Now subscribe to our newsletter and don't miss a thing
 



Comments (13)

Topic: Arduino Programming the HC-SR04 with Interrupts
Full StarFull StarFull StarFull StarHalf Star 4.5/5 (9)
Facebookdel.icio.usStumbleUponDiggGoogle+Twitter
Full StarFull StarFull StarFull StarFull Star
Kim (South Korea) says...
amazing post of interrupt and operating ultrasonic module. great help for my advanced project! thx a lot Smile
11th August 2022 4:31pm
Full StarFull StarFull StarFull StarEmpty Star
Biomedical engineering project (India) says...
nice information
3rd March 2021 6:54am
Full StarFull StarFull StarFull StarFull Star
Fabri (Italy) says...
Hi, i'm testing you code on arduino mega,
is it possible that the only working pin for Echo signal is 2?
Why can't I put it on a random digital input, since the interrupt is generated by the timer?
20th April 2020 12:42pm
Full StarFull StarFull StarEmpty StarEmpty Star
Juan Alberto Guerrero Ramírez (Spain) says...
I´m quite new in Arduino and I´m trying to understanding interrupts and etc...

I guess this code is super, however, it is a bit difficult for me to understand.

In any case, many thanks for sharing and useful info.

Best Regards,
Juan Alberto
4th January 2020 1:02pm
ElysiumPro (India) says...
Excellent information about working HC-SR04. Thanks you so much for sharing the valuable information.
28th January 2019 11:31am
Full StarFull StarFull StarFull StarEmpty Star
embeddedschool (India) says...
Informative content about arduino programming.simple and Easy to understand embedded beginners. Thanks for sharing this kind of post
4th August 2018 10:23am
Mahdi (Iran) says...
hi
for my lesson i need your code .
can you send it for me please.
i observe publisher's rights, and introduce your site to my friends.
23rd June 2018 6:55am
Full StarFull StarFull StarFull StarFull Star
Mohamed says...
hello

i want to use input capture in the timer so i can see in your code that you are using timer and intrrupt , however is this intrrupt is ( the intrrupt for the timer ) or normal intrrupt , im asking about that cuz my all intrrupt pins is totaly busy and i want to use the input capture feature in the arduino mega ...??
11th May 2017 12:56pm
Full StarFull StarFull StarFull StarFull Star
Reema Shrestha (Nepal) says...
I've been trying to use Interrupt and timer for two ultrasonic sensors to measure time interval between when the first sensor's echo pin is HIGH to second sensor's echo pin is HIGH. So could help me with it?
16th February 2017 5:31pm
Forest Cooper (US) says...
I don't know those circuit details.
16th February 2017 5:56pm
Reema Shrestha (Nepal) says...
If you could,just help me with the algorithm.The circuit is similar to the one used above but I've used two sensors. When the moving object is detected by the first sensor then the interrupt should be called and timer should record the time until the object is detected by the second sensor. What I've been stuck to is,mode of Interrupts(). According to your code the timer measures the value from one state of the pin to the change of pin. But in mine the case should be HIGH for both. I mean when ... Read More
17th February 2017 11:09am
Full StarFull StarFull StarFull StarEmpty Star
Bob Jones (US) says...
Steve, I have been reading as much as I can find about how to program the HC-SR04 Ultrasonic Sensor using a timer interrupt. I got your sketch working and am adapting it, but hitting a question about delays. In your code, you use a for loop to simulate a delay. Your comment says that a delay won't work. So what I have done is use the micros() function to do the same. Only problem is that it doesn't work... yet. Would you be willing to take a quick look at my code and help me make it ... Read More
20th December 2015 4:52am
Full StarFull StarFull StarFull StarFull Star
Forest Cooper (US) says...
Hello, I am looking for 35 of the HC-RS04 modules and cards. Do you know where I can purchase these in the US? I need them in 1-2 days! Thanks for your help.
21st May 2014 4:36pm

Add Comment

* Required information
(will not be published)
 
Bold Italic Underline Strike Superscript Subscript Code PHP Code Quote Insert line Bullet list Numeric list Link Email Image Video
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form details on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
Click here for a free subscription to "HDE" our site newsletter.