Library for interfacing HC_SR04 ultrasonic sensor under MbedCE.
- Create a new project according to MbedCE instructions
- Add this as submodule to your project via
git submodule add --depth 1 https://github.com/mbed-ce-libraries-examples/HC_SR04 HC_SR04
- The top level
CMakeList.txt
(in root of your project) should be modified according to this wiki page - Create your main.cpp file and copy & Paste example code below.
- Build the project
#include "mbed.h"
#include "HC_SR04.h"
void dist(int distance)
{
//here put a code what should happen after the distance is measured
printf("Distance %dmm\r\n", distance);
}
/* Set the trigger pin to D8 and the echo pin to D9.
Rate of updates every 500ms with a timeout after 1sec.
The dist is called after the distance is measured */
HC_SR04 mu(D8, D9, 500ms, 1s, &dist);
int main()
{
printf("MbedCE_lib_example_HC_SR04\r\n");
//start mesuring the distance
mu.startUpdates();
while(1)
{
//Do something else here
/*call checkDistance() as much as possible, as this is where
the class procesed call of dist function.*/
mu.checkDistance();
}
}
https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
This library was tested (02/2024) with Nucleo-F446RE, HC_SR04 sensor, VS-Code under Win11, GCC 12.3 and MbedCE library