cancel
Showing results for 
Search instead for 
Did you mean: 

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

Emanuele78
Associate
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
8 REPLIES 8
Emanuele78
Associate

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
Evangelist III

@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

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

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

 

Another question: if I run the program, it terminates after a few seconds. Can I understand through CUBEIDE why the program terminated? The only feedback I see is the attached image. Thanks again

In reference to the external hardware, it seems to me that the connections are correct, as can be seen from the attached photos. Don't you think? Thanks

Please post a schematic

Here it is.

 

Another question: if I run the program, it terminates after a few seconds. Can I understand through CUBEIDE why the program terminated? The only feedback I see is the attached image. Thanks again