DS18B20 with Raspberry Pi Pico using MicroPython
Summary
DS18B20 is a 1-wire programmable temperature sensor which requires only one data line for communication. Today we will highlight the important facts on DS18B20. Also, we would learn how to use DS18B20 with Raspberry Pi Pico using a micro python. So, let's begin!
Introduction
The Raspberry Pi Pico is a microcontroller board that, when combined with the DS18B20 temperature sensor from Maxim Integrated, provides users with an easy and reliable way to measure temperatures. With this combination of products, it's now possible to implement cost-effective monitoring or even smart home automation projects. The Raspberry Pico itself offers robust performance features such as built-in USB support and a native MicroPython programming environment so that you can implement your project quickly without having to learn too much about coding languages at the moment.
Furthermore, due to its higher GPIO speeds than previous versions of the RPi boards - up to 50 MHz - more complex applications become achievable while conserving power in comparison with other alternatives available on the market today. All these great qualities make Raspberry Pi Pico coupled with DS18b20 an ideal choice for measuring temperatures accurately yet reliably saving energy resources in the long run!
What is DS18B20?
The DS18B20 is a 1-wire programmable temperature sensor from Maxim integrated that requires only one data line for communication with a central microprocessor. Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area.
Applications
- Thermostatic Controls
- Industrial Systems
- Consumer Products
- Thermometers
- Thermally Sensitive Systems
Material Required is as follows:
Circuit Diagram

- Connect the sensor's ground to one of the ground pins on the Pico.
- Connect the sensor's VCC pin to the Pico's 3.3V(out) pin.
Code and Library
Setup library- For DS18B20 we need OneWire and DS18X20 libraries. Download from Github
- Visit the link for the respective library.
- Click on the file present inside the downloaded folder and then copy the contents of the entire file .
- Click on the “New” button on the Thonny IDE to open a blank script and paste the copied code.
- Click on the save button and when prompted to choose
- When asked for save location, choose Raspberry Pi Pico.4
- Once the dialog box opens to choose the save location on the Pico, double click on the “lib” folder present already to save the file inside it. If the lib folder does not exist, create it and then save the file inside it.
- Enter the same name for the file as on the GitHub repo and press the Ok button. To add the downloaded library, open Thonny and navigate to the following directory in the Pico.
Thonny > File > Open > Raspberry Pi Pico > lib
- Copy the library code and save the file with .py extension.
import machine, onewire, ds18x20, time
ds_pin = machine.Pin(22)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
print('Found DS devices: ', roms)
while True:
ds_sensor.convert_temp()
time.sleep_ms(750)
for rom in roms:
print(rom)
print(ds_sensor.read_temp(rom))
time.sleep(5)
Copy the above code into thonny ide and run the code.

You can see output into Thonny Shell
Troubleshooting
Solution: Check your connections, make sure the connections are correct and not loose. Also, check-in the code if the pin number is correct.
Excerpt
Frequently Asked Questions
1. What is a DS18B20 sensor?
The DS18B20 is a digital temperature sensor widely used in electronics and robotics projects. It provides highly accurate temperature readings ranging from -55°C to +125°C with a resolution of 0.5°C. Its 1-Wire interface allows multiple sensors to be connected over a single data line, making it convenient for various applications.
2. How to connect DS18B20 to Raspberry Pi Pico?
To connect the DS18B20 to Raspberry Pi Pico, wire the sensor's VDD pin to a 3.3V power source, GND to ground, and the data pin to a GPIO pin (like GP15). Ensure to add a 4.7kΩ pull-up resistor between the data pin and VDD. This setup allows proper data communication.
3. How to read temperature data in MicroPython?
To read temperature data in MicroPython, use the OneWire library to communicate with the DS18B20. Initialize the library, locate the sensor, and read the data using the sensor's commands. Finally, convert the data to Celsius, and you’ll have real-time temperature readings for your project.
4. What libraries are required?
You will need the OneWire and DS18X20 libraries to interface with the DS18B20 sensor in MicroPython. The OneWire library handles data communication over the 1-Wire protocol, while the DS18X20 library simplifies temperature reading and conversion processes.
5. Can multiple DS18B20 sensors be used?
Yes, you can connect multiple DS18B20 sensors on the same 1-Wire bus. Each sensor has a unique address, allowing you to identify and read data from each one individually. This makes it ideal for complex temperature monitoring applications.
6. What is the output format of the sensor?
The DS18B20 outputs temperature data in a 12-bit digital format, typically represented as a signed integer. Temperature values can be read in degrees Celsius and are often displayed in 0.0625°C increments, making it precise for various applications.
7. How to convert Celsius to Fahrenheit?
To convert Celsius to Fahrenheit, use the formula: °F = (°C × 9/5) + 32. This simple calculation allows you to easily transform your DS18B20 temperature readings to Fahrenheit, catering to preferences and requirements in various projects.
8. How to display readings on an OLED?
To display readings on an OLED, connect the OLED to the Raspberry Pi Pico using I2C. Use libraries like SSD1306 in MicroPython to initialize the display. You can then print the temperature readings to the OLED screen, providing a convenient visualization of your data.
9. What are typical applications?
Typical applications of the DS18B20 sensor include home automation systems, weather stations, and aquarium temperature monitoring. It's also used in industrial temperature control systems and DIY electronics projects where accurate temperature data is essential.
10. How accurate is DS18B20?
The DS18B20 sensor offers an accuracy of ±0.5°C within the range of -10°C to +85°C, making it reliable for most applications. Its precision and range of operation make it a popular choice for temperature measurement in various environments.
1. What is a DS18B20 sensor?
The DS18B20 is a digital temperature sensor widely used in electronics and robotics projects. It provides highly accurate temperature readings ranging from -55°C to +125°C with a resolution of 0.5°C. Its 1-Wire interface allows multiple sensors to be connected over a single data line, making it convenient for various applications.
2. How to connect DS18B20 to Raspberry Pi Pico?
To connect the DS18B20 to Raspberry Pi Pico, wire the sensor's VDD pin to a 3.3V power source, GND to ground, and the data pin to a GPIO pin (like GP15). Ensure to add a 4.7kΩ pull-up resistor between the data pin and VDD. This setup allows proper data communication.
3. How to read temperature data in MicroPython?
To read temperature data in MicroPython, use the OneWire library to communicate with the DS18B20. Initialize the library, locate the sensor, and read the data using the sensor's commands. Finally, convert the data to Celsius, and you’ll have real-time temperature readings for your project.
4. What libraries are required?
You will need the OneWire and DS18X20 libraries to interface with the DS18B20 sensor in MicroPython. The OneWire library handles data communication over the 1-Wire protocol, while the DS18X20 library simplifies temperature reading and conversion processes.
5. Can multiple DS18B20 sensors be used?
Yes, you can connect multiple DS18B20 sensors on the same 1-Wire bus. Each sensor has a unique address, allowing you to identify and read data from each one individually. This makes it ideal for complex temperature monitoring applications.
6. What is the output format of the sensor?
The DS18B20 outputs temperature data in a 12-bit digital format, typically represented as a signed integer. Temperature values can be read in degrees Celsius and are often displayed in 0.0625°C increments, making it precise for various applications.
7. How to convert Celsius to Fahrenheit?
To convert Celsius to Fahrenheit, use the formula: °F = (°C × 9/5) + 32. This simple calculation allows you to easily transform your DS18B20 temperature readings to Fahrenheit, catering to preferences and requirements in various projects.
8. How to display readings on an OLED?
To display readings on an OLED, connect the OLED to the Raspberry Pi Pico using I2C. Use libraries like SSD1306 in MicroPython to initialize the display. You can then print the temperature readings to the OLED screen, providing a convenient visualization of your data.
9. What are typical applications?
Typical applications of the DS18B20 sensor include home automation systems, weather stations, and aquarium temperature monitoring. It's also used in industrial temperature control systems and DIY electronics projects where accurate temperature data is essential.
10. How accurate is DS18B20?
The DS18B20 sensor offers an accuracy of ±0.5°C within the range of -10°C to +85°C, making it reliable for most applications. Its precision and range of operation make it a popular choice for temperature measurement in various environments.
