Raspberry Pi IoT In C - The DS18B20 Temperature Sensor
Written by Harry Fairhead   
Tuesday, 17 May 2022
Article Index
Raspberry Pi IoT In C - The DS18B20 Temperature Sensor
A Temperature Function

The DS18B20 is the most used of the 1-wire devices. Find out how to use it. This is an extract from the newly-published Raspberry Pi IoT in C, Second Edition.

Raspberry Pi And The IoT In C Second Edition

By Harry Fairhead


Buy from Amazon.


  1. Why Pi For IoT?
  2. Getting Started
  3. Getting Started With The GPIO
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. GPIO The Linux Way
       Extract 1:The Linux GPIO Driver 
  8. Advanced Input – Events, Threads, Interrupts
       Extract 1: Events & Interrupts 
  9. Pulse Width Modulation - Servos And More
       Extract 1:Basic Pulse Width Modulation 
  10. Using The I2C Bus
  11. The DHT22 Sensor Implementing A Custom Protocol
  12. Exploring - 1‑Wire Bus Basics ***NEW!
  13. Using iButtons
  14. DS18B20 Temperature Sensor
      Extract 1: The DS18B20 Temperature Sensor 
  15. The Multidrop 1‑Wire Bus
  16. The Serial Port
      Extract 1: 1-wire Via Serial 
  17. Getting Started With The SPI Bus
  18. A to D With The SPI Bus
  19. Connecting With The Web - Sockets
  20. Memory-Mapped GPIO
  21. Almost Real-Time Linux
  22. Appendix I GPIO Sysfs Interface



Using the software developed in previous chapters we can connect and use the very popular DS18B20 temperature sensor without the need for external drivers.

The Hardware

The DS18B20 is available in a number of formats, but the most common makes it look just like a standard BJT (Bipolar Junction Transistor) which can sometimes be a problem when you are trying to find one. You can also get them made up into waterproof sensors complete with cable.

Exploring Edison - The DS18B20 1-Wire Temperature Sensor


Exploring Edison - The DS18B20 1-Wire Temperature SensorNo matter how packaged, they will work at 3.3V or 5V.

The basic specification of the DS18B20 is:

  • Measures temperatures from -55°C to +125°C (-67°F to +257°F)

  • ±0.5°C accuracy from -10°C to +85°C

  • Thermometer resolution is user-selectable from 9 to 12 bits

  • Converts temperature to 12-bit digital word in 750ms (max)

It can also be powered from the data line, making the bus physically need only two wires - data and ground, however, this "parasitic power" mode is difficult to make work reliably and best avoided in an initial design. To supply it with enough power during a conversion the host has to connect it directly to the data line by providing a "strong pull-up" - essentially a transistor. In normal-powered mode there are just three connections:

Exploring Edison - The DS18B20 1-Wire Temperature SensorGround needs to be connected to the system ground, VDD to 3.3V and DQ to the pull-up resistor of an open collector bus.

Exploring Edison - The DS18B20 1-Wire Temperature Sensor

While you can have multiple devices on the same bus, for simplicity it is better to start off with a single device until you know that everything is working.

Pi IoT In Python Using Linux Drivers -1-Wire And The DS18B20

You can build the circuit in a variety of ways. You can solder the resistor to the temperature sensor and then use some longer wires with clips to connect to the Pi. You could also solder directly to the Pi, which is a good plan for the Pi Zero, or use a prototyping board.

Last Updated ( Tuesday, 17 May 2022 )