Page 1 of 2

[SOLVED] Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 07, 2021 3:59 am
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;
}


Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 07, 2021 6:24 am
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.

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 07, 2021 6:55 am
by dinihuygens
which pin are you using?

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 07, 2021 9:14 am
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

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 07, 2021 3:02 pm
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.

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Sat Jan 09, 2021 7:40 am
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.

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 14, 2021 3:12 am
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

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 14, 2021 3:15 am
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:

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 14, 2021 3:16 am
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

Re: Temp sensor DS18B20 on STM32 sometimes read 85

Posted: Thu Jan 14, 2021 4:04 am
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