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

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 (2)

Topic: Arduino Programming the HC-SR04 with Interrupts
Full StarFull StarFull StarFull StarHalf Star 4.5/5 (2)
Facebookdel.icio.usStumbleUponDiggGoogle+Twitter
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. *
 
 
Powered by Commentics
Click here for a free subscription to "HDE" our site newsletter.