A lightweight and non-blocking DS18B20 driver written in C for STM32 (HAL-based).
Built on top of the non-blocking 1-Wire library, it allows reading temperature from one or multiple DS18B20 devices without blocking the CPU.
It supports:
- π‘οΈ DS18B20 β 9β12 bit temperature sensor (Celsius & Fahrenheit)
The library is designed for:
- Applications requiring non-blocking temperature acquisition
- Multi-device support on a single 1-Wire bus
- STM32 projects across F0/F1/F3/F4/F7/G0/G4/H7 families
- πΉ Non-blocking operation using timer callbacks
- πΉ Supports multiple DS18B20 devices (
OW_MAX_DEVICE
) - πΉ Configurable resolution (9β12 bit)
- πΉ Temperature read in Celsius and Fahrenheit
- πΉ Alarm threshold configuration
- πΉ Works with any GPIO pins
- πΉ Built on top of HAL-compatible 1-Wire library
Add these files to your STM32 project:
ds18b20.h
ds18b20.c
- Ensure One-Wire Library added, https://github.com/nimaltd/ow
Not yet available; include files manually for now.
Set conversion resolution and alarm thresholds (Not yet available):
ds18b20_config_t ds18_conf = {
.alarm_high = 50, // High temperature alarm
.alarm_low = -50, // Low temperature alarm
.cnv_bit = DS18B20_CNV_BIT_12 // Resolution (9β12 bit)
};
Read in One-Wire Reopository
#include "ds18b20.h"
ds18b20_handle_t ds18;
void ds18_tim_cb(TIM_HandleTypeDef *htim)
{
ow_callback(&ds18.ow);
}
void ds18_done_cb(ow_err_t error)
{
}
ow_init_t ow_init_struct;
ow_init_struct.tim_handle = &htim1;
ow_init_struct.gpio = GPIOC;
ow_init_struct.pin = GPIO_PIN_8;
ow_init_struct.tim_cb = ds18_tim_cb;
ow_init_struct.done_cb = NULL; // Optional
ow_init_struct.rom_id_filter = DS18B20_ID;
ds18b20_init(&ds18, &ow_init_struct);
// Update ROM IDs for all devices
ds18b20_update_rom_id(&ds18);
while(ds18b20_is_busy(&ds18));
// Configure alarm thresholds and resolution
ds18b20_config_t ds18_conf = {
.alarm_high = 50,
.alarm_low = -50,
.cnv_bit = DS18B20_CNV_BIT_12
};
ds18b20_conf(&ds18, &ds18_conf);
while(ds18b20_is_busy(&ds18));
int16_t temp_c[2];
while(1) {
ds18b20_cnv(&ds18);
while(ds18b20_is_busy(&ds18));
while(!ds18b20_is_cnv_done(&ds18));
ds18b20_req_read(&ds18, 0);
while(ds18b20_is_busy(&ds18));
temp_c[0] = ds18b20_read_c(&ds18);
ds18b20_req_read(&ds18, 1);
while(ds18b20_is_busy(&ds18));
temp_c[1] = ds18b20_read_c(&ds18);
}
Function | Description |
---|---|
ds18b20_init() |
Initialize DS18B20 driver handle |
ds18b20_is_busy() |
Check if bus is busy |
ds18b20_last_error() |
Get last error |
ds18b20_update_rom_id() |
Update connected ROM IDs |
ds18b20_cnv() |
Start temperature conversion |
ds18b20_conf() |
Set configuration (alarm/resolution) |
ds18b20_is_cnv_done() |
Check if conversion is done |
ds18b20_req_read() |
Request temperature read |
ds18b20_read_c() |
Read temperature in Celsius |
ds18b20_read_f() |
Convert Celsius to Fahrenheit |
If you find this project useful, please β star the repo and support!
Licensed under the terms in the LICENSE.