Multiple DS18B20 sensors using UART

I have covered different one wire sensors in the past, including the DS18B20. If you recall, we used the method where we check if the pin is high for the given period of time, to identify if the bit is a ‘1’ or a ‘0’.
There seems to be another approach towards these one wire sensors, and that is using the UART.

This tutorial will cover how to interface DS18B20 sensor with STM32, but using the UART. Also we will see the working of the multiple DS18B20 sensors.

Setting up

The CubeMX setup is as shown below

  • As shown above, we need to use the Half Duplex Mode.
  • Keep every parameter to default state.
  • In the NVIC, enable the global interrupt.

NOTE:- It is recommended to use the USART instead of UART. I tried testing it with UART but it doesn’t seems to work.

This is all the setup needed in CubeMX.

After the code is generated, include the library files onewire.c and onewire.h

First of all we need to define the UART instance we are using

This is defined in the onewire.c file (line 23 onwards). Since I am using USART 1, I have defined it.

Next we will define the number of sensors we have connected to the single pin.

The number of devices needed to be defined in 2 different places. In the onewire.c file (line 26) and in onewire.h file (line 26).

I have connected 2 DS18B20 sensors, so I have defined 2.

This completes the setup process, now we will take a look at the code.



extern float Temp[MAXDEVICES_ON_THE_BUS];


int main()



  /* USER CODE END 2 */

  /* Infinite loop */
  while (1)

    /* USER CODE BEGIN 3 */
	  HAL_Delay (2000);
  • First of all we will declare the external float array, Temp.
  • This array is already defined in the onewire.c file, and this is why we need to define it as an external array here.
  • This will basically store the temperature values from different sensors we have connected.
  • Inside the main function, we will call the get_Romid()
  • This function will scan for all the connected sensors, and sort them according to the ascending order of their IDs.
  • Finally inside the while loop, we will call the get_Temperature()
  • This will read the temperature from the connected sensors, and the values will be stored in the Temp variable.
  • It is advised that you provide at least 1 second delay between two readings.


Below is the image of the debugger with 2 sensors connected.

I am holding one of the sensors, and you can see the reading for both is slightly different.

Check out the Video Below


You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

9 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add to your ad blocking whitelist or disable your adblocking software.