KY-001 Temperature sensor (DS18B20)
The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements.
- Arduino
- Raspberry Pi
- Raspberry Pi Pico
- Micro:Bit

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with non-volatile, user-programmable high and low trigger points.
This means that the sensor has a programmable upper and lower limit. The DS18B20 communicates via a 1-wire bus, which requires only one data line for communication with a central microprocessor. In addition, the DS18B20 can draw power directly from the data line ("parasite power"). This eliminates the need for an external power source. Each DS18B20 has a unique 64-bit serial code. This allows multiple sensors to operate on the same 1-Wire bus and be evaluated by only one microprocessor.
Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems in buildings, plants or machinery, and process monitoring and control systems.
Technical data
Chipset | DS18B20 |
Communication Protocol | 1-Wire |
Accuracy | 9 to 12 Bit |
Measuring range | -55 °C to +125 °C |
Measurement accuracy | ±0.5 °C from -10 °C to +85 °C |
Pin assignment
Code example Arduino
Pin assignment Arduino:
Arduino | Sensor |
---|---|
Pin 4 | signal |
5 V | +V |
GND | GND |
For the following code example two additional libraries are needed:
OneWire Library by. Paul Stoffregen | published under the MIT License
Dallas Temperature Control Library by Miles Burton | released under LGPL.
Both libraries are included in the package and must be copied to the "library" folder before starting the Arduino IDE.
This can be found by default under the following path of your Windows installation:
C:\user[username]\documents\Arduino\libraries
// Required libraries will be imported
#include <OneWire.h>
#include <DallasTemperature.h>
// Here the input pin is declared to which the sensor module is connected
#define KY001_Signal_PIN 4
// Libraries are configured
OneWire oneWire(KY001_Signal_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
// Initialize serial output
Serial.begin(9600);
Serial.println("KY-001 temperature measurement");
// Sensor is initialized
sensors.begin();
}
//main program loop
void loop()
{
// Temperature measurement is started...
sensors.requestTemperatures();
// ... and output measured temperature
Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.println(" °C");
delay(1000); // 1s pause until next measurement
}
Example program download

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with non-volatile, user-programmable high and low trigger points.
This means that the sensor has a programmable upper and lower limit. The DS18B20 communicates via a 1-wire bus, which requires only one data line for communication with a central microprocessor. In addition, the DS18B20 can draw power directly from the data line ("parasite power"). This eliminates the need for an external power source. Each DS18B20 has a unique 64-bit serial code. This allows multiple sensors to operate on the same 1-Wire bus and be evaluated by only one microprocessor.
Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems in buildings, plants or machinery, and process monitoring and control systems.
Technical data
Chipset | DS18B20 |
Communication Protocol | 1-Wire |
Accuracy | 9 to 12 Bit |
Measuring range | -55 °C to +125 °C |
Measurement accuracy | ±0.5 °C from -10 °C to +85 °C |
Pin assignment
Code example Raspberry Pi
Pin assignment Raspberry Pi:
Raspberry Pi | Sensor |
---|---|
GPIO4 [Pin 7] | Signal |
3.3 V [Pin 1] | +V |
GND [Pin 6] | GND |
One-Wire Configuration Raspberry Pi
In order for the Raspberry Pi to communicate with the One-Wire bus, it must first be activated. For this, the file "/boot/config.txt" must be edited. Therefore open the file with the following command:
sudo nano /boot/config.txt
Add the following content to the end of the file:
dtoverlay=w1-gpio,gpiopin=4
Afterwards you can save the file with the key combination [CTRL+O]. Confirm this with [Enter] and exit the editor with [CTRL+X]. Now restart your Raspberry Pi with the following command:
sudo reboot
Now you can apply the following example. Here a console output is generated as soon as a signal is detected.
# coding=utf-8
# Required modules are imported and set up
import glob
import time
from time import sleep
import RPi.GPIO as GPIO
# At this point the pause between the individual measurements can be set
sleeptime = 1
# The One-Wire input pin is declared and the integrated PullUp resistor is activated
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# After activation of the Pull-UP resistor it waits
# until the communication with the DS18B20 sensor is established.
print ("Waiting for initialization...")
base_dir = '/sys/bus/w1/devices/'
while True:
try:
device_folder = glob.glob(base_dir + '28*')[0]
break
except IndexError:
sleep(0.5)
continue
device_file = device_folder + '/w1_slave'
# Function is defined, with which the current measured value can be read out at the sensor.
def TemperaturMessung():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
# For initialization, the sensor is read out "blind" once.
TemperaturMessung()
# The temperature evaluation: On the Raspberry Pi, detected one-Wire slaves are assigned to a separate subfolder in the folder
# /sys/bus/w1/devices/ are assigned to an own subfolder. In this folder is the file w1-slave
# in which the data sent over the one-wire bus is stored.
# In this function these data are analyzed and the temperature is read out and output.
def TemperaturAuswertung():
lines = TemperaturMessung()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = TemperaturMessung()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
# Main program loop
# The measured temperature is output to the console - between the individual measurements
# is a pause, the length of which can be set with the "sleeptime" variable
try:
while True:
print ("---------------------------------------")
print ("Temperature:", TemperaturAuswertung(), "degC")
time.sleep(sleeptime)
except KeyboardInterrupt:
GPIO.cleanup()
Example program download
To start with the command:
sudo python3 KY001-RPi.py

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with non-volatile, user-programmable high and low trigger points.
This means that the sensor has a programmable upper and lower limit. The DS18B20 communicates via a 1-wire bus, which requires only one data line for communication with a central microprocessor. In addition, the DS18B20 can draw power directly from the data line ("parasite power"). This eliminates the need for an external power source. Each DS18B20 has a unique 64-bit serial code. This allows multiple sensors to operate on the same 1-Wire bus and be evaluated by only one microprocessor.
Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems in buildings, plants or machinery, and process monitoring and control systems.
Technical data
Chipset | DS18B20 |
Communication Protocol | 1-Wire |
Accuracy | 9 to 12 Bit |
Measuring range | -55 °C to +125 °C |
Measurement accuracy | ±0.5 °C from -10 °C to +85 °C |
Pin assignment
Code example Micro:Bit
Pinout Micro:Bit:
Micro:Bit | Sensor |
---|---|
Pin 1 | Signal |
3 V | +V |
GND | GND |
An additional library is needed for the following code example:
pxt-ds18b20 from DFRobot | published under the GUI License.
Add the library to your IDE by clicking on "Extensions" and entering the following URL in the search box: https://github.com/DFRobot/pxt-ds18b20.git Confirm the search with [Enter].

