Measuring distances with ultrasonic sensors
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.
#include <stdlib.h>
#include <libraries/Scope/Scope.h>
#include <libraries/PulseIn/PulseIn.h>
int gTriggerInterval = 2646;
int gMinPulseLength = 7;
float gRescale = 58;
unsigned int gTrigDigitalOutPin = 0;
unsigned int gEchoDigitalInPin = 1;
int gTriggerCount = 0;
int gPrintfCount = 0;
{
pinMode(context, 0, gTrigDigitalOutPin, OUTPUT);
pinMode(context, 0, gEchoDigitalInPin, INPUT);
pulseIn.setup(context, gEchoDigitalInPin, HIGH);
return true;
}
{
gTriggerCount++;
bool state;
if(gTriggerCount == gTriggerInterval){
gTriggerCount = 0;
state = HIGH;
} else {
state = LOW;
}
int pulseLength = pulseIn.hasPulsed(context, n);
static float distance = 0;
if(pulseLength >= gMinPulseLength){
distance = duration / gRescale;
}
static int count = 0;
if(count > 5000){
rt_printf("pulseLength: %d, distance: %fcm\n", pulseLength, distance);
count = 0;
}
++count;
scope.log(
digitalRead(context, n, gEchoDigitalInPin) * 0.5, distance/100);
}
}
{
}
A class to measure the duration of a pulse on a digital pin.
Definition PulseIn.h:12
An oscilloscope which allows data to be visualised in a browser in real time.
Definition Scope.h:23
static int digitalRead(BelaContext *context, int frame, int channel)
Read a digital input, specifying the frame number (when to read) and the pin.
Definition Bela.h:1518
static void digitalWrite(BelaContext *context, int frame, int channel, int value)
Write a digital output, specifying the frame number (when to write) and the pin.
Definition Bela.h:1525
static void pinMode(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition Bela.h:1548
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition render.cpp:68
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition render.cpp:51
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition render.cpp:96
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const uint32_t digitalFrames
Number of digital frames per period.
Definition Bela.h:365
const float digitalSampleRate
Digital sample rate in Hz (currently always 44100.0).
Definition Bela.h:371