cancel
Showing results for 
Search instead for 
Did you mean: 

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

Emanuele78
Associate III
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
9 REPLIES 9
Emanuele78
Associate III

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

Measuring with a multimeter (See the code snippet I provide below.), the PROVA variable correctly changes state (from 3V to zero), while OneWire_OutputLow(onewire); gives me a value of only 2.1V from 3V (when it’s high), if I keep the external 4.7kOhm pull-up resistor connected.

If I remove the pull-up resistor, I get 2.8V high and 2.1V low.

Is the program able to detect the state change with this modest variability (3V high, 2.1V low)?

I would appreciate a response to the points mentioned above as well.

I'm using the code from https://github.com/lamik/DS18B20_STM32_HAL/blob/master/Src/onewire.c.

Thanks a lot

 

void OneWire_Init(OneWire_t* onewire, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
	HAL_TIM_Base_Start(&_DS18B20_TIMER); // Start the delay timer

	onewire->GPIOx = GPIOx; // Save 1-wire bus pin
	onewire->GPIO_Pin = GPIO_Pin;

	// 1-Wire bit bang initialization
	OneWire_BusOutputDirection(onewire);
	OneWire_OutputHigh(onewire);
	HAL_GPIO_WritePin(PROVA_GPIO_Port, PROVA_Pin, GPIO_PIN_SET);

	HAL_Delay(100);
	OneWire_OutputLow(onewire);
	HAL_GPIO_WritePin(PROVA_GPIO_Port, PROVA_Pin, GPIO_PIN_RESET);

	HAL_Delay(100);
	OneWire_OutputHigh(onewire);
	HAL_Delay(200);
}