Raspberry Pi + DS18B20 Temperature Sensor

Tested with two DS18B20 temperature probes;
1.) Waterproof DS18B20 Digital Temperature Sensor for Arduino
2.) Digital Temperature Sensor Module. CAT.NO: XC3700


The DS18B20 provides 9 to 12-bit (configurable) temperature readings over a 1-Wire interface, so that only one wire (and ground) needs to be connected from a central microprocessor. Usable with 3.0-5.5V systems.
A 4.7k resistor is required as a pullup from the DATA to VCC line when using the sensor.

3 wires interface:
Type A : Red wire – VCC, Black wire – GND, Yellow wire – DATA
Type B : Red wire – VCC, Yellow wire – GND, Green wire – DATA

If you are using the XC3700Digital Temperature Sensor Module [JAYCAR] check the pin mapping as it isn’t a direct replica of the DS18B20. The 4.7k resistor is also preinstalled.


For the Raspberry Pi,
DATA is on GPIO4 by default, pin 7
VCC to pin 1 (3V3)
GND to pin 6 (GND)


To enable 1-Wire, from the Raspberry Pi Configuration tool, found in the Preferences menu. In the Interfaces tab click on Enable for the 1-Wire interface

Device ID?

cd /sys/bus/w1/devices/


The Python library is called w1thermsensor

sudo pip3 install w1thermsensor

Python script to check the temperature.

import time
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()

while True:
    temperature = round(sensor.get_temperature())
    print("The temperature is %s celsius" % temperature)


  1. How it works: DS18B20 and Arduino [Tushev]
  2. Measuring Temperature with a Raspberry Pi Temperature Sensor (1-Wire) [tutorials-raspberrypi.com]
  3. Sensors – Temperature with the 1-Wire interface and the DS18B20 [The PiHut]
  4. DS18B20 Temperature Sensor With Python (Raspberry Pi) (2017-Jun-25) [bigl.es]
  5. Temperature Sensing With Raspberry Pi (2017-Oct-02) [core-electronics]
  6. OneWire Library: OneWire Arduino Library, connecting 1-wire devices (DS18S20, etc) to Teensy [PJRC]
  7. Raspberry Pi DS18B20 Temperature Sensor Tutorial [Circuit Basics]
This entry was posted in 2019, geek. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.