Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

nimaltd/ds18b20

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

23 Commits

Repository files navigation

🌑️ DS18B20 Temperature Sensor Library for STM32

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

✨ Features

  • πŸ”Ή 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

βš™οΈ Installation

1. Copy files directly

Add these files to your STM32 project:

2. STM32Cube Pack Installer (Recommended)

Not yet available; include files manually for now.


πŸ”§ Configuration (ds18b20_config_t)

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)
};

πŸ›  CubeMX Setup

Read in One-Wire Reopository

πŸš€ Quick Start

Include header

#include "ds18b20.h"

Define a handle

ds18b20_t ds18;

Timer callback

void ds18_tim_cb(TIM_HandleTypeDef *htim)
{
 ow_callback(&ds18.ow);
}

Optional done callback

void ds18_done_cb(ow_err_t error)
{
}

Initialize DS18B20 in main.c

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));

Read temperatures

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);
}

🧰 API Overview

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

πŸ’– Support

If you find this project useful, please ⭐ star the repo and support!


πŸ“œ License

Licensed under the terms in the LICENSE.


AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /