Introduction and Initialization Configuration of DS18B20 for STM32
DS18B20 Overview
The DS18B20 is a digital temperature sensor that supports 9-bit to 12-bit Celsius temperature measurement, with non-volatile, user-programmable upper and lower alarm trigger thresholds. It communicates over the 1-Wire bus, which requires only one data line to exchange data with a host microcontroller. Its operating temperature range is -55°C to +125°C, with an accuracy of ±0.5°C in the range of -10°C to +85°C. Additionally, the sensor can draw power directly from the data line, eliminating the need for an external power supply.
Every DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20 sensors to share the same 1-Wire bus. This makes it simple for a single microcontroller to control many sensors distributed over a large area. Common applications that benefit from this feature include HVAC environmental control, indoor building temperature monitoring, temperature sensing for industrial equipment and machinery, and process monitoring and control systems.
Pin Assignment
| 8-pin SOIC Pin | TO-92 Package Pin | Symbol | Description |
|---|---|---|---|
| 5 | 1 | GND | Ground connection |
| 4 | 2 | DQ | Digital input/output, open-drain for 1-Wire operation. Also supplies power to the sensor in parasite power mode |
| 3 | 3 | VDD | Optional external power pin. Must be tied to ground when operating in parasite power mode |
Any unlisted pins do not need to be connected.
Parasite Power Mode: The DS18B20 can operate without an external power supply. When the bus is held high, the sensor charges an internal capacitor (Cpp) from the DQ line through the 1-Wire pull-up resistor. This capacitor powers the device when the bus is pulled low, and this power supply method is called parasite power. As an alternative, the DS18B20 can also be powered by an external supply connected to the VDD pin.
Configuration Register
| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
|---|---|---|---|---|---|---|---|
| 0 | R1 | R0 | 1 | 1 | 1 | 1 | 1 |
The measurement resolution of DS18B20 is configured via the R1 and R0 bits. The default setting after power-up is R0=1, R1=1, which corresponds to 12-bit resolution.
Higher resolution generally requires longer conversion time.
Resolution Configuration Table
| R1 | R0 | Resolution | Maximum Conversion Time |
|---|---|---|---|
| 0 | 0 | 9-bit | 93.75ms (Tconv/8) |
| 0 | 1 | 10-bit | 187.5ms (Tconv/4) |
| 1 | 0 | 11-bit | 375ms (Tconv/2) |
| 1 | 1 | 12-bit | 750ms (Tconv) |
Tconv = full 12-bit temperature conversion time
Communication Sequence
The protocol for accessing DS18B20 over the 1-Wire bus follows three mandatory steps:
- Initialization
- ROM operation command
- DS18B20 function command
All access must follow this sequence. If steps are missing or out of order, the DS18B20 will not return any data to the host.
Initialization
All 1-Wire operations start with an initialization sequence. The sequence consists of a reset pulse sent by the host controller, followed by a presence pulse from the DS18B20. The presence pulse notifies the host that the DS18B20 is online and ready for communication.
Initialization Timing
To start the sequence, the host pulls the 1-Wire bus low and holds it for 480us to send a reset pulse, then releases the bus and switches to receive mode. A 5kΩ pull-up resistor pulls the bus back to high level. After the DS18B20 detects the rising edge on the DQ pin, it waits 1560us, then sends a presence pulse by pulling the bus low for 60240us.
In practical implementation: the host first configures DQ as output, drives low for 480us, then drives high, waits 15~60us, switches DQ to pull-up input, and detects the low presence pulse sent by the sensor.
ROM Operation Commands
After the host detects the presence pulse, it sends a ROM command. When multiple DS18B20 are connected to the same bus, these commands allow the host to select a specific target sensor via its unique 64-bit ROM serial code.
Commonly used ROM commands:
0x33 (Read ROM Command)
This command can only be used when there is exactly one DS18B20 on the bus. It allows the host to read the 64-bit serial code directly without using the search ROM command.
0xCC (Skip ROM Command)
This command allows the host to send function commands without first transmitting the 64-bit ROM code, suitable for single-sensor bus setups.
Other ROM commands are documented in the official DS18B20 datasheet.
DS18B20 Function CommandsnAfter sending a valid ROM command to the target sensor, the host sends a DS18B20 function command to read/write the scratchpad or trigger operations.
Commonly used function commands:
0x44 (Convert T Command)
This command triggers a new temperature conversion. After conversion completes, the 16-bit result is stored in the sensor's scratchpad, and the sensor enters idle mode. If the sensor is in parasite power mode, the host must enable a strong pull-up on the 1-Wire bus within 10us after sending this command, and keep it pulled high for the entire conversion period.
0xB4 (Read Power Mode Command)
After sending this command, the DS18B20 will pull the bus low if it operates in parasite power mode, and leave the bus high if powered by an external supply.
Power Supply Modes
The DS18B20 can be powered by an external supply connected to VDD, or operate in parasite power mode that draws power from the 1-Wire data line. Parasite power eliminates the need for an external power supply, which is especially useful for long-distance temperature measurement.
Note: VDD must be tied to ground when operating in parasite power mode.
If the host does not know whether the connecetd DS18B20 uses parasite or external power, the sensor can signal its power mode. The host sends a Skip ROM command (0xCC), followed by the Read Power command (0xB4), then reads the bus state. Parasite-powered sensors will pull the bus low, while externally-powered sensors leave the bus high. If the bus is pulled low, the host knows it needs to provide a strong pull-up during temperature conversion.
Temperature Measurement Process
The DS18B20 stays in low-power idle mode until triggered by the host. To start a measurement, the host sends the 0x44 Converrt T command. After conversion, the 2-byte temperature result is stored in the scratchpad temperature register, and the sensor returns to idle.
If the sensor is powered by an external supply, the host can start a read sequence immediately after the conversion command to get the result.
Read Timing Requirements
A read sequence is required whenever the DS18B20 needs to transmit data to the host. After the host sends a Read Scratchpad command (0xBE), Read Power command (0xB4), Convert T command (0x44) or Recall EEPROM command (0xB8), it must immediately start a read sequence to get the requested data from the DS18B20.
All read sequences must be at least 60us long, with a minimum 1us recovery period between consecutive reads. A read sequence starts when the host pulls the data line low from high, holds the low for at least 1us, then releases the bus. After the read sequence starts, the DS18B20 drives the bus high for a logic 1, or low for a logic 0. After transmitting a logic 0, the bus is released and pulled back to high by the pull-up resistor. The output data from the DS18B20 is valid within 15us after the falling edge of the host's read start pulse. Therefore, the host must sample the I/O pin state within 15us after releasing the bus at the start of the read sequence.
Example STM32F1xx Implementation Code
ds18b20_bsp.c
#include "ds18b20_bsp.h"
// Configure DQ pin as push-pull output
static void DS18B20_SetOutputMode(void)
{
GPIO_InitTypeDef gpio_cfg;
gpio_cfg.GPIO_Pin = DS18B20_DQ_PIN;
gpio_cfg.GPIO_Speed = GPIO_Speed_50MHz;
gpio_cfg.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(DS18B20_DQ_PORT, &gpio_cfg);
}
// Configure DQ pin as pull-up input
static void DS18B20_SetInputMode(void)
{
GPIO_InitTypeDef gpio_cfg;
gpio_cfg.GPIO_Pin = DS18B20_DQ_PIN;
gpio_cfg.GPIO_Speed = GPIO_Speed_50MHz;
gpio_cfg.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(DS18B20_DQ_PORT, &gpio_cfg);
}
// Initialize GPIO clock and base settings
void DS18B20_GPIO_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
DS18B20_SetOutputMode();
}
// Run DS18B20 initialization sequence, return 1 if sensor detected
uint8_t DS18B20_Init(void)
{
uint8_t presence = 0;
DS18B20_SetOutputMode();
DS18B20_WriteDQ(0);
Delay_us(750);
DS18B20_WriteDQ(1);
Delay_us(20);
DS18B20_SetInputMode();
// Wait for sensor to pull bus low (presence pulse)
while(GPIO_ReadInputDataBit(DS18B20_DQ_PORT, DS18B20_DQ_PIN) != 0);
presence = 1;
// Wait for sensor to release bus
while(GPIO_ReadInputDataBit(DS18B20_DQ_PORT, DS18B20_DQ_PIN) == 0);
return presence;
}
ds18b20_bsp.h
#ifndef __DS18B20_BSP_H
#define __DS18B20_BSP_H
#include "stm32f10x.h"
#include "systick_bsp.h"
#define DS18B20_DQ_PORT GPIOB
#define DS18B20_DQ_PIN GPIO_Pin_6
#define DS18B20_WriteDQ(level) GPIO_WriteBit(DS18B20_DQ_PORT, DS18B20_DQ_PIN, (BitAction)(level))
void DS18B20_GPIO_Init(void);
uint8_t DS18B20_Init(void);
#endif
systick_bsp.c
#include "systick_bsp.h"
static volatile uint32_t tickDelay;
void SysTick_Init(void)
{
// 72MHz system clock, 1 tick = 1 microsecond
while(SysTick_Config(72));
// Disable timer after initialization, enable before use
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}
void Delay_us(uint32_t us)
{
tickDelay = us;
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
while(tickDelay != 0);
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}
// SysTick interrupt handler
void SysTick_Handler(void)
{
if(tickDelay > 0)
tickDelay--;
}
systick_bsp.h
#ifndef __SYSTICK_BSP_H
#define __SYSTICK_BSP_H
#include "stm32f10x.h"
void SysTick_Init(void);
void Delay_us(uint32_t n);
#endif