Arduino Programming the HC-SR04 with Interrupts
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.
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:
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.
|Now subscribe to our newsletter and don't miss a thing|