Non-blocking Ultrasonic Sensor For Arduino : 3 Steps - Instructables
Maybe your like
The HC-SR04 ultrasonic ranging module is a fantastic addition to any robot project. Its a very simple and cheap way to track a robots progress (assuming we have fixed reference points) over a reported range of 2-400cm with accuracies of 3mm possible. It's also a great way to add object avoidance to stop your precious creation crashing into the scenery.
The modules operation is very simple. When triggered with a 10us pulse on the trigger pin the module sends out eight 40kHz ultrasonic pulses. The module then takes the echo pin high and holds it there until it receives an echo back to the module. The length of time the echo pin is high is directly proportional to the distance to an object. In fact, this time is the time it takes the pulses to travel to the object and back and, because we know the speed of sound at 20°C in dry air at sea level in 343.2m/s, we can use the following formula to work out the distance from the module to the object:
distance (m) = (time(s) * speed (m/s)) / 2
As we are going to measuring time in microseconds its easy for us to work out how far sound travels per microsecond and simply multiply our measured time by this number.
343.2m/s = 0.0343.2cm/us
So now we can say:
distance (cm) = (time(us) * speed(cm/us)) / 2 = (time * 0.0343.2)/2
As multiplying and dividing are expensive steps (in terms of time) on a microcontroller we may as well divide our speed constant by 2 now and save repeating the effort later, thus:
distance (cm) = time(us) * 0.01716
Now, we are going to measuring an integer (whole) number of microseconds and we know that floating point mathematics is less efficient on a microcontroller than the integer equivalent. So, rather than multiplying our measured amount of time by a floating point number we could divide it by an integer because 0.01716 is approximately the same as 1/58, thus:
distance (cm) = time(us) / 58(cm/us)
By using the same method we can determine that the distance to an object in inches is as follows:
distance (in) = time(us) / 148(in/us)
So, now we know how the sensor works, how to use it and how to get a useful value from its output. The final problem, for me anyway, is that waiting for the echo pin to go low will block our programs execution. This means that whilst waiting for the module to take a reading we can't do anything else. Given the modules range of 400cm we could be waiting for as much as 23ms. Now, this may not seem like much, but for a robot which has an iterative control loop and has lots of other things to be doing (controlling motors, gathering other sensor data) this is a significant delay.
Thankfully, there is a way that the microcontroller can tell us when the echo pin changes rather than us having to watch and wait. This mechanism is called an 'interrupt' because the microcontroller literally interrupts the execution of the program when an interrupt condition occurs and call a function of our choosing. And this is the method I've used for this example. When the echo pin goes high at the beginning of the ranging our interrupt service routine (ISR, out function which deals with the interrupt) is called and we save the current time as our start time. When the echo pin goes low at the end of the ranging our ISR is called again and we save the current time as our end time. The difference between the start and end times is the time taken for the ranging. Simple! There are lots of resources out there that describe how interrupts far better than I could and I've already written far to much here! Also, I've wrapped the code up in a little library, so if you want to use it and don't care how interrupts work, you don't need to.
Tag » Arduino Hc-sr04 Interrupt
-
Simple Arduino HC-SR04 (HCSR04) Distance Detection Using ...
-
Generating An Interrupt From HC-SR04 Ultrasonic Sensor
-
Reading The HC-SR04 With Timer1 And No Interrupts - Arduino Forum
-
Arduino Programming The HC-SR04 With Interrupts
-
Arduino Interrupt For Ultrasonic Sensor - YouTube
-
Arduino Programming The HC-SR04 With Interrupts - YouTube
-
Interrupt Arduino When A Particular Value Is Read By A Ultrasonic ...
-
How To Measure Distance Using Ultrasonic Sensor
-
Arduino Multi-tasking - The Online Shed
-
HC-SR04 Sensor Timer Interrupt Issues - AVR Freaks
-
Arduino Interrupt Tutorial
-
Trying To Use Ultrasonic Sensor With Interrupts - ESP32 Forum
-
Arduino - En Utilisant Les Interruptions De Gels De Traitement Et De ...
-
Interrupt | Cộng đồng Arduino Việt Nam