{"id":3896,"date":"2025-10-29T08:40:25","date_gmt":"2025-10-29T08:40:25","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3896"},"modified":"2025-10-29T08:40:28","modified_gmt":"2025-10-29T08:40:28","slug":"stm32-uart-part-8-2-1-wire-ds18b20-temperature-sensor-read-temperature","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3896","title":{"rendered":"STM32 UART Part 8.2: 1- Wire DS18B20 Temperature Sensor: Read Temperature"},"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\/image-3-1024x683.png\" alt=\"\" class=\"wp-image-3897\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-1024x683.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-300x200.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-768x512.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-1150x767.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-750x500.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-400x267.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3-250x167.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/image-3.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this part of the guide, we will bring together all the concepts learned so far to measure temperature from the\u00a0<strong>DS18B20<\/strong>sensor using the\u00a0<strong>1-Wire protocol<\/strong>. You will see how the STM32 communicates with the sensor, initiates a temperature conversion, reads the raw data, and then calculates the precise temperature value in Celsius.<\/p>\n\n\n\n<p><\/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>How to reset, send 0 and 1.<\/li>\n\n\n\n<li>Firmware development.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. How to Reset, Send 0 and 1:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>To send the reset condition, the master needs to pull the line low for a minimum of 480us and then release it. When the DS18B20 detects this rising edge, it waits 15\u03bcs to 60\u03bcs and then transmits a presence pulse by pulling the line low for 60\u03bcs to 240\u03bcs.<\/p>\n\n\n\n<p>With the baud rate of 115200, the total time taken by 9 bits (Start + 8Data bits) is less than 80us. So we can not possibly pull the line Low continuously for at least 480us. This is why we will use the baud rate of 9600 to send the reset condition.<\/p>\n\n\n\n<p>If the master send the&nbsp;<strong>byte 0xF0&nbsp;<\/strong>with the baud rate of 9600, there will be 5 Low bits (Start + 4 Data bits). The total time for which the line will remain low will be around 520us.<\/p>\n\n\n\n<p>Once the sensor detect the reset condition, it will pull the line low for 60 to 240uS, this will inform the master, there is a device. The figure below shows the timing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"423\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-1024x423.webp\" alt=\"\" class=\"wp-image-3898\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-1024x423.webp 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-300x124.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-768x317.webp 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-1150x475.webp 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-750x310.webp 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-400x165.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6-250x103.webp 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_6.webp 1284w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>By setting the UART baudrate to 9600 and sending 0xF0 via UART, the timing diagram as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"630\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19.png\" alt=\"\" class=\"wp-image-3899\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19-768x473.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19-750x461.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19-400x246.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS19-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As per the information given in the DS18B20 datasheet, for the sensor to recognize a signal as the bit 0, the data line should transit from HIGH to LOW and then it should be LOW for around 60-120us.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"778\" height=\"445\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4.webp\" alt=\"\" class=\"wp-image-3900\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4.webp 778w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4-300x172.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4-768x439.webp 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4-750x429.webp 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4-400x229.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_4-250x143.webp 250w\" sizes=\"(max-width: 778px) 100vw, 778px\" \/><\/figure>\n\n\n\n<p>Now if we use the UART at the baud rate of 115200, each bit would take around 8.68us. We are using 8 Data bit, so along with the Start bit we have a total of 9 bits in a single byte. These 9 bits would take around 78us in total.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"616\" height=\"453\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_5.webp\" alt=\"\" class=\"wp-image-3901\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_5.webp 616w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_5-300x221.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_5-400x294.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/uart7_5-250x184.webp 250w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/figure>\n\n\n\n<p>As per the information given in the DS18B20 datasheet, for the sensor to recognize a signal as the bit 1, the data line should transit from HIGH to LOW and then it should be LOW for around 1us. After that the line should be released and the pull up resistor will pull the line High for the rest of the time.<\/p>\n\n\n\n<p>Since each bit at the baud rate of 115200 takes around 8.68us, it is not possible for the MCU to pull the line low for just 1us, it will at least be low for 8.68us.<\/p>\n\n\n\n<p>One interesting point to note is that the DS18B20 sensor does not sample the line immediately after 1us. Instead it will only sample 15us after the line was first pulled low. We will send the&nbsp;<strong>data 0xFF<\/strong>, such that only the START bit is low and the rest of the bits are high. This will keep the line low for only around 9-10us and then the line will be pulled High with the help of the pull up resistor.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In short, we need to set the baudrate 10 115200, send 0x00 and 0xFF to represent 0 and 1 respectively.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Firmware Development:<\/h2>\n\n\n\n<p>First, we need to declare the following variable:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int presence = 0, isRxed = 0;<\/pre><\/div>\n\n\n\n<p>presence is used during reset (start) condition to detect the presence of the sensor.<\/p>\n\n\n\n<p>isRxed is used when all 8 bytes represent the 8-bit data is received.<\/p>\n\n\n\n<p>Next declare the following variables:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t RxData[8], Temp_LSB = 0, Temp_MSB = 0;<\/pre><\/div>\n\n\n\n<p>RxData is used to hold the received data when the sensor sends the temperature data. The data shall be read using DMA.<\/p>\n\n\n\n<p>Next,<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int16_t Temp;<\/pre><\/div>\n\n\n\n<p>This will hold the 16-bit temperature data.<\/p>\n\n\n\n<p>Finally,<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">float Temperature;<\/pre><\/div>\n\n\n\n<p>This will provide actual temperature.<\/p>\n\n\n\n<p>Next, declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void uart_Init (uint32_t baud)\n{\n\t  huart1.Instance = USART1;\n\t  huart1.Init.BaudRate = baud;\n\t  huart1.Init.WordLength = UART_WORDLENGTH_8B;\n\t  huart1.Init.StopBits = UART_STOPBITS_1;\n\t  huart1.Init.Parity = UART_PARITY_NONE;\n\t  huart1.Init.Mode = UART_MODE_TX_RX;\n\t  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;\n\t  huart1.Init.OverSampling = UART_OVERSAMPLING_16;\n\t  if (HAL_HalfDuplex_Init(&amp;huart1) != HAL_OK)\n\t  {\n\t    Error_Handler();\n\t  }\n}<\/pre><\/div>\n\n\n\n<p>This function will allow us to change the baudrate during the execution of the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int DS18B20_Start (void)<\/pre><\/div>\n\n\n\n<p>This function will allow us to send start command as seen in the previous section.<\/p>\n\n\n\n<p>With the function, declare the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t data = 0xF0;<\/pre><\/div>\n\n\n\n<p>This will hold the data to be transmitted to UART, as we saw, we need to send 0xF0 to get the required delay.<\/p>\n\n\n\n<p>Next, initialize the UART with baudrate of 9600 as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uart_Init(9600);<\/pre><\/div>\n\n\n\n<p>Next, transmit the data as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_UART_Transmit(&amp;huart1, &amp;data, 1, 100);<\/pre><\/div>\n\n\n\n<p>Capture the next presence data as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if (HAL_UART_Receive(&amp;huart1, &amp;data, 1, 1000) != HAL_OK) return -1;  \/\/ failed.. check connection<\/pre><\/div>\n\n\n\n<p>If the reception failed after 1 second, it means there is no sensor connected, you need to check your connection or the sensor is faulty.<\/p>\n\n\n\n<p>Reinitialize the UART back to 115200 as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uart_Init(115200);<\/pre><\/div>\n\n\n\n<p>Next, if data ==-2, then there is no device detected:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if (data == 0xf0) return -2;  \/\/ error no device detected<\/pre><\/div>\n\n\n\n<p>If it is successful, we shall return 1 as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">return 1;<\/pre><\/div>\n\n\n\n<p>Hence, the function as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int DS18B20_Start (void)\n{\n\tuint8_t data = 0xF0;\n\tuart_Init(9600);\n\tHAL_UART_Transmit(&amp;huart1, &amp;data, 1, 100);\n\tif (HAL_UART_Receive(&amp;huart1, &amp;data, 1, 1000) != HAL_OK) return -1;  \/\/ failed.. check connection\n\tuart_Init(115200);\n\tif (data == 0xf0) return -2;  \/\/ error no device detected\n\treturn 1;\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, the write function:<\/p>\n\n\n\n<p>Declare the function as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void DS18B20_Write (uint8_t data)<\/pre><\/div>\n\n\n\n<p>Within the function, declare an array to hold the 8 bytes as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t buffer[8];<\/pre><\/div>\n\n\n\n<p>Next, we shall construct the array to hold 0 and 1 according to the previous section as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">for (int i=0; i&lt;8; i++)\n\t{\n\t\tif ((data &amp; (1&lt;&lt;i)) != 0)  \/\/ if the bit is HIGH\n\t\t{\n\t\t\tbuffer[i] = 0xFF;  \/\/ send a 1\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbuffer[i] = 0x00;  \/\/ send a 0\n\t\t}\n\t}<\/pre><\/div>\n\n\n\n<p>Finally, transmit the data as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_UART_Transmit(&amp;huart1, buffer, 8, 1000);<\/pre><\/div>\n\n\n\n<p>Hence, the function as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void DS18B20_Write (uint8_t data)\n{\n\tuint8_t buffer[8];\n\tfor (int i=0; i&lt;8; i++)\n\t{\n\t\tif ((data &amp; (1&lt;&lt;i)) != 0)  \/\/ if the bit is HIGH\n\t\t{\n\t\t\tbuffer[i] = 0xFF;  \/\/ send a 1\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbuffer[i] = 0x00;  \/\/ send a 0\n\t\t}\n\t}\n\n\tHAL_UART_Transmit(&amp;huart1, buffer, 8, 1000);\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, the read function.<\/p>\n\n\n\n<p>Declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t DS18B20_Read (void)<\/pre><\/div>\n\n\n\n<p>Within the function, we shall first declare a buffer as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t buffer[8];<\/pre><\/div>\n\n\n\n<p>Declare a variable to hold the read data as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t value = 0;<\/pre><\/div>\n\n\n\n<p>Next, fill the buffer with 0xFF to be send to the sensor as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">for (int i=0; i&lt;8; i++)\n{\n  buffer[i] = 0xFF;\n}<\/pre><\/div>\n\n\n\n<p>Send the buffer using DMA as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_UART_Transmit_DMA(&amp;huart1, buffer, 8);<\/pre><\/div>\n\n\n\n<p>Receive the data using DMA as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_UART_Receive_DMA(&amp;huart1, RxData, 8);<\/pre><\/div>\n\n\n\n<p>Wait for isRxed to set as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while (isRxed == 0);<\/pre><\/div>\n\n\n\n<p>Next convert the 8 byte to single byte that represents the original value as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tfor (int i=0; i&lt;8; i++)\n\t{\n\t\tif (RxData[i] == 0xFF)  \/\/ bit is 1\n\t\t{\n\t\t\tvalue |= 1&lt;&lt;i;  \/\/ write a 1 to that position\n\t\t}\n\t}<\/pre><\/div>\n\n\n\n<p>Next, reset the isRxed as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">isRxed = 0;<\/pre><\/div>\n\n\n\n<p>Return the value as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">return value;<\/pre><\/div>\n\n\n\n<p>Hence, the entire code as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t DS18B20_Read (void)\n{\n\tuint8_t buffer[8];\n\tuint8_t value = 0;\n\tfor (int i=0; i&lt;8; i++)\n\t{\n\t\tbuffer[i] = 0xFF;\n\t}\n\n\tHAL_UART_Transmit_DMA(&amp;huart1, buffer, 8);\n\tHAL_UART_Receive_DMA(&amp;huart1, RxData, 8);\n\n\twhile (isRxed == 0);\n\n\tfor (int i=0; i&lt;8; i++)\n\t{\n\t\tif (RxData[i] == 0xFF)  \/\/ bit is 1\n\t\t{\n\t\t\tvalue |= 1&lt;&lt;i;  \/\/ write a 1 to that position\n\t\t}\n\t}\n\n\tisRxed = 0;\n\treturn value;\n}<\/pre><\/div>\n\n\n\n<p>Finally, the DMA reception completed callback function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)\n{\n\tisRxed = 1;\n}<\/pre><\/div>\n\n\n\n<p>In the while 1 loop.<\/p>\n\n\n\n<p>Start with present detection by sending the start as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">presence = DS18B20_Start();<\/pre><\/div>\n\n\n\n<p>Send skip ROM as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DS18B20_Write(0xCC);  \/\/ skip ROM<\/pre><\/div>\n\n\n\n<p>Next, start the temperature conversion as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DS18B20_Write(0x44);  \/\/ convert t<\/pre><\/div>\n\n\n\n<p>Send start again:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">presence = DS18B20_Start();<\/pre><\/div>\n\n\n\n<p>Skip ROM again:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DS18B20_Write(0xCC);  \/\/ skip ROM<\/pre><\/div>\n\n\n\n<p>Send read scratchpad command<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DS18B20_Write(0xBE);  \/\/ Read Scratchpad<\/pre><\/div>\n\n\n\n<p>Next, read LSB and MSB of the temperature and construct the 16-bit as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">Temp_LSB = DS18B20_Read();\nTemp_MSB = DS18B20_Read();\nTemp = (Temp_MSB&lt;&lt;8) | Temp_LSB;<\/pre><\/div>\n\n\n\n<p>Convert the temperature from Temp variable:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">Temperature = (float)Temp\/16.0; \/\/ reslution is 0.0625<\/pre><\/div>\n\n\n\n<p>Delay by 100ms as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_Delay(100);<\/pre><\/div>\n\n\n\n<p>Thats all for the project.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In a debugging session, add Temperature variable to Live Expression<\/p>\n\n\n\n<p>Room temperature:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"436\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-1024x436.jpg\" alt=\"\" class=\"wp-image-3902\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-1024x436.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-300x128.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-768x327.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-1150x490.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-750x319.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-400x170.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39-250x106.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-06-39.jpg 1184w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Holding the sensor in my hand:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"333\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-1024x333.jpg\" alt=\"\" class=\"wp-image-3903\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-1024x333.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-300x97.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-768x250.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-1150x374.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-750x244.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-400x130.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47-250x81.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/2025-10-29_10-07-47.jpg 1188w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Full communication:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"630\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20.png\" alt=\"\" class=\"wp-image-3904\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20-768x473.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20-750x461.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20-400x246.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/RigolDS20-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-69975f81-9abf-45c7-ba84-6543821e895b\" href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/DS18B20.zip\">DS18B20<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/10\/DS18B20.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-69975f81-9abf-45c7-ba84-6543821e895b\">Download<\/a><\/div>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this part of the guide, we will bring together all the concepts learned so far to measure temperature from the\u00a0DS18B20sensor using the\u00a01-Wire protocol. You will see how the STM32 communicates with the sensor, initiates a temperature conversion, reads the raw data, and then calculates the precise temperature value in Celsius. In this guide, we [&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-3896","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\/3896"}],"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=3896"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3896\/revisions"}],"predecessor-version":[{"id":3906,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3896\/revisions\/3906"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}