DS18b20 only works after hot-plug

What are you developing?
Post Reply
Ooozair
Posts: 7
Joined: Fri Jan 15, 2021 5:24 pm

DS18b20 only works after hot-plug

Post by Ooozair »

Hello all,

I have a custom PCBA that is built around an STM32F103CBT6 microcontroller, and I need to read temperatures from a DS18b20 sensor.

I am using the STM32Duino core, not the rogerclark one.

I have two libraries that govern this, the Arduino-Temperature-Control-Library by MilesBurton and OneWire by PaulS

My sensor is wired as such:
Pin 1: 5V+
Pin 2: Pullup to 5V with 4.7K resistor, and also goes to the microcontroller PA8 pin
Pin 3: Ground

When my code first runs, I read -196, indicating a bad connection possibly. However, if I unplug the sensor and plug it back in, I get a reading. The reading then works just fine, it goes up and down when I touch the sensor, and is accurate as far as I can tell.

So, it appears that the code is initializing the sensor alright, but reading it properly is requiring a power cycle of the sensor? It's just odd. I've tried to read on all of the other forums and the github for STM32Duino and tried various suggestions from those posts but haven't had any luck.

Could it be because I've wired the sensor to 5V, when I really should have wired it to 3.3V to be the same as the microcontroller? What do you guys think? Have any of you run one of these sensors off a STM32F103CBT6 successfully? I'm locked into this sensor because I'm updating a legacy product to use a new microcontroller, otherwise I'd just use a thermistor and call it a day.

Here's my code segments.

Declaration:

Code: Select all

#define SENSORPIN PA8
OneWire Bus1(SENSORPIN); // Create a 1-wire object
DallasTemperature sensor1(&Bus1);
In Setup():

Code: Select all

 sensor1.begin();
  delay(750);
  sensor1.requestTemperatures();
In my routine to request temperatures

Code: Select all

void gettemps()
{
  sensor1.requestTemperatures(); // Send the command to get temperatures
  temp1 = sensor1.getTempFByIndex(0);
  //temp1 is an int declared globally
}
TlDhST
Posts: 2
Joined: Thu Jun 23, 2022 12:43 pm

Re: DS18b20 only works after hot-plug

Post by TlDhST »

Hello @Ooozair,
I reproduced the similar setup (Nucleo F103RB), i 'm using the STM32DUINO core and i installed the two library the Arduino-Temperature-Control-Library by MilesBurton and OneWire by PaulS.
I First I ran the code with OneWire library and i get a reading from the first time. After I ran the code with the DallasTemperature library and it also worked the first time.
So could you please check that you are using the right pin, and check your core version if it is the latest.
Best regards
Ooozair
Posts: 7
Joined: Fri Jan 15, 2021 5:24 pm

Re: DS18b20 only works after hot-plug

Post by Ooozair »

Thanks for the reply @TlDhST

I just updated my core via PlatformIO and I'm getting the same behavior. I read -196 on startup but room temp after a hotswap.

You mentioned you first ran the code with OneWire and then ran the code with DallasTemperature - I'm unfamiliar with any method that runs them separately, I only know the method of creating a bus with OneWire and then creating a sensor object with DallasTemperature referencing that bus.

Could you share your code setup for what you described?

I've double checked my pins- it's on a PCBA so theres no breadboard miswiring happening here. Did you connect your sensor to 5V or 3.3V? I'm away from my soldering station for another day so I cant cut and jump this 5V trace to test that myself so I'm wondering how yours is set up.
TlDhST
Posts: 2
Joined: Thu Jun 23, 2022 12:43 pm

Re: DS18b20 only works after hot-plug

Post by TlDhST »

Hello @Ooozair ,
I understand that you use PlatformIO, I used Arduino IDE to run my tests.
I executed basics examples provided by DallasTemperature and OneWire:
Dallastemperature example: https://github.com/milesburton/Arduino- ... les/Simple
OneWIre example: https://github.com/PaulStoffregen/OneWi ... mperature
I connected my sensor to 5V pin alimenattion, about the setup and the pin data is mentioned in the example sketch.
Best regards
Phono
Posts: 68
Joined: Thu Mar 19, 2020 9:32 am

Re: DS18b20 only works after hot-plug

Post by Phono »

This may be a bit off topic, but I have done several projects using the 18B20 and I noticed that this chip is very prone to latchup. So much that for a reliable operation, I do not connect the +Vcc pin directly to the Vcc of the system, but instead, I drive it using an extra pin of the microcontroller. Its very small consumption can easily be handled by a pin.
Then, to do a reading, I switch it on, perform the reading, then switch it off. So that is is not powered between readings.
The latchup problem is especially serious when the 18B20 is several meters away, and the ringing as well as the emi pickup by the wires suffices to trigger a latchup state.
In systems with several sensors, I power them separately, which relieves me from the cumbersome addressing scheme. (In case of several sensors, you have to register their unique identifiers at least once, and to do this, you probably need to switch them on and off in turn, so this hardware complexity is likely to be required anyway).
Post Reply

Return to “Projects”