IOT

How to interface DS18B20 Temperature Sensor with STM32?

We will learn how to interface a DS18B20 temperature sensor with an STM32 microcontroller in this tutorial. The DS18B20 does not support the Arduino Dallas and OneWire libraries. This is why we must write programs without using any libraries. Because there is no library involved and all of the addressing modes are defined in the code, the DS18B20 Code for STM32F103C is a little large and complex.

The DS18B20 is a one-wire waterproof sensor that measures liquid temperature between -55 and 125°C. Without the use of an external power source, power for reading, writing, and performing temperature conversions can be generated from the data line itself. Multiple DS18B20s can exist on the same board since each DS18B20 has its own silicon serial number, multiple DS18B20s can exist on the same 1-Wire bus.

DS18B20 Waterproof Digital Temperature Sensor

The DS18B20 sensor is a pre-wired and waterproofed temperature sensor. When you need to measure anything from afar or in damp conditions, this tool comes in handy. The sensor can detect temperatures ranging from -55 to 125 degrees Celsius (-67 degrees Fahrenheit to +257 degrees Fahrenheit). The cable is PVC jacketed.

The 3-wire digital temperature sensor offers a fairly precise of ±0.5°C over much of the range. Hence the temperature with a precision of 0.0625 C was measured. It can give output digitally accurate to 12 bits of precision from the onboard digital-to-analog converter. The sensor is connected to Arduino via the dedicated 1-wire protocol. As a result, several sensors can be connected only through one common wire to Arduino. Each one has a unique 64-bit ID burned in at the factory to differentiate them. Thus convert 12-bit data at a maximum conversion time of 750ms.

Moreover, this is the latest DS18B20 1-Wire digital temperature sensor that operates with 3.0-5.0V systems providing  9 to 12-bit precision, at a Temperature measurement range of-55C to +125C, be accurate to 0.5C.  When using with microcontroller put a 4.7k resistor to the sensing pin, required as a pullup from the DATA to the VCC line. Finally, the Alarm search command identifies and addresses devices whose temperature is outside of programmed limits.

Features

  • Resolution: 9bits to 12bits (Selectable)
  • Supply Voltage (V): 3 to 5.5
  • Temperature Range(°C): -55 to +125
  • Measuring Accuracy(°C): ±0.5
  • Uses 1-Wire interface- requires only one digital pin for communication
  • Unique 64 bit ID burned into the chip
  • Multiple sensors can share one pin
  • Maximum conversion time and response: 750 ms
  • Multidrop capability simplifies distributed temperature sensing applications
  • Requires no external components
  • Digital signal output
  • 18B20 Temperature Sensor Chip
  • Send data via a pin
  • User-definable nonvolatile (NV) alarm settings
  • Dimension: 19*15*6cm
  • Weight: 9gm

 STM32F103C DISCOVERY BOARD

STM32F103C8T6 Discovery Board was designed as a development board that is similar to Arduino in terms of advanced capabilities and accessibility. The STM32 Discovery Board enables the development of high-reliability applications by utilizing an advanced performance microcontroller known as the Arm Cortex-M4 32-bit core. I think you’re familiar with ARM Architecture. It provides versatility and customization, allowing you to experiment with libraries, communication protocols, GPIO pins, and so on.

Interfacing DS18B20 Temperature Sensor with STM32

Let’s connect the DS18B20 sensor to the STM32F103C Bluepill Board now. Below is the connecting diagram.

The STM32’s 3.3V pin powers the sensor, and GND is wired to GND. The digital Pin is also connected to Bluepill’s PA8 pin. A 4.7K resistor is used to pull the digital pin.

DS18B20 STM32 Code

To read the temperature measured by the DS18B20, upload the following code to the STM32F103C board. To upload the code, you can use any method. I used the Serial method and the USB-TTL Converter Module to upload the code.

 
int DSPIN = PA8;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}
 
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;
}

Open the Serial Monitor once the code has been uploaded. The Room Temperature will be displayed on the Serial Monitor right away.

Conclusion
Hope this blog helps you to understand the Interfacing of DS18B20 Temperature Sensor with STM32F103C8T6 MCU based STM32 Blue Pill Board. We, MATHA ELECTRONICS  will come back with more informative blogs.

Leave a Reply

Your email address will not be published.