Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
|
Ultrasonic sensors can detect distances to a high degree of accuracy, even in many conditions where infrared sensors would not be suitable (e.g.: when natural light is a problem. For this example we used the HC-SR04.
After it receives a short pulse (> 10us) at its TRIGGER input, the module outputs from the ECHO output a pulse whose length is proportional to the distance of the object that is in front of the sensor. When a trigger is received the module emits an ultrasonic wave from its emitter and then receives the signal back in its receiver, measuring the time difference between the two.
According to the datasheet of this sensor, the following relation stands: time[us] / 58 = distance[cm] The dataseheet recommends a minimum interval of 60ms between triggers, in order to be able to read the result appropriately.
Bela sends out the TRIGGER event every 2646 samples(60ms) and then waits for a pulse to come appear on the ECHO pin. The PulseIn
class is used here to monitor a digital pin for an HIGH pulse and once the pulse termiantes, it returnes the duration ( in samples ) of the pulse.
The module requires a 5V power supply and its digital inputs and outputs are low at 0V and HIGH at 5V. Check the pin diagram in the IDE to see where to find the pins you need. It is important that the 5V ECHO output from the module is not connected straight to Bela's digital inputs, as that would most likely kill the Bela board (digital I/Os are 3.3V tolerant). You will need to use a passive resistor divider from the HC-SR04's ECHO output to scale down the voltage before connecting it to the digital input on gEchoDigitalInPin
.
On the scope you should see the pulses coming in from the trigger and the distance. The closer the object, the shorter the pulses. Make sure you set the trigger to "channel 1" (the pulse) and you set the horizontal zoom appropriately.