Skip to content

Contributed by iiv, curated by Ai-Thinker

Preface: The DS18B20 is a digital temperature sensor that communicates with the host MCU via the 1-Wire bus protocol. The DS18B20 features high accuracy, providing accurate ambient temperature readings with a measurement accuracy of 0.5°C. It uses digital output and can be adjusted within 12-bit resolution as needed. It has various functions and application modes, including temperature measurement, temperature alarms and thermal management. By configuring the registers, users can flexibly choose the working mode suitable for their application. This sensor has a long lifespan and low power consumption, making it very suitable for battery-powered applications that require long-term operation and strict power budgets. The DS18B20 is commonly used in industrial automation, environmental monitoring, meteorological instruments, medical equipment and other fields. Its high accuracy, digital output and versatility make the DS18B20 a widely used temperature measurement solution. Whether in industrial environments requiring high-precision temperature measurement or other practical applications needing accurate temperature monitoring, the DS18B20 is a reliable choice. Chinese datasheet: click me click me (I recommend a good place to look up datasheets: LCSC marketplace)

Hello everyone, yes, I'm here again. This time I'll share how to use the Xiao An Pai to read the temperature from a DS18B20 temperature sensor. I'm sure many of you use this sensor often, and so do I!

Physical demo:

  • DS18B20 temperature sensor module
  • Current temperature output on the serial port:

After some research, I finally managed to read the temperature from the sensor with the Xiao An Pai (I borrowed an existing driver from the internet and adjusted it; it can read the temperature successfully).

I won't describe the wiring in detail — it's simple, and I'm sure everyone knows it (GND to GND, VCC to VCC, DQ to an IO pin of the Xiao An Pai).

Function introduction:

The function calls are very simple: initialize first, then read the temperature.

1: ds18b20_init(pin number here)

2: ds18b20_read() returns the temperature information

Code:

The ds18b20.h file: just create a new ds18b20.h header file, copy the content into it, and include it in main.c.

c
/*ds18b20 temperature sensor driver*/
#ifndef _ds18b20_H_
#define _ds18b20_H_
#include "bflb_mtimer.h"
#include "bflb_gpio.h"
struct bflb_device_s *gpio;
uint8_t _pin;
// initialize IO
void ds18b20_init(uint8_t pin)
{
    gpio = bflb_device_get_by_name("gpio");
    bflb_gpio_init(gpio, pin, GPIO_INPUT | GPIO_PULLUP);
    _pin = pin;
}

// bus reset.
uint8_t ds18b20_reset(void)
{
    uint8_t r;
    uint8_t retries = 125;
    bflb_gpio_init(gpio, _pin, GPIO_OUTPUT);
    bflb_gpio_set(gpio, _pin);
    bflb_mtimer_delay_us(8);
    bflb_gpio_init(gpio, _pin, GPIO_INPUT);
    do {
        if (retries-- == 0) {
            return 0;
        }
        bflb_mtimer_delay_us(2);
    } while (!bflb_gpio_read(gpio, _pin));
    bflb_gpio_init(gpio, _pin, GPIO_OUTPUT);
    bflb_gpio_reset(gpio, _pin);   // pull low for 500uS
    bflb_mtimer_delay_us(500);
    bflb_gpio_init(gpio, _pin, GPIO_INPUT);
    bflb_mtimer_delay_us(65);
    r = !bflb_gpio_read(gpio, _pin);
    bflb_mtimer_delay_us(500);
    return r;
}

// read one byte.
uint8_t ds18b20_read_one_char(void)
{
    uint8_t i = 0;
    uint8_t dat = 0;
    for (i = 8; i > 0; i--) {
        bflb_gpio_init(gpio, _pin, GPIO_OUTPUT);
        bflb_mtimer_delay_us(1);       // ensure more than 1us between two read operations
        bflb_gpio_reset(gpio, _pin);   // pulse signal
        bflb_mtimer_delay_us(2);       // ensure the host pulls the bus low for more than 1us
        dat >>= 1;
        bflb_gpio_set(gpio, _pin);     // pulse signal
        bflb_mtimer_delay_us(5);
        bflb_gpio_init(gpio, _pin, GPIO_INPUT);
        if (bflb_gpio_read(gpio, _pin))     // host samples the bus to determine the value
            dat |= 0x80;
        bflb_mtimer_delay_us(50);      // wait for the read operation to finish
    }
    return (dat);
}
// write one byte.
void ds18b20_write_one_char(unsigned char dat)
{
    unsigned char i = 0;
    bflb_gpio_init(gpio, _pin, GPIO_OUTPUT);
    for (i = 8; i > 0; i--) {
        bflb_mtimer_delay_us(1);
        bflb_gpio_reset(gpio, _pin);
        bflb_mtimer_delay_us(2);
        (dat & 0x01) ? bflb_gpio_set(gpio, _pin) : bflb_gpio_reset(gpio, _pin);
        bflb_mtimer_delay_us(70);
        bflb_gpio_set(gpio, _pin);
        dat >>= 1;
    }
}

// description: read temperature
float ds18b20_read()
{
    uint8_t low_byte, high_byte;
    int16_t TReading, SignBit, Tc_100;
    ds18b20_reset();
    ds18b20_write_one_char(0xCC); // skip the read-ROM operation
    ds18b20_write_one_char(0x44); // start temperature conversion
    bflb_mtimer_delay_us(600);
    ds18b20_reset();
    ds18b20_write_one_char(0xCC); // skip the read-ROM operation
    ds18b20_write_one_char(0xBE); // read temperature registers, etc. (9 registers total); the first two are the temperature
    bflb_mtimer_delay_us(600);
    low_byte = ds18b20_read_one_char();  // read low byte of temperature
    high_byte = ds18b20_read_one_char(); // read high byte of temperature
    TReading = (high_byte << 8) + low_byte;
    SignBit = TReading & 0x8000;
    if (SignBit) {
        TReading = (TReading ^ 0xffff) + 1;
        TReading = 0 - TReading;
    }
    Tc_100 = (6 * TReading) + TReading / 4;
    return (float)Tc_100 / 100.0;
}
#endif

Calling it in main.c:

c
#include "board.h"
#include "ds18b20.h"
float data;
int main(void)
{
    board_init();
    ds18b20_init(24); // initialize; fill in whichever IO the sensor is connected to.
    printf("hello world\r\n");
    while (1) {
        data = ds18b20_read(); // read temperature in a loop
        printf("temperature:%.2f °C\r\n", data);
        bflb_mtimer_delay_ms(1000);
    }
}

OK, that's my sharing about the DS18B20 temperature sensor this time. I added some comments in the code, and some comments were already there. I'm not very experienced, so if anything is wrong, feel free to modify it yourself (this driver was ported from the internet; it's not my original work).

Have questions?

For other questions, please visit the unified discussion area: Ai-Thinker Discussions

Released under the MIT License. Build Time 2026-09-11 14:52:23