Example program download

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with non-volatile, user-programmable high and low trigger points.
This means that the sensor has a programmable upper and lower limit. The DS18B20 communicates via a 1-wire bus, which requires only one data line for communication with a central microprocessor. In addition, the DS18B20 can draw power directly from the data line ("parasite power"). This eliminates the need for an external power source. Each DS18B20 has a unique 64-bit serial code. This allows multiple sensors to operate on the same 1-Wire bus and be evaluated by only one microprocessor.
Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems in buildings, plants or machinery, and process monitoring and control systems.
Technical data
Chipset | DS18B20 |
Communication Protocol | 1-Wire |
Accuracy | 9 to 12 Bit |
Measuring range | -55 °C to +125 °C |
Measurement accuracy | ±0.5 °C from -10 °C to +85 °C |
Pin assignment
Code example Raspberry Pi Pico
Pin assignment Raspberry Pi Pico
Raspberry Pi Pico | Sensor |
---|---|
GPIO2 | Signal |
3,3V | +V |
GND | GND |
Two additional libraries are needed for the following code example:
OneWire Library by Damien P. George | published under the MIT License.
DS18X20 Library by Damien P. George | published under the MIT License.
This is an example program which outputs the measured temperature serially after initialization of the sensor.
# Load libraries
import machine, onewire, ds18x20
from time import sleep
# Initialization of GPIO2
ds_pin = machine.Pin(10)
# Initialization of the sensor object
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
# Search for all matching sensors
roms = ds_sensor.scan()
# Serial output
print("Found DS devices")
print("Temperature (°C)")
# Endless loop for continuous reading of the temperature
while True:
ds_sensor.convert_temp()
sleep(1)
# Based on the number of compatible sensors found it will count up
for rom in roms:
# Serial output of the measured temperature
print(ds_sensor.read_temp(rom))
sleep(3)