{"id":4351,"date":"2018-10-17T20:00:52","date_gmt":"2018-10-17T18:00:52","guid":{"rendered":"https:\/\/msalamon.pl\/?p=4351"},"modified":"2025-12-27T20:33:33","modified_gmt":"2025-12-27T19:33:33","slug":"the-immortal-ds18b20","status":"publish","type":"post","link":"https:\/\/msalamon.pl\/en\/the-immortal-ds18b20\/","title":{"rendered":"The Immortal DS18B20"},"content":{"rendered":"\n<p>Ask&nbsp;anywhere&nbsp;which&nbsp;temperature sensor to use and the answer you\u2019ll most often get is DS18B20. Dallas\u2019s chip is extremely popular. You can also buy a waterproof version in stores, which further strengthens its popularity. Obviously, I couldn\u2019t miss this sensor.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h1 class=\"wp-block-heading\">Communication with DS18B20<\/h1>\n\n\n\n<p>You can talk to the DS18B20 only via the 1-Wire interface. What is that?! It\u2019s an invention by Dallas Semiconductor, which later merged with Maxim.&nbsp; This interface, which is also a protocol, uses only a single line for bidirectional communication, hence its name.<\/p>\n\n\n\n<p>I don\u2019t want to go into the details of this interface and how it works. In short, the most important information is:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\n<ol class=\"wp-block-list\">\n<li>All devices are connected to a single bus.<\/li>\n\n\n\n<li>Ones and zeros are defined as pulses with time constants defined by the interface. Because of this, the communication speed is essentially fixed.<\/li>\n\n\n\n<li>Transmission is serial and follows the\u00a0<em>master-slave<\/em>\u00a0rules.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>The protocol includes addressing, thanks to which we can activate a selected sensor, similar to I\u00b2C.<\/li>\n<\/ol>\n\n\n\n<p>I recommend the official Maxim documentation, which explains the details much better than I would. On the company website there are, for example, some helpful articles:&nbsp;<a href=\"https:\/\/www.maximintegrated.com\/en\/app-notes\/index.mvp\/id\/1796\" target=\"_blank\" rel=\"noopener\">Link1<\/a> <a href=\"https:\/\/www.maximintegrated.com\/en\/app-notes\/index.mvp\/id\/126\" target=\"_blank\" rel=\"noopener\">Link2<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connection<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/sklep.msalamon.pl\/?s=ds18b20&amp;post_type=product&amp;?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=ds18b20&amp;utm_content=Text\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"400\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner.jpg\" alt=\"DS18B20 sensors at sklep.msalamon.pl\" class=\"wp-image-1410\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner.jpg 1200w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner-300x100.jpg 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner-1024x341.jpg 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner-768x256.jpg 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner-24x8.jpg 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner-36x12.jpg 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/02\/DS18B20-baner-160x53.jpg 160w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As I mentioned earlier, devices on the 1-Wire bus are connected with only one wire. Adding power, we typically have 3 wires. The data line must be pulled up to the supply line through a resistor. It is usually 4k7 ohm.<\/p>\n\n\n\n<p>Hmm, I said 3 wires by default. And non\u2011standard? Two! Namely, the 1\u2011Wire interface allows communication in the so\u2011called <em>parasitic mode<\/em>, where the positive supply line is not provided to the sensor. The VDD pin on the <em>Slave<\/em> device is then shorted to ground. So where does the sensor get its power from? As the name of the mode implies \u2013 it parasitizes on the data line. In 1\u2011Wire <em>s<\/em><em>lave<\/em> devices there is a capacitor that, when a high state appears on the data line, charges and maintains the supply during the low state.<\/p>\n\n\n\n<p>For the purposes of this post I used the regular mode. I connected 4 DS18B20 sensors to pin PA1 on the <a href=\"https:\/\/sklep.msalamon.pl\/produkt\/nucleo-f401re\/?utm_source=blog&amp;utm_medium=article&amp;utm_campaign=ds18b20&amp;utm_content=Text\">Nucleo F401RE<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"551\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-1024x551.png\" alt=\"\" class=\"wp-image-386\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-1024x551.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-300x161.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-768x413.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-24x13.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-36x19.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic-149x80.png 149w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/sklep.msalamon.pl\/kategoria-produktu\/dev-boardy\/stm32-nucleo\/?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=ds18b20&amp;utm_content=nucleo\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"400\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner.jpg\" alt=\"\" class=\"wp-image-1593\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner.jpg 1200w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner-300x100.jpg 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner-1024x341.jpg 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner-768x256.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_schematic.png\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programming<\/h2>\n\n\n\n<p>On the Internet you can find tons of 1\u2011Wire communication examples for probably all microcontrollers in the world. I won\u2019t pretend my library is unique or written from scratch. When writing it I relied on various open examples that are publicly available. I made a library that suits me in use. I\u2019m not claiming it\u2019s the best \u2013 it\u2019s good \ud83d\ude42<\/p>\n\n\n\n<p>For the library to work you need a <em>t<\/em><em>imer<\/em> with a 1&nbsp;\u00b5s tick. It\u2019s easy to obtain by using an integer number of megahertz to clock the MCU. Just set the <em>prescaler<\/em> to <em>(MCU clock) &#8211; 1<\/em>. Remember to enter a large value \u2013 e.g., 65000 \u2013 into the&nbsp;<em>Counter Period<\/em> of the <em>timer<\/em> configuration in CubeMX.&nbsp;<\/p>\n\n\n\n<p>The library consists of two parts. The first is the 1\u2011Wire interface handling. It is hands\u2011off and does not require any configuration in headers. The second part is, of course, DS18B20 handling. Configuration in the header file includes:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The maximum number of DS18B20 devices that can be connected to the bus.<\/li>\n\n\n\n<li>The GPIO pin and port used for 1\u2011Wire.<\/li>\n\n\n\n<li>The name of the handler for the timer with a 1\u00a0\u00b5s tick.<\/li>\n\n\n\n<li>A define flag that decides whether to use CRC. You can save a few bytes of time when reading at the possible cost of data correctness.<\/li>\n<\/ol>\n\n\n\n<p>Configuration in CubeMX is trivially simple. Just define the GPIO bus pin as a regular <em>GPIO Output<\/em>&nbsp;and configure the aforementioned&nbsp;<em>timer<\/em>. Additionally, I threw in the on\u2011board Nucleo LED for blinking, a UART for printing temperatures to the PC, and a test output for viewing on a logic analyzer.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"722\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-1024x722.png\" alt=\"\" class=\"wp-image-387\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-1024x722.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-300x212.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-768x542.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-24x17.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-36x25.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX-113x80.png 113w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/ds18b20_CubeMX.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>How does my code work? Information about each connected sensor is stored in an array of <em>Ds18b20Sensor_t<\/em> structures with the number of elements given in the header as the maximum number of sensors<em>.&nbsp;<\/em>It contains information about the full address, the last correctly read temperature, and the success of the last read. Additionally, I created a variable that says how many DS18B20 sensors were detected. There may be fewer than the MAX defined in the header. I wondered about dynamically allocating memory for the number of connected sensors, but would that make sense? In general their number doesn\u2019t change during the device\u2019s lifetime, so the number of elements in the array can be set fixed.<\/p>\n\n\n\n<p>Access to data such as address or temperature is possible only through dedicated functions. There is no manual poking around in the structure. The most important functions:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void DS18B20_Init(DS18B20_Resolution_t resolution);\n<\/pre>\n\n\n\n<p>This is, of course, initialization. Inside the initialization, the 1\u2011Wire bus is started and DS18B20 sensors that are connected are searched for and counted. You must pass the required resolution for the devices as an argument. Remember that resolution has a huge impact on the time it takes to convert the temperature to digital form. Available options:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DS18B20_Resolution_9bits\nDS18B20_Resolution_10bits\nDS18B20_Resolution_11bits\nDS18B20_Resolution_12bits<\/pre>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void DS18B20_StartAll(void);\n<\/pre>\n\n\n\n<p>This sends the start temperature conversion command to all connected sensors. Simple.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void DS18B20_ReadAll(void);\n<\/pre>\n\n\n\n<p>Function to read the converted temperature into the appropriate elements in the sensor array. Every sensor detected in the initialization function is read.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">uint8_t DS18B20_Quantity(void);\n<\/pre>\n\n\n\n<p>Returns the number of sensors detected during initialization.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">uint8_t DS18B20_GetTemperature(uint8_t number, float* destination);\n<\/pre>\n\n\n\n<p>Writes the temperature value from the i\u2011th sensor to the memory location pointed to by&nbsp;<em>destination<\/em>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void DS18B20_GetROM(uint8_t number, uint8_t* ROM);\nvoid DS18B20_WriteROM(uint8_t number, uint8_t* ROM);\n<\/pre>\n\n\n\n<p>Operations on sensor addresses. With them, you can, for example, change the order of sensors in the array.<\/p>\n\n\n\n<p>Putting it all together I got a sample code that prints all sensors to the terminal.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* USER CODE BEGIN 2 *\/\n\tDS18B20_Init(DS18B20_Resolution_12bits);\n\/* USER CODE END 2 *\/\n\n\/* Infinite loop *\/\n\/* USER CODE BEGIN WHILE *\/\nwhile (1)\n{\n\t\/* USER CODE END WHILE *\/\n\n\t\/* USER CODE BEGIN 3 *\/\n\tDS18B20_ReadAll();\n\tDS18B20_StartAll();\n\tuint8_t ROM_tmp[8];\n\tuint8_t i;\n\tfor(i = 0; i &amp;lt; DS18B20_Quantity(); i++)\n\t{\n\t\tif(DS18B20_GetTemperature(i, &amp;amp;temperature))\n\t\t{\n\t\t\tDS18B20_GetROM(i, ROM_tmp);\n\t\t\tmemset(message, 0, sizeof(message));\n\t\t\tsprintf(message, \"%d. ROM: %X%X%X%X%X%X%X%X Temp: %f\\n\\r\",i, ROM_tmp[0], ROM_tmp[1], ROM_tmp[2], ROM_tmp[3], ROM_tmp[4], ROM_tmp[5], ROM_tmp[6], ROM_tmp[7], temperature);\n\t\t\tHAL_UART_Transmit(&amp;amp;huart2, (uint8_t*)message, sizeof(message), 100);\n\t\t}\n\t}\n\tHAL_UART_Transmit(&amp;amp;huart2, (uint8_t*)\"\\n\\r\", sizeof(\"\\n\\r\"), 100);\n\tHAL_Delay(1000);\n\tHAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);\n}\n\/* USER CODE END 3 *\/<\/pre>\n\n\n\n<p><strong>Warning, danger!!!<\/strong> The STM32 compiler has the quirk that after generating code in CubeMX you don\u2019t immediately have the ability to print variables of type&nbsp;<em>float<\/em>! You\u2019ll get only zeros or nothing at all \ud83d\ude41 To make this work you need to add the flag&nbsp;<em>-u _printf_float<\/em> to the linker.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float.png\"><img loading=\"lazy\" decoding=\"async\" width=\"821\" height=\"855\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float.png\" alt=\"\" class=\"wp-image-370\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float.png 821w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float-288x300.png 288w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float-768x800.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float-24x24.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float-36x36.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/print_float-77x80.png 77w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now that everything is clear, see the result of this little piece of code:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results.png\"><img loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"414\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results.png\" alt=\"\" class=\"wp-image-371\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results.png 661w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results-300x188.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results-24x15.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results-36x23.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/putty_results-128x80.png 128w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>It\u2019s warm at my desk, isn\u2019t it? \ud83d\ude42<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sooo, how long will this take?<\/h2>\n\n\n\n<p>Of course I had to check how long it takes to transfer the necessary data over 1\u2011Wire!<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"285\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-1024x285.png\" alt=\"\" class=\"wp-image-379\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-1024x285.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-300x84.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-768x214.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read-160x45.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"285\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-1024x285.png\" alt=\"\" class=\"wp-image-381\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-1024x285.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-300x84.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-768x214.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start-160x45.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>The communication to read the temperature itself takes about 14 ms.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"285\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-1024x285.png\" alt=\"\" class=\"wp-image-380\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-1024x285.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-300x84.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-768x214.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start-160x45.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>The start conversion command for all sensors is about 2.3 ms.<br>\n<\/p>\n\n\n\n<p>So the whole thing for one sensor is about 16.4 ms. Knowing that the start command is sent only once for all sensors, the formula for read and start conversion time for n sensors is 14 * n + 2.3 [ms]. Let\u2019s check.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"286\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-1024x286.png\" alt=\"\" class=\"wp-image-377\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-1024x286.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-300x84.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-768x215.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start-160x45.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_two_read_and_start.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"287\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-1024x287.png\" alt=\"\" class=\"wp-image-378\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-1024x287.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-300x84.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-768x215.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start-160x45.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Two sensors. 14 * 2 + 2.3 = 30.3. Measurement is 30.43.<\/p>\n\n\n\n<p>Four sensors. 14 * 4 + 2.3 = 58.3. Measurement 58.45.<\/p>\n\n\n\n<p>So everything adds up. Can we shave off a few milliseconds? By disabling CRC.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"284\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-1024x284.png\" alt=\"\" class=\"wp-image-385\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-1024x284.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-300x83.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-768x213.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc-160x44.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_nocrc.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"284\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-1024x284.png\" alt=\"\" class=\"wp-image-382\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-1024x284.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-300x83.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-768x213.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc-160x44.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_start_nocrc.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>11.75 ms for the readout of a single sensor.<\/p>\n\n\n\n<p>The start conversion command is of course unchanged. There is nothing related to CRC here. Without CRC, the formula for handling n sensors is 11.75 * n + 2.3 [ms].<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"285\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-1024x285.png\" alt=\"\" class=\"wp-image-384\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-1024x285.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-300x84.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-768x214.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc-160x45.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_one_read_and_start_nocrc.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Confirmation on four sensors.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"284\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-1024x284.png\" alt=\"\" class=\"wp-image-383\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-1024x284.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-300x83.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-768x213.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-24x7.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-36x10.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc-160x44.png 160w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/timing_four_read_and_start_nocrc.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>11.75 * 4 + 2.3 = 49.3. The measurement comes out 49.05. Checks out \ud83d\ude42<\/p>\n\n\n\n<p>However, be careful when giving up CRC! It may happen that you read garbage, and now you have no way to verify it.<\/p>\n\n\n\n<p>Can we shave off more? We can, but not with the DS18B20 \ud83d\ude41 1\u2011Wire offers something called the <em>Override<\/em> mode, which speeds up transmission about 10\u2011fold. Our temperature sensor does not support this mode, so unfortunately I have nothing to test. In general, few 1\u2011Wire devices support accelerated communication.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>DS18B20 temperature sensors are very pleasant. Handling them is not overly complicated, and there are thousands of examples on the Internet showing how to deal with them. I\u2019m adding one of my own, hoping it\u2019s written sensibly. An unquestionable advantage is the ability to handle many devices via only one data line. This works great for multi\u2011point measurements. Additionally, waterproof versions are perfect for measuring water temperature, e.g., in an aquarium \ud83d\ude42<\/p>\n\n\n\n<p>Thank you for reading this post. If you like this topic, let me know in a comment. I\u2019ll also be grateful for topic suggestions you\u2019d like me to cover.<\/p>\n\n\n\n<p>The code is, as usual, available on my GitHub: <a href=\"https:\/\/github.com\/lamik\/DS18B20_STM32_HAL\" target=\"_blank\" rel=\"noopener\">link<\/a><\/p>\n\n\n\n<p><span>If you noticed any error, disagree with something, would like to add something important, or simply feel like discussing this topic, write a comment. Remember that the discussion should be polite and in accordance with the rules of the Polish language.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Contest results<\/h3>\n\n\n\n<p>In the previous post I announced a mini contest. Time to pick the winners. From all comments that did not repeat by name, IP and email and met all requirements, I chose 10, in my opinion, most interesting uses of the display.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1120\" height=\"223\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD.png\" alt=\"\" class=\"wp-image-396\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD.png 1120w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD-300x60.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD-1024x204.png 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD-768x153.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD-24x5.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD-36x7.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2018\/10\/Wybrane_komentarz_konkurs_LCD-160x32.png 160w\" sizes=\"auto, (max-width: 1120px) 100vw, 1120px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>From among them I drew two winners according to the rules given in the contest. These are comments number 4 and 10 from the screenshot above. The video of the drawing is available at&nbsp;<a href=\"https:\/\/youtu.be\/vqYASbZ2Xr0\" target=\"_blank\" rel=\"noopener\">https:\/\/youtu.be\/vqYASbZ2Xr0<\/a><\/p>\n\n\n\n<p>Congratulations to the winners! I\u2019ve already emailed you to finalize the contest. There will probably be more contests on the blog. I have a bit too much stuff in my closet \ud83d\ude09<\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;4351&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;0&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;The Immortal DS18B20&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ask&nbsp;anywhere&nbsp;which&nbsp;temperature sensor to use and the answer you\u2019ll most often get is DS18B20. Dallas\u2019s chip is extremely popular. You can also buy a waterproof version in stores, which further strengthens its popularity. Obviously, I couldn\u2019t miss this sensor.<\/p>\n","protected":false},"author":1,"featured_media":3017,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[160],"tags":[175,176,174,177],"class_list":["post-4351","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stm32","tag-electronics","tag-programming","tag-stm32","tag-stm32cubemx"],"_links":{"self":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/comments?post=4351"}],"version-history":[{"count":3,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4351\/revisions"}],"predecessor-version":[{"id":4467,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4351\/revisions\/4467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/media\/3017"}],"wp:attachment":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/media?parent=4351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/categories?post=4351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/tags?post=4351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}