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

Arduino Programming the HC-SR04 Project Interrupt Driven Software

Software sketch for the Arduino HC-SR04 programming project

If you don't have an oscilloscope then get a copy of my book: Sound Card Oscilloscope and build yourself some very powerful test gear to examine the inner workings of the HC-SRO4.

The sketch below takes advantage of the ATmega328 16 bit timer 1. Before you can use the code you must download and install the TimerOne library.

Download the timerOne library from http://code.google.com/p/arduino-timerone/downloads/detail?name=TimerOne-v9.zip&can=2&q= and install it using the guide http://arduino.cc/en/Guide/Libraries

Copy and paste the sketch below

Load the sketch onto your Arduino Uno and let it run. If all is well then you will see the Arduino onboard LED start to flash. This LED is located near to the USB connector. Place an object in front of the HC-SR04 sensor and vary the distance. The flash rate of the onboard LED will change to reflect the distance. Just like the parking sensor on a new car.

How the program works

The code sketch above is well commented but I will explain briefly how it works here.

When the program is loaded the first thing it does is execute the setup() function. The various pins of the Arduino are set to input or output as appropriate. Timer 1 is initialized, the interrupt service routines attached to the appropriate interrupts and the serial port is initialized.

The background loop() function then begins to run. Any routines placed in loop() are executed continuously. All of the code required to handle the HC-SR04 and the onboard LED is contained in interrupts routines so the only task in the loop() function is to send the distance measured in centimeters to the serial monitor port every 100 mS.

timerIsr() is called every time the timer 1 times out and creates an interrupt. The simplify the code this function calls two more functions. One to handle the trigger pulse and the other one to flash the onboard LED.

The trigger_pulse() function is called from timerIsr() every 50 uS and creates a trigger pulse every 4000 calls or 200 mS. The pulse generator is implemented with a simple finite state machine contained in the switch() statement. Every time this routine is called exactly one of the cases is executed. When the count reaches 200 mS the state is changed from 0 to 1 which immediately causes the trigger output to be set high and the state changed to 2. When the function is next called 50 mS later, case 2 is called which resets the trigger output to low and the state reset to 0.

echo_interrupt() is called every time the echo input signal changes state and generates an interrupt. The echo input is tested to determine whether this is the start or end of the echo pulse and start and end times recorded accordingly. If the input has changed from high to low then this is the end of the pulse and the duration is computed.

The distance_flasher() function is called from timerIsr() every 50 uS. A counter is decremented and if it reaches zero then the onboard LED is toggled and the counter is reloaded with the value of the computed echo pulse duration. This has the effect of flashing the LED faster the closer the object is to the sensor.

If you have followed this project you should now have a working model of an ultrasonic range finder or parking sensor and along the way you may have picked up a few new skills. Now please sign up to our free newsletter.



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



Comments (5)

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