[SOLVED] Temp sensor DS18B20 on STM32 sometimes read 85

Post here first, or if you can't find a relevant section!
dinihuygens
Posts: 13
Joined: Thu Dec 17, 2020 9:20 am
Answers: 1

[SOLVED] Temp sensor DS18B20 on STM32 sometimes read 85

Post by dinihuygens »

I'm using a temperature sensor DS18B20 with STM32F103 Bluepill, but sometimes this sensor gives me 85°C which is the power-on reset value of the temperature register. I'm so confused because sometimes it can give me the correct value but mostly is 85°C. I have put a 4.7K resistor as the pull-up resistor. I have read some posts regarding this problem but it doesn't gimme solutions. For the data pin, I'm using PB5 on my STM32F103 and I'm using Arduino_STM32 core official. I have tested it before, using Arduino Uno and Wemos D1 mini. When I was using those boards I didn't face any problems. Any suggestions?
This is my code:

Code: Select all


int DSPIN = PB5;
void setup() {
  
  // put your setup code here, to run once:
  Serial.begin(9600);
}
 
void loop()
{
  // put your main code here, to run repeatedly:
  double temp = TempRead();
  temp  = temp * 0.0625; // conversion accuracy is 0.0625 / LSB
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println(" °C");
  Serial.println("");
  delay(500);
}
 
boolean DS18B20_Init()
{
  pinMode(DSPIN, OUTPUT);
  digitalWrite(DSPIN, HIGH);
  delayMicroseconds(5);
  digitalWrite(DSPIN, LOW);
  delayMicroseconds(750);//480-960
  digitalWrite(DSPIN, HIGH);
  pinMode(DSPIN, INPUT);
  int t = 0;
  while (digitalRead(DSPIN))
  {
    t++;
    if (t > 60) return false;
    delayMicroseconds(1);
  }
  t = 480 - t;
  pinMode(DSPIN, OUTPUT);
  delayMicroseconds(t);
  digitalWrite(DSPIN, HIGH);
  return true;
}
 
void DS18B20_Write(byte data)
{
  pinMode(DSPIN, OUTPUT);
  for (int i = 0; i < 8; i++)
  {
    digitalWrite(DSPIN, LOW);
    delayMicroseconds(10);
    if (data & 1) digitalWrite(DSPIN, HIGH);
    else digitalWrite(DSPIN, LOW);
    data >>= 1;
    delayMicroseconds(50);
    digitalWrite(DSPIN, HIGH);
  }
}
 
byte DS18B20_Read()
{
  pinMode(DSPIN, OUTPUT);
  digitalWrite(DSPIN, HIGH);
  delayMicroseconds(2);
  byte data = 0;
  for (int i = 0; i < 8; i++)
  {
    digitalWrite(DSPIN, LOW);
    delayMicroseconds(1);
    digitalWrite(DSPIN, HIGH);
    pinMode(DSPIN, INPUT);
    delayMicroseconds(5);
    data >>= 1;
    if (digitalRead(DSPIN)) data |= 0x80;
    delayMicroseconds(55);
    pinMode(DSPIN, OUTPUT);
    digitalWrite(DSPIN, HIGH);
  }
  return data;
}
 
int TempRead()
{
  if (!DS18B20_Init()) return 0;
  DS18B20_Write (0xCC); // Send skip ROM command
  DS18B20_Write (0x44); // Send reading start conversion command
  if (!DS18B20_Init()) return 0;
  DS18B20_Write (0xCC); // Send skip ROM command
  DS18B20_Write (0xBE); // Read the register, a total of nine bytes, the first two bytes are the conversion value
  int temp = DS18B20_Read (); // Low byte
  temp |= DS18B20_Read () << 8; // High byte
  return temp;
}

by dinihuygens » Fri Jan 15, 2021 6:16 am
thanks all, finally I can overcome this issue by changing the wire cable of my sensor :D
I just cut it off and replace it with the new one
Go to full post
Last edited by dinihuygens on Fri Jan 15, 2021 6:14 am, edited 1 time in total.
fredbox
Posts: 98
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by fredbox »

Your code seems to work on my sensor. I'm not seeing any unexpected readings.
I have a 0.1uf capacitor across 3.3v and GND, but it is mostly there to have a place to land the dupont wires.
I am using same board and core as you.
dinihuygens
Posts: 13
Joined: Thu Dec 17, 2020 9:20 am
Answers: 1

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by dinihuygens »

which pin are you using?
User avatar
fpiSTM
Posts: 865
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by fpiSTM »

Tested too on several boards with different MCU and it works.
I guess it is more a wiring issue (resistor?).
You can also try with https://github.com/PaulStoffregen/OneWire
fredbox
Posts: 98
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by fredbox »

PB5 - I copied and pasted your code without any modification.

Code: Select all

+------+
| Vcc  |---+-----+------> 3.3V
|      |   |     |
|      |   |    | | 4.7K resistor
|      |   |     |
| Data |---------+------> PB5
|      |   |
|      |  ---
|      |  ---  0.1uf capacitor
|      |   |
| GND  |---+------------>GND
+------+
DS18B20
Mine is one of those waterproof sensors with a cable attached. Red=Vcc, Blue=Data, Black=Gnd.
zoomx
Posts: 24
Joined: Fri Dec 20, 2019 10:12 am
Location: Near Mt.Etna

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by zoomx »

From
https://forum.arduino.cc/index.php?topi ... msg1488781
There is a footnote on page 4 which says "The power-on reset value of the temperature register is +85°C."
so if you read 85 degrees it means it hasn't done a conversion since the last power on.
You hadn't wired it up properly so the sensor was being powered off and on periodically, which resets the register to 85 and that is why you got a valid CRC from it.
Maybe are you using a PAR version? Is there a P at the end of the label, in other words it's written DS18B20P?

Anyway seems that 85 is a sort of error code.
dinihuygens
Posts: 13
Joined: Thu Dec 17, 2020 9:20 am
Answers: 1

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by dinihuygens »

Sorry, it might be late. I still working on this issue. I have tried to change my PCB design too but still can't overcome this issue. Anyway, I have tried in a breadboard, and same as you guys it worked as well. I don't know why it doesn't work on PCB, I did the same wiring as I did on a breadboard. Does it require any additional setting while using that sensor in a PCB with an external power supply like Li-Po battery? I have put a 4.7K resistor too. Here I attached my schematic:
https://github.com/Proxcentaur/stm32-ds ... ematic.PNG
dinihuygens
Posts: 13
Joined: Thu Dec 17, 2020 9:20 am
Answers: 1

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by dinihuygens »

zoomx wrote:
Sat Jan 09, 2021 7:40 am
From
https://forum.arduino.cc/index.php?topi ... msg1488781
There is a footnote on page 4 which says "The power-on reset value of the temperature register is +85°C."
so if you read 85 degrees it means it hasn't done a conversion since the last power on.
You hadn't wired it up properly so the sensor was being powered off and on periodically, which resets the register to 85 and that is why you got a valid CRC from it.
Maybe are you using a PAR version? Is there a P at the end of the label, in other words it's written DS18B20P?

Anyway seems that 85 is a sort of error code.
I can't check this out, because I have bought it a long time ago and there is no information available on my sensor :oops:
dinihuygens
Posts: 13
Joined: Thu Dec 17, 2020 9:20 am
Answers: 1

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by dinihuygens »

fredbox wrote:
Thu Jan 07, 2021 3:02 pm
PB5 - I copied and pasted your code without any modification.

Code: Select all

+------+
| Vcc  |---+-----+------> 3.3V
|      |   |     |
|      |   |    | | 4.7K resistor
|      |   |     |
| Data |---------+------> PB5
|      |   |
|      |  ---
|      |  ---  0.1uf capacitor
|      |   |
| GND  |---+------------>GND
+------+
DS18B20
Mine is one of those waterproof sensors with a cable attached. Red=Vcc, Blue=Data, Black=Gnd.
I did it too, but that error still occurs
dinihuygens
Posts: 13
Joined: Thu Dec 17, 2020 9:20 am
Answers: 1

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Post by dinihuygens »

fpiSTM wrote:
Thu Jan 07, 2021 9:14 am
Tested too on several boards with different MCU and it works.
I guess it is more a wiring issue (resistor?).
You can also try with https://github.com/PaulStoffregen/OneWire
I have tried this, but the output sometimes -127 and sometimes 85
Post Reply

Return to “General discussion”