{"id":3858,"date":"2025-10-15T11:36:23","date_gmt":"2025-10-15T11:36:23","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3858"},"modified":"2025-10-15T11:36:25","modified_gmt":"2025-10-15T11:36:25","slug":"stm32-uart-part-8-1-1-wire-ds18b20-temperature-sensor-introduction-and-setup","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3858","title":{"rendered":"STM32 UART Part 8.1: 1- Wire DS18B20 Temperature Sensor &#8211; Introduction and setup"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-1024x683.png\" alt=\"\" class=\"wp-image-3859\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-1024x683.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-300x200.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-768x512.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-1150x767.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-750x500.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-400x267.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1-250x167.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/ChatGPT-Image-Oct-2-2025-at-05_27_55-PM-1.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this two-part guide, we explore how to interface the\u00a0<strong>DS18B20 temperature sensor<\/strong>\u00a0with an\u00a0<strong>STM32 microcontroller<\/strong>using the 1-Wire communication protocol. The first part introduces the fundamentals of the 1-Wire protocol, explains how the DS18B20 operates, and walks through the essential environment setup for STM32 development.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What is 1-wire protocol.<\/li>\n\n\n\n<li>DS18B20 temperature sensor.<\/li>\n\n\n\n<li>Hardware setup.<\/li>\n\n\n\n<li>STM32CubeIDE setup.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is 1-Wire Protocol:<\/h2>\n\n\n\n<p>The&nbsp;<strong>1-Wire protocol<\/strong>&nbsp;is a serial communication standard developed by&nbsp;<strong>Dallas Semiconductor (now Maxim Integrated)<\/strong>that enables data exchange between a&nbsp;<strong>single master device<\/strong>&nbsp;(such as an STM32 microcontroller) and one or more&nbsp;<strong>slave devices<\/strong>&nbsp;(such as sensors or memory ICs) over a&nbsp;<strong>single data line plus ground<\/strong>. Unlike protocols such as SPI or I\u00b2C that require multiple signal lines for data and clock, 1-Wire uses just one wire for&nbsp;<strong>both communication and, optionally, power delivery<\/strong>&nbsp;(in&nbsp;<em>parasitic power mode<\/em>), making it one of the simplest and most efficient communication systems for low-cost embedded designs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Core Concept<\/h3>\n\n\n\n<p>At its core, 1-Wire communication is&nbsp;<strong>asynchronous<\/strong>&nbsp;and&nbsp;<strong>half-duplex<\/strong>, meaning data can travel in both directions, but only one direction at a time. All devices share the same bus, and the master controls all communication timing. Each slave device has a&nbsp;<strong>unique, factory-programmed 64-bit address<\/strong>, which ensures global uniqueness and allows the master to identify and communicate with specific devices even when many are connected on the same line.<\/p>\n\n\n\n<p>The bus operates with&nbsp;<strong>open-drain signaling<\/strong>, where devices can only pull the line low \u2014 never drive it high. A&nbsp;<strong>pull-up resistor<\/strong>&nbsp;(typically 4.7 k\u03a9) keeps the line high when no device is pulling it low. This design allows multiple devices to coexist without bus contention and also enables parasitic powering.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Electrical Characteristics<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single data line:<\/strong>\u00a0Used for both data transmission and power (optional).<\/li>\n\n\n\n<li><strong>Pull-up resistor:<\/strong>\u00a0Keeps the line idle high; required for proper communication.<\/li>\n\n\n\n<li><strong>Logic levels:<\/strong>\u00a0Typically 0 V for logic \u20180\u2019 and 3.3 V\/5 V for logic \u20181\u2019.<\/li>\n\n\n\n<li><strong>Speed:<\/strong>\u00a0Standard mode at 16.3 kbps; Overdrive mode at 142 kbps.<\/li>\n\n\n\n<li><strong>Bus topology:<\/strong>\u00a0Supports linear, star, and mixed configurations up to several tens of meters (depending on capacitance and wiring quality).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Communication Sequence<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reset pulse (Initialization):<\/strong>\n<ul class=\"wp-block-list\">\n<li>The master pulls the line low for at least 480 \u00b5s to reset all slaves.<\/li>\n\n\n\n<li>Slaves respond with a\u00a0<strong>presence pulse<\/strong>\u00a0(holding the line low for 60\u2013240 \u00b5s) to signal they are active.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>ROM Commands:<\/strong><br>These commands are used to identify or address devices on the bus. Examples include:\n<ul class=\"wp-block-list\">\n<li><strong>Read ROM (0x33):<\/strong>\u00a0Read the 64-bit unique ID of a single connected device.<\/li>\n\n\n\n<li><strong>Match ROM (0x55):<\/strong>\u00a0Select a specific device using its unique address.<\/li>\n\n\n\n<li><strong>Search ROM (0xF0):<\/strong>\u00a0Discover all devices connected to the bus.<\/li>\n\n\n\n<li><strong>Skip ROM (0xCC):<\/strong>\u00a0Address all devices simultaneously (useful when only one device is connected).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function Commands:<\/strong><br>Once a specific device is addressed, function commands are used to perform device-specific actions such as reading sensor data, writing memory, or configuring parameters.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Timing and Bit Transmission<\/h3>\n\n\n\n<p>Each bit (logical \u20180\u2019 or \u20181\u2019) is represented by&nbsp;<strong>timing differences<\/strong>&nbsp;in how long the data line stays low or high:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Write \u20181\u2019:<\/strong>\u00a0Master pulls the line low for ~6 \u00b5s, then releases it to let it stay high for the rest of the 60 \u00b5s time slot.<\/li>\n\n\n\n<li><strong>Write \u20180\u2019:<\/strong>\u00a0Master pulls the line low for ~60 \u00b5s, then releases it briefly before the next bit.<\/li>\n\n\n\n<li><strong>Read Bit:<\/strong>\u00a0Master pulls the line low for ~6 \u00b5s, releases it, and reads the line state after ~15 \u00b5s to sample the bit value sent by the slave.<\/li>\n<\/ul>\n\n\n\n<p>Accurate timing is critical \u2014 even small deviations can corrupt communication. This is why precise delay functions or hardware peripherals (like UART in half-duplex mode or timers) are often used to emulate the 1-Wire protocol on STM32.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Parasitic Powering<\/h3>\n\n\n\n<p>One of the most distinctive features of 1-Wire devices is&nbsp;<strong>parasitic power mode<\/strong>. In this mode, the slave draws power directly from the data line when it is high, storing energy in an internal capacitor to operate when the line is low. This allows devices like the DS18B20 to run with&nbsp;<strong>only two connections \u2014 data and ground<\/strong>, eliminating the need for a separate power line.<\/p>\n\n\n\n<p>However, parasitic power limits the available current and may not be suitable for high-current operations or long cable runs. In such cases, a dedicated power pin (VDD) can be used.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Multi-Device Bus and Unique Identification<\/h3>\n\n\n\n<p>Each 1-Wire device includes a&nbsp;<strong>globally unique 64-bit ROM code<\/strong>&nbsp;structured as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>8-bit family code (identifies device type, e.g., temperature sensor, memory)<\/li>\n\n\n\n<li>48-bit serial number (unique identifier)<\/li>\n\n\n\n<li>8-bit CRC for error checking<\/li>\n<\/ul>\n\n\n\n<p>This makes it possible for a single master to enumerate and communicate with&nbsp;<strong>hundreds of devices<\/strong>&nbsp;on one shared line without conflicts. Devices can be dynamically added or removed without reconfiguration.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages of the 1-Wire Protocol<\/h3>\n\n\n\n<p><strong>Minimal wiring<\/strong>\u00a0\u2014 just one data line plus ground.<br><strong>Globally unique IDs<\/strong>\u00a0for every device.<br><strong>Low hardware cost<\/strong>\u00a0and easy scalability.<br><strong>Parasitic power option<\/strong>\u00a0for truly minimal connections.<br><strong>Multi-drop support<\/strong>\u00a0\u2014 multiple devices on one bus.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Limitations<\/h3>\n\n\n\n<p><strong>Low communication speed<\/strong>\u00a0compared to I\u00b2C or SPI.<br><strong>Strict timing requirements<\/strong>, often requiring precise microcontroller control or hardware timers.<br><strong>Limited cable length and capacitance tolerance<\/strong>, which may affect reliability in large networks.<br><strong>No hardware 1-Wire interface<\/strong>\u00a0on most MCUs (implemented in software).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Typical Applications<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Temperature sensing<\/strong>\u00a0(e.g., DS18B20, DS1822)<\/li>\n\n\n\n<li><strong>Identification and authentication<\/strong>\u00a0(e.g., iButton devices)<\/li>\n\n\n\n<li><strong>EEPROM memory storage<\/strong>\u00a0(e.g., DS2431)<\/li>\n\n\n\n<li><strong>Battery monitoring<\/strong><\/li>\n\n\n\n<li><strong>Industrial sensor networks<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. DS18B20 Temperature Sensor:<\/h2>\n\n\n\n<p>The&nbsp;<strong>DS18B20<\/strong>&nbsp;is a&nbsp;<strong>digital temperature sensor<\/strong>&nbsp;developed by&nbsp;<strong>Dallas Semiconductor (now Maxim Integrated)<\/strong>&nbsp;that measures temperature with&nbsp;<strong>high accuracy<\/strong>&nbsp;and communicates using the&nbsp;<strong>1-Wire protocol<\/strong>. It is one of the most popular and widely used temperature sensors in embedded systems because of its&nbsp;<strong>simplicity<\/strong>,&nbsp;<strong>accuracy<\/strong>, and&nbsp;<strong>versatility<\/strong>.<\/p>\n\n\n\n<p>Unlike analog temperature sensors (such as LM35 or TMP36), the DS18B20 provides a&nbsp;<strong>digital output<\/strong>&nbsp;\u2014 no need for analog-to-digital conversion. This makes it ideal for microcontrollers like STM32, which can directly read temperature data through a single GPIO pin using software or hardware-driven 1-Wire communication.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Key Features<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><strong>Interface<\/strong><\/td><td>1-Wire protocol (single data line for communication and optional power)<\/td><\/tr><tr><td><strong>Temperature range<\/strong><\/td><td>\u201355 \u00b0C to +125 \u00b0C<\/td><\/tr><tr><td><strong>Accuracy<\/strong><\/td><td>\u00b10.5 \u00b0C (\u201310 \u00b0C to +85 \u00b0C)<\/td><\/tr><tr><td><strong>Resolution<\/strong><\/td><td>Configurable: 9-bit to 12-bit<\/td><\/tr><tr><td><strong>Power supply<\/strong><\/td><td>3.0 V to 5.5 V<\/td><\/tr><tr><td><strong>Parasitic power<\/strong><\/td><td>Supported (can operate without dedicated VDD)<\/td><\/tr><tr><td><strong>Unique ID<\/strong><\/td><td>Each device has a factory-programmed 64-bit ROM code<\/td><\/tr><tr><td><strong>Communication speed<\/strong><\/td><td>Standard (16.3 kbps) and Overdrive (142 kbps) modes<\/td><\/tr><tr><td><strong>Package options<\/strong><\/td><td>TO-92, waterproof probe, SMD, or custom PCB modules<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Internal Structure and Operation<\/h2>\n\n\n\n<p>The DS18B20 consists of several internal blocks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Temperature Sensor and ADC<\/strong>\n<ul class=\"wp-block-list\">\n<li>The sensor measures temperature using an internal silicon bandgap sensor.<\/li>\n\n\n\n<li>The analog signal is converted to a\u00a0<strong>digital 16-bit value<\/strong>\u00a0using an internal ADC.<\/li>\n\n\n\n<li>Resolution is configurable between\u00a0<strong>9-bit (0.5 \u00b0C steps)<\/strong>\u00a0and\u00a0<strong>12-bit (0.0625 \u00b0C steps)<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Scratchpad Memory<\/strong>\n<ul class=\"wp-block-list\">\n<li>A temporary 9-byte memory area that stores temperature readings, user settings, and configuration data.<\/li>\n\n\n\n<li>It can be read or written by the master device.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>EEPROM<\/strong>\n<ul class=\"wp-block-list\">\n<li>Used to store the high\/low temperature thresholds and resolution configuration permanently.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Unique 64-bit ROM<\/strong>\n<ul class=\"wp-block-list\">\n<li>Each device has a globally unique ROM code, which consists of:\n<ul class=\"wp-block-list\">\n<li><strong>8-bit Family Code (0x28 for DS18B20)<\/strong><\/li>\n\n\n\n<li><strong>48-bit Serial Number<\/strong><\/li>\n\n\n\n<li><strong>8-bit CRC<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>This allows multiple DS18B20 sensors to share the same 1-Wire bus.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>1-Wire Interface Logic<\/strong>\n<ul class=\"wp-block-list\">\n<li>Handles all communication, including reset detection, bit timing, and CRC verification.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Powering Modes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.&nbsp;<strong>Normal Power Mode<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The sensor is powered through the\u00a0<strong>VDD pin (3.0 \u2013 5.5 V)<\/strong>.<\/li>\n\n\n\n<li>The data line is only used for communication.<\/li>\n\n\n\n<li>This mode is the most stable and is recommended when a dedicated power line is available.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.&nbsp;<strong>Parasitic Power Mode<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The\u00a0<strong>VDD pin is connected to ground<\/strong>, and the device draws power directly from the\u00a0<strong>data line<\/strong>\u00a0through an internal diode and capacitor.<\/li>\n\n\n\n<li>The capacitor charges when the line is high, and powers the device when the line is pulled low.<\/li>\n\n\n\n<li>This enables operation with\u00a0<strong>only two connections (DQ and GND)<\/strong>.<\/li>\n\n\n\n<li>However, it requires a\u00a0<strong>strong pull-up<\/strong>\u00a0on the data line during temperature conversion and EEPROM writes.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Communication Overview<\/h2>\n\n\n\n<p>The DS18B20 communicates through the&nbsp;<strong>1-Wire protocol<\/strong>, controlled entirely by the master (e.g., STM32). The master initiates all operations, and the sensor only responds when addressed.<\/p>\n\n\n\n<p>A typical communication cycle includes:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization (Reset and Presence Pulse)<\/strong>\n<ul class=\"wp-block-list\">\n<li>The master pulls the data line low for \u2265480 \u00b5s to reset all devices.<\/li>\n\n\n\n<li>After release, each connected device responds with a presence pulse (low for 60\u2013240 \u00b5s).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>ROM Commands<\/strong>\n<ul class=\"wp-block-list\">\n<li>Used to identify or address individual sensors.<\/li>\n\n\n\n<li>Key ROM commands:\n<ul class=\"wp-block-list\">\n<li><code>0x33<\/code>\u00a0\u2014\u00a0<em>Read ROM<\/em>\u00a0(read the device\u2019s 64-bit address)<\/li>\n\n\n\n<li><code>0x55<\/code>\u00a0\u2014\u00a0<em>Match ROM<\/em>\u00a0(address a specific device)<\/li>\n\n\n\n<li><code>0xCC<\/code>\u00a0\u2014\u00a0<em>Skip ROM<\/em>\u00a0(address all devices)<\/li>\n\n\n\n<li><code>0xF0<\/code>\u00a0\u2014\u00a0<em>Search ROM<\/em>\u00a0(enumerate devices on the bus)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function Commands<\/strong>\n<ul class=\"wp-block-list\">\n<li>After selecting a device, function commands control operations like temperature measurement and data reading.<\/li>\n\n\n\n<li>Common commands:\n<ul class=\"wp-block-list\">\n<li><code>0x44<\/code>\u00a0\u2014\u00a0<em>Convert T<\/em>\u00a0(start temperature conversion)<\/li>\n\n\n\n<li><code>0xBE<\/code>\u00a0\u2014\u00a0<em>Read Scratchpad<\/em>\u00a0(read temperature and settings)<\/li>\n\n\n\n<li><code>0x4E<\/code>\u00a0\u2014\u00a0<em>Write Scratchpad<\/em>\u00a0(set thresholds and resolution)<\/li>\n\n\n\n<li><code>0x48<\/code>\u00a0\u2014\u00a0<em>Copy Scratchpad<\/em>\u00a0(store thresholds\/resolution to EEPROM)<\/li>\n\n\n\n<li><code>0xB8<\/code>\u00a0\u2014\u00a0<em>Recall EEPROM<\/em>\u00a0(restore saved settings)<\/li>\n\n\n\n<li><code>0xB4<\/code>\u00a0\u2014\u00a0<em>Read Power Supply<\/em>\u00a0(detect if powered parasitically)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Temperature Conversion and Data Format<\/h2>\n\n\n\n<p>When the master issues the&nbsp;<strong>Convert T (0x44)<\/strong>&nbsp;command, the DS18B20 measures the current temperature and stores the result in the&nbsp;<strong>scratchpad<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conversion Time<\/h3>\n\n\n\n<p>The conversion time depends on resolution:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Resolution<\/th><th>Bits<\/th><th>Time (Typical)<\/th><th>Step Size<\/th><\/tr><\/thead><tbody><tr><td>9-bit<\/td><td>93.75 ms<\/td><td>0.5 \u00b0C<\/td><td><\/td><\/tr><tr><td>10-bit<\/td><td>187.5 ms<\/td><td>0.25 \u00b0C<\/td><td><\/td><\/tr><tr><td>11-bit<\/td><td>375 ms<\/td><td>0.125 \u00b0C<\/td><td><\/td><\/tr><tr><td>12-bit<\/td><td>750 ms<\/td><td>0.0625 \u00b0C<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Scratchpad Memory Layout (9 Bytes)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Byte<\/th><th>Purpose<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>Temperature LSB<\/td><td>0x50<\/td><\/tr><tr><td>1<\/td><td>Temperature MSB<\/td><td>0x05<\/td><\/tr><tr><td>2<\/td><td>TH (High Alarm Trigger)<\/td><td>User-defined<\/td><\/tr><tr><td>3<\/td><td>TL (Low Alarm Trigger)<\/td><td>User-defined<\/td><\/tr><tr><td>4<\/td><td>Configuration Register<\/td><td>Resolution bits (R1, R0)<\/td><\/tr><tr><td>5\u20137<\/td><td>Reserved<\/td><td>Internal use<\/td><\/tr><tr><td>8<\/td><td>CRC<\/td><td>For data integrity check<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Temperature Data Format<\/h3>\n\n\n\n<p>Temperature is stored as a\u00a0<strong>signed 16-bit two\u2019s complement value<\/strong>:<br>T = Raw Data* 0.0625\u00b0C<br><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Device Addressing and Multi-Sensor Systems<\/h2>\n\n\n\n<p>Since every DS18B20 has a&nbsp;<strong>unique 64-bit ROM<\/strong>, multiple sensors can share the same data line. The master can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use\u00a0<strong>Search ROM (0xF0)<\/strong>\u00a0to enumerate all sensors.<\/li>\n\n\n\n<li>Use\u00a0<strong>Match ROM (0x55)<\/strong>\u00a0to communicate with a specific sensor.<\/li>\n\n\n\n<li>Use\u00a0<strong>Skip ROM (0xCC)<\/strong>\u00a0if only one device is connected.<\/li>\n<\/ul>\n\n\n\n<p>This makes the 1-Wire bus incredibly scalable \u2014 dozens of sensors can measure temperature at different points using a single microcontroller pin.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Applications<\/h2>\n\n\n\n<p>The DS18B20 is used in a wide range of environments due to its&nbsp;<strong>robustness, accuracy, and digital simplicity<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>HVAC systems<\/li>\n\n\n\n<li>Industrial process monitoring<\/li>\n\n\n\n<li>Weather stations<\/li>\n\n\n\n<li>Smart thermostats and home automation<\/li>\n\n\n\n<li>Medical temperature probes<\/li>\n\n\n\n<li>Battery management systems<\/li>\n\n\n\n<li>Embedded system diagnostics<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages and Limitations<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Advantages<\/th><th>Limitations<\/th><\/tr><\/thead><tbody><tr><td>Requires only one data line<\/td><td>Communication speed is slow<\/td><\/tr><tr><td>Each sensor has a unique ID<\/td><td>Timing-critical protocol<\/td><\/tr><tr><td>No ADC required (digital output)<\/td><td>Needs precise pull-up resistor and timing control<\/td><\/tr><tr><td>Operates in wide voltage range<\/td><td>Limited cable length (capacitance-sensitive)<\/td><\/tr><tr><td>Supports multiple sensors on one bus<\/td><td>Parasitic power limits current and cable distance<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Connection:<\/h2>\n\n\n\n<p>The connection as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"655\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-1024x655.jpg\" alt=\"\" class=\"wp-image-3863\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-1024x655.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-300x192.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-768x491.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-1536x982.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-2048x1310.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-1150x736.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-750x480.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-400x256.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-23-37-250x160.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>SD18B20<\/td><td>STM32F411RE Nucleo-64<\/td><\/tr><tr><td>Vcc<\/td><td>5V<\/td><\/tr><tr><td>DIO<\/td><td>PA9 (D8 of Arduino pin)<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Also, connect a pull up resistor between DIO and 3V3.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. STM32CubeIDE Setup:<\/h2>\n\n\n\n<p>Open STM32CubeIDE after selecting the workspace and create new project as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"355\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1024x355.jpg\" alt=\"\" class=\"wp-image-2940\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1024x355.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-300x104.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-768x266.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1536x532.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-2048x709.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1150x398.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-750x260.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-400x139.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-250x87.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Select the MCU:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"832\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1024x832.jpg\" alt=\"\" class=\"wp-image-2941\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1024x832.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-300x244.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-768x624.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1536x1248.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-2048x1664.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1150x934.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-750x609.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-400x325.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-250x203.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Give the project a name and click on finish:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"874\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-874x1024.jpg\" alt=\"\" class=\"wp-image-3864\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-874x1024.jpg 874w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-256x300.jpg 256w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-768x900.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-750x879.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-400x469.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15-250x293.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-30-15.jpg 976w\" sizes=\"(max-width: 874px) 100vw, 874px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Once the project has been created, STM32CubeMX shall appear.<\/p>\n\n\n\n<p>From connectivity, select USART1 (Or any other serial you want) and set the mode as Single-Wire (Half-duplex) as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"637\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-1024x637.jpg\" alt=\"\" class=\"wp-image-3825\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-1024x637.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-300x186.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-768x477.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-1536x955.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-1150x715.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-750x466.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-400x249.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28-250x155.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-33-28.jpg 1792w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This wil set PA9 as UART1_TX pin as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"930\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-1024x930.jpg\" alt=\"\" class=\"wp-image-3828\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-1024x930.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-300x272.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-768x697.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-1536x1395.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-1150x1044.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-750x681.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-400x363.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16-250x227.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-55-16.jpg 1764w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the configuration, leave everything as is since we shall change the UART configuration according to our needs.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1011\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-1024x1011.jpg\" alt=\"\" class=\"wp-image-3826\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-1024x1011.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-300x296.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-768x758.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-750x740.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-400x395.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06-250x247.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-02_17-36-06.jpg 1088w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Next, from DMA settings, add DMA for TX and RX as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"744\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-1024x744.jpg\" alt=\"\" class=\"wp-image-3865\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-1024x744.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-300x218.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-768x558.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-1150x836.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-750x545.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-400x291.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40-250x182.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-15_14-33-40.jpg 1524w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the configuration.<br>Save the project and this will generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In part 2, we shall dig deeper into DS18B20 communication.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Stay tuned.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this two-part guide, we explore how to interface the\u00a0DS18B20 temperature sensor\u00a0with an\u00a0STM32 microcontrollerusing the 1-Wire communication protocol. The first part introduces the fundamentals of the 1-Wire protocol, explains how the DS18B20 operates, and walks through the essential environment setup for STM32 development. In this guide, we shall cover the following: 1. What is 1-Wire [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-3858","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3858"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3858"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3858\/revisions"}],"predecessor-version":[{"id":3866,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3858\/revisions\/3866"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}