Library 13- Reading temperature with Dallas DS18B20 on STM32F4

DS18B20In the last post, I show OneWire library. Today, I will use that library to show how to read temperature with Dallas’s temperature sensor DS18B20.

DS18B20 Features

  • 9 to 12 bits resolution
  • -55°C to 125°C Celsius
  • 64bit unique ROM number
  • Alarm temperatures enable or disable
  • Search devices with alarm flag set
  • Can be parasite-powered

Library

Features

  • Search sensors
  • Read temperature
  • Set resolution for each device
  • Start temperature conversion on all devices simultaneously
  • Set alarm low and high temperatures
  • Disable alarm temperatures
  • Search devices with alarm flag set
  • Data pin depends on Onewire library pin
  • Version 2.0 – January 04, 2015

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM ONEWIRE
    • TM DELAY
    • defines.h
Conversion time and least significant bit value for DS18B20
Resolution0 9bits 10bits 11bits 12bits
Conversion time 93.75ms 187.5ms 375ms 750ms
LSB 0.5°C 0.25°C 0.125°C 0.0625°C

Before we can start a temperature conversion, we have to know ROM code of DS18B20. If you don’t know it, then use search, that will do this for you. In example on the bottom, search for devices is included.

Start temperature conversion

You can start temperature conversion for specific sensor, or for all sensors simultaneously. Do this with

Reading temperature

Because DS18B20 needs some time (table on top) we can not immediately read temperature, if you try, you will not get correct value. If we know resolution od DS18B20 device, we can just put some delay after temperature conversions has started, or if you want to exactly know when it finished with conversion, you can use

When all devices finish with conversion, you are able to read data from them. To read temperature you have to know ROM address number. Read temperature with

Set/Get resolution

Every DS18B20 has it’s own resolution number from 9 to 12 bits. Higher resolution is, more time sensor needs to convert temperature. If you want to set resolution to DS18B20, you can do this with

You are also able to read resolution. Function will return 9 – 12 bits or 0 if device is not DS18B20.

Alarm high/low temperature

Every DS18B20 can trigger an alarm flag if temperature is greater than high temp, or lower than low temp. High and low temperatures are stored in DS18B20’s internal EEPROM. You can set temperatures with

Remember: You can only select integer numbers between -55 to 125°C. Also know, that these values are stored in EEPROM and are not deleted after power is down.

Disable alarm

If you don’t want to use alarm on specific device, you can disable it. To this with

Check alarm flag

If there are sensors with active alarm, we have somehow recognize devices that has set it. DS18B20 has specific command for search devices which have set alarm flag. You can search for ROM’s that have alarm set with

Functions and enumerations

Example

In example below I used two DS18B20 sensors connected to PD0 pin, with 4k7 pull up resistor.

Project available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...