The HC-SR04 ultrasonic range finder

it is any way how to use HC-SR04 for measuring distance, I want use power module for 5V source.

Not sure if that answers your question, but there is a 3V version that can be directly connected to the core module:

1 Like

HI, I know, but 5V version is not problem for, because I want use it with power module witch contains 5V, I need some support for SW side, like patch for generic firmware, for measure one sensor in a specific time period.

Hi, we have sucesfully used 5 V version with Core Module. Here is example code that sends measured value over serial debug port.

Hi, thanks! it is working. But only when I modify old “bcf” project, when I try add this code to new “twr-skeleton” I am not able to build a project.

If you send us compiler error output, we can take a look at it.

Executing task: C:\Users\budul.platformio\penv\Scripts\platformio.exe run --environment release <

Processing release (platform: hardwario-tower; board: core_module; framework: stm32cube)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via -v, --verbose option
PLATFORM: HARDWARIO TOWER - Industrial IoT Kit (1.1.0) > Core Module
HARDWARE: STM32L083CZ 32MHz, 20KB RAM, 192KB Flash
DEBUG: Current (jlink) External (jlink, stlink)

  • framework-stm32cubel0 1.11.3
  • tool-ldscripts-ststm32 0.1.0
  • toolchain-gccarmnoneeabi 1.90301.200702 (9.3.1)
    LDF: Library Dependency Finder ->
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 18 compatible libraries
    Scanning dependencies…
    Dependency Graph
    |-- 1.0.0
    Building in release mode
    Compiling .pio\build\release\src\application.o
    src\application.c: In function ‘hc_sr04_event_handler’:
    src\application.c:61:44: warning: passing argument 1 of ‘twr_hc_sr04_get_distance_millimeter’ from incompatible pointer type [-Wincompatible-pointer-types]
    61 | if (bc_hc_sr04_get_distance_millimeter(&value))
    | ^~~~~~
    | |
    | float *
    In file included from .pio\libdeps\release\twr-sdk\twr\inc/twr.h:35,
    from include/application.h:8,
    from src\application.c:5:
    .pio\libdeps\release\twr-sdk\twr\inc/twr_hc_sr04.h:90:57: note: expected ‘twr_hc_sr04_t *’ {aka ‘struct twr_hc_sr04_t *’} but argument is of type ‘float *’
    90 | bool twr_hc_sr04_get_distance_millimeter(twr_hc_sr04_t *self, float *millimeter);
    | ~^~
    In file included from include/application.h:9,
    from src\application.c:5:
    .pio\libdeps\release\twr-sdk\bcl\inc/bcl.h:806:44: error: too few arguments to function ‘twr_hc_sr04_get_distance_millimeter’
    806 | #define bc_hc_sr04_get_distance_millimeter twr_hc_sr04_get_distance_millimeter
    | ^
    .pio\libdeps\release\twr-sdk\bcl\inc/bcl.h:806:44: note: in definition of macro ‘bc_hc_sr04_get_distance_millimeter’
    806 | #define bc_hc_sr04_get_distance_millimeter twr_hc_sr04_get_distance_millimeter
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from .pio\libdeps\release\twr-sdk\twr\inc/twr.h:35,
    from include/application.h:8,
    from src\application.c:5:
    .pio\libdeps\release\twr-sdk\twr\inc/twr_hc_sr04.h:90:6: note: declared here
    90 | bool twr_hc_sr04_get_distance_millimeter(twr_hc_sr04_t *self, float *millimeter);
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src\application.c: In function ‘application_task’:
    src\application.c:97:16: warning: unused variable ‘counter’ [-Wunused-variable]
    97 | static int counter = 0;
    | ^~~~~~~
    At top level:
    src\application.c:97:16: warning: ‘counter’ defined but not used [-Wunused-variable]
    *** [.pio\build\release\src\application.o] Error 1
    ======================================================================================================== [FAILED] Took 4.61 seconds ========================================================================================================
    Environment Status Duration

release FAILED 00:00:04.611
================================================================================================== 1 failed, 0 succeeded in 00:00:04.611 ==================================================================================================
The terminal process “C:\Users\budul.platformio\penv\Scripts\platformio.exe ‘run’, ‘–environment’, ‘release’” terminated with exit code: 1.

So this error is because in newer SDK we’ve improved ultrasound library so you can now choose any GPIO pin to trigger and sense echo. With that you can also have multiple sensors connected to the Core Module and not just one on fixed pins.

In SDK we have example code with correct parameters.

Basically you have to create instance of the sensor

// HC-SR04 instance
twr_hc_sr04_t hc_sr04;

and then this instance has to be passed as a pointer with & to every ultrasound function as the first parameter &hc_sr04

With this flexibility we we’re able to simplify the project so customers could connect ultrasound sensors to the Sensor Module. However in your case it is not possible because you need 5V.

    // Sensor module:
    // Echo - channel B
    // Trig - channel A