Page 1 of 1

Can't Work with DS18B20 with STM blue Pill

Posted: Mon May 29, 2023 1:21 pm
by asking
Hi All,

I have been struggling to work with DS18B20 Sensor to get it worked but not working.

I am getting 4095.94 Degree. I have copied code given in this forum.

No change in temperature. What could be wrong ? I have connected on PB5 Pin and Sensor i tried with both 5V and 3.3V with 4.7K Pull up resistor and 0.1uF Capacitor

Please guide what could be wrong ?

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: Can't Work with DS18B20 with STM blue Pill

Posted: Mon May 29, 2023 3:04 pm
by dannyf
two suggestions, based on my quick scan:
1. the output should be open drain / tri-state. essentially you output 1/0 by making the pin input/output.
2. the timing for the write 1/0 doesn't seem to be right.

Re: Can't Work with DS18B20 with STM blue Pill

Posted: Mon May 29, 2023 3:46 pm
by fpiSTM
You could try this library: https://github.com/PaulStoffregen/OneWire
I already use it and it works.

Re: Can't Work with DS18B20 with STM blue Pill

Posted: Wed May 31, 2023 11:04 am
by asking
Thanks for reply, Can you share on which pin of STM Blue pill you are using ?

Re: Can't Work with DS18B20 with STM blue Pill

Posted: Wed May 31, 2023 11:23 am
by fpiSTM
The one you want. It is purely gpio code

Re: Can't Work with DS18B20 with STM blue Pill

Posted: Wed May 31, 2023 11:42 am
by asking
fpiSTM wrote: Mon May 29, 2023 3:46 pm You could try this library: https://github.com/PaulStoffregen/OneWire
I already use it and it works.
Getting Error: (Serial Monitor)
No more Addresses


Image

Re: Can't Work with DS18B20 with STM blue Pill

Posted: Wed May 31, 2023 12:13 pm
by asking
Finally it works on PB5 Pin for me. i had to change resistor to 2.2K. with 1 Meter Cable.

Re: Can't Work with DS18B20 with STM blue Pill

Posted: Wed May 31, 2023 12:14 pm
by fpiSTM
I've tested and it works, using GenericF103C6 and default sketch:
GenF103C_OneWire.png
GenF103C_OneWire.png (53.09 KiB) Viewed 943 times
DSB connected with 5V, GND and data on pin 10 (PA10 for this generic). And of course a 4.7 resistor.