Skip to main content
Associate II
October 10, 2024
Question

DS18B20 Sensor Detection via ONE-WIRE (1-Wire) Protocol

  • October 10, 2024
  • 5 replies
  • 6110 views
Hi, where can I find a working One-Wire protocol? Is there an example provided by STM? I wrote code for a program to detect the temperature from a DS18B20 sensor, but I can’t detect the sensor, even though the debug shows everything is fine. Can someone help me? Thanks

5 replies

Associate II
October 10, 2024

Hi, where can I find a working One-Wire protocol? Is there an example provided by STM? I wrote code for a program to detect the temperature from a DS18B20 sensor, but I can’t detect the sensor, even though the debug shows everything is fine. Can someone help me? Thanks

Andrew Neil
Super User
October 10, 2024

@Emanuele78 wrote:
where can I find a working One-Wire protocol?

From the manufacturer:

https://www.analog.com/en/resources/technical-articles/interfacing-the-ds18x20ds1822-1wire-temperature-sensor-in-a-microcontroller-environment.html

See also:

https://www.analog.com/en/resources/technical-articles/using-a-uart-to-implement-a-1wire-bus-master.html

 

Useful background info:

https://www.analog.com/en/resources/technical-articles/guidelines-for-reliable-long-line-1wire-networks.html

https://www.analog.com/en/resources/technical-articles/advanced-1-wire-network-driver.html

https://www.analog.com/en/resources/technical-articles/choosing-right-1-wire-master-for-embedded-applications.html

 

There are also UART-to-1-Wire and I2C-to-1-Wire hardware bridge chips.

 


@Emanuele78 wrote:
 even though the debug shows everything is fine.  

What "debug" is that?

Did you look at the 1-Wire line with an oscilloscope to see what was actually happening?

A useful feature of 1-Wire is that the timing between "Slots" (ie, bits) is non-critical - so you can step your code one complete Slot at a time ...

 

#1Wire

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate II
October 10, 2024

Thanks, I will try to compare the links with my code.

Unfortunately, I don't have an oscilloscope, but if necessary, I will make sure to get one

I performed a debug with CUBEIDE using F6, placing breakpoints to check if the microcontroller detects the sensor, but it never finds it. The quantity variable in the following code is always zero.

/* Infinite loop */

/* USER CODE BEGIN WHILE */



while (1)

{

DS18B20_ReadAll();

HAL_GPIO_WritePin(Test_GPIO_Port, Test_Pin, 1);

DS18B20_StartAll();

HAL_GPIO_WritePin(Test_GPIO_Port, Test_Pin, 0);

uint8_t ROM_tmp[8];

uint8_t i;



int quantity = DS18B20_Quantity(); // quantity always = 0
Andrew Neil
Super User
October 10, 2024

Please see the Posting Tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 


@Emanuele78 wrote:

I performed a debug with CUBEIDE using F6, placing breakpoints to check if the microcontroller detects the sensor, but it never finds it. T= 0


Getting the correct timing on the wire is critical to 1-Wire operation - the CubeIDE debugger will not tell you anything about that.

It's also essential that your external hardware is correct - again, the CubeIDE debugger will not tell you anything about that.

This is why an oscilloscope is an essential tool for embedded firmware development: you need to be able to see what's happening in the real world of the hardware - not just the software.

https://community.st.com/t5/stm32-mcus-products/i-ran-printf-over-uart-but-the-results-were-not-displayed-in/m-p/723772/highlight/true#M261747

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Senior
September 19, 2025

Hi. I released new one wire library. It is non-blocking and can run on any pins.

Https://github.com/nimaltd/ow

Andrew Neil
Super User
September 19, 2025

EDIT: Oops - I missed that this is an old post (nearly a year), which has just been woken up by @nimaltd

So all of this has pretty much been said already:

 

First call should always be to see what the manufacturer says:

Choosing the Right 1-Wire Master for the Embedded Applications

 

Application Note 148: Guidelines for Reliable Long Line 1-Wire® Networks

There is basic, MCU-independent code here:

Interfacing the DS18X20/DS1822 1-Wire® Temperature Sensor in a Microcontroller Environment

 

See also:

https://community.st.com/t5/stm32-mcus-embedded-software/ds18b20-sensor-detection-via-one-wire-1-wire-protocol/m-p/729945/highlight/true#M55860

 


@Emanuele78 wrote:
even though the debug shows everything is fine.

Clearly everything is not fine - it doesn't work!

 

A software debugger probably won't show hardware problems.

Are you sure that the DS18B20 actually works?

Is it wired correctly?

Please show your schematics; some good, clear photos of your setup may also help - see:

How to write your question to maximize your chances to find a solution

 

Have you used an oscilloscope to see what's actually happening on the wire?

The DS18B20 datasheet & links above show what you should be seeing...

 

Note that DS18B20 are commonly faked - are you sure you have a genuine one?

Does it work with any other controller; eg, Arduino?

See:

https://community.st.com/t5/others-stm32-mcus-related/ds18b20-one-works-one-doesn-t-on-stm32l071-both-ok-on-arduino/m-p/833866/highlight/true#M7818


@Emanuele78 Are you still there? Did you fix this?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Senior
September 19, 2025

Yes :) @Andrew Neil 

Because I released a library. It is non-blocking and very simple. And ready to use. I tested with 2 ds18b20 on bus and works perfectly. 

 

Andrew Neil
Super User
September 19, 2025

Just to collect together some more 1-WireTM tips:

  1. Although the timing within a bit frame is critical, the timing between bits is not.
    So a blocking implementation only really needs to block for each bit - not the whole transaction.
    This also helps debugging - you can still breakpoint between bits.

  2. UART and SPI can be used to "synthesise" the timing.
    See, for example: Using a UART to Implement a 1-Wire Bus Master
    discussed here: Dallas iButton and UART.

#1WireTips

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.