A lightweight library for interfacing SRF04 and SRF05 ultrasonic distance sensors with STM32 microcontrollers using HAL drivers. Supports blocking measurements in microseconds and centimeters.
- SRF04 & SRF05 Support: Works with both popular ultrasonic modules.
- Microsecond Resolution: Uses a 1 MHz timer for precise pulse-width measurement.
- Simple API: Easy-to-use functions to trigger, read raw pulse width, and get distance in cm.
- Modular Handle: Encapsulate per-sensor configuration in an
SRF0X_HandleTypeDef
. - HAL-Based: Leverages STM32Cube HAL; minimal external dependencies.
-
Hardware:
-
STM32 microcontroller with Cortex-M core
-
SRF04 or SRF05 ultrasonic sensor
-
Connections:
- Trig pin → any GPIO output
- Echo pin → any GPIO input
- Shared ground and VCC (5 V or 3.3 V as your sensor supports)
-
-
Software:
- STM32CubeIDE (or any toolchain with HAL libraries)
- C compiler supporting STM32 HAL
-
Clone or download this repository:
git clone https://github.com/alixahedi/SRF0X-STM32.git
-
Copy source files into your project:
srf0x.h
→ your project'sInc/
foldersrf0x.c
→ your project'sSrc/
folder
-
Include header where needed:
#include "srf0x.h"
-
Configure peripherals as described below.
-
Configure GPIOs in CubeMX (or code):
- TrigPin: Output push-pull
- EchoPin: Input (no pull or pull-down as needed)
-
Configure a timer (e.g.,
TIM2
) for 1 MHz counter:- Prescaler =
(HAL_RCC_GetPCLK1Freq()/1000000 - 1)
- Counter mode = up
- Prescaler =
-
Enable DWT cycle counter (optional, for 10 µs trigger delay):
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
-
Fill the handle and call
srf0x_init()
:SRF0X_HandleTypeDef hsrfx; hsrfx.TrigPort = GPIOA; hsrfx.TrigPin = GPIO_PIN_9; hsrfx.EchoPort = GPIOA; hsrfx.EchoPin = GPIO_PIN_10; hsrfx.htim = &htim2; // must be 1 MHz timer srf0x_init(&hsrfx);
-
Get raw pulse width (µs):
uint32_t pulse_us = srf0x_read_us(&hsrfx);
-
Get distance (cm):
float dist_cm = srf0x_read_cm(&hsrfx);
#include "main.h"
#include "srf0x.h"
extern TIM_HandleTypeDef htim2;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
// Enable DWT for microsecond delay
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
// Sensor handle
SRF0X_HandleTypeDef sensor = {
.TrigPort = GPIOA,
.TrigPin = GPIO_PIN_9,
.EchoPort = GPIOA,
.EchoPin = GPIO_PIN_10,
.htim = &htim2
};
srf0x_init(&sensor);
while (1)
{
float distance = srf0x_read_cm(&sensor);
// Use distance (e.g., print via UART or display)
HAL_Delay(100);
}
}
Member | Type | Description |
---|---|---|
TrigPort | GPIO_TypeDef * |
GPIO port for trigger |
TrigPin | uint16_t |
GPIO pin number for trigger |
EchoPort | GPIO_TypeDef * |
GPIO port for echo |
EchoPin | uint16_t |
GPIO pin number for echo |
htim | TIM_HandleTypeDef * |
Pointer to 1 MHz timer handle |
void srf0x_init(SRF0X_HandleTypeDef *hsrf);
- Initialize sensor handle (peripherals must be ready).
void srf0x_trigger(SRF0X_HandleTypeDef *hsrf);
- Send a 10 µs pulse to trigger the sensor.
uint32_t srf0x_read_us(SRF0X_HandleTypeDef *hsrf);
- Measure echo pulse width, returns microseconds.
float srf0x_read_cm(SRF0X_HandleTypeDef *hsrf);
- Measure distance, returns centimeters (
pulse_us / 58.0f
).
- Fork the repository
- Create a feature branch (
git checkout -b feature/NewFeature
) - Commit your changes (
git commit -m "Add NewFeature"
) - Push to branch (
git push origin feature/NewFeature
) - Open a pull request
Please ensure code is formatted and documented in the same style.
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or suggestions, feel free to reach out via email: 📧 Alixahedi@gmail.com



