Can't Work with DS18B20 with STM blue Pill

Post here first, or if you can't find a relevant section!
Post Reply
asking
Posts: 16
Joined: Mon May 29, 2023 1:15 pm

Can't Work with DS18B20 with STM blue Pill

Post 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;
}

by fpiSTM » Mon May 29, 2023 3:46 pm
You could try this library: https://github.com/PaulStoffregen/OneWire
I already use it and it works.
Go to full post
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

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

Post 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.
fpiSTM
Posts: 1838
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 102
Location: Le Mans
Contact:

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

Post by fpiSTM »

You could try this library: https://github.com/PaulStoffregen/OneWire
I already use it and it works.
asking
Posts: 16
Joined: Mon May 29, 2023 1:15 pm

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

Post by asking »

Thanks for reply, Can you share on which pin of STM Blue pill you are using ?
fpiSTM
Posts: 1838
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 102
Location: Le Mans
Contact:

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

Post by fpiSTM »

The one you want. It is purely gpio code
asking
Posts: 16
Joined: Mon May 29, 2023 1:15 pm

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

Post 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
asking
Posts: 16
Joined: Mon May 29, 2023 1:15 pm

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

Post by asking »

Finally it works on PB5 Pin for me. i had to change resistor to 2.2K. with 1 Meter Cable.
fpiSTM
Posts: 1838
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 102
Location: Le Mans
Contact:

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

Post by fpiSTM »

I've tested and it works, using GenericF103C6 and default sketch:
GenF103C_OneWire.png
GenF103C_OneWire.png (53.09 KiB) Viewed 2097 times
DSB connected with 5V, GND and data on pin 10 (PA10 for this generic). And of course a 4.7 resistor.
Post Reply

Return to “General discussion”