Raspberry Pi Pico: DS18B20 Temperature Sensor (Arduino IDE) – Single and Multiple

In this guide, you’ll learn how to interface the Raspberry Pi Pico with the DS18B20 temperature sensor and how to get temperature readings using Arduino IDE. The DS18B20 is an addressable temperature sensor, which means you can connect multiple sensors to the same GPIO. We’ll also show you how to get temperature from multiple DS18B20 sensors all at once.

Raspberry Pi Pico DS18B20 Temperature Sensor Arduino IDE Single and Multiple

We have a similar guide but using MicroPython firmware: Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) – Single and Multiple.

Table of Contents

Prerequisites

You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven’t already:

Introducing DS18B20 Temperature Sensor

The DS18B20 temperature sensor is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with your Raspberry Pi Pico.

It can be powered by an external power supply or it can derive power from the data line (called “parasite mode”), which eliminates the need for an external power supply.

DS18B20 Temperature Sensor Pinout Pins

Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one GPIO.

The DS18B20 temperature sensor is also available in waterproof version.

DS18B20 Temperature Sensor Waterproof version

Here’s a summary of the most relevant specs of the DS18B20 temperature sensor:

  • Communicates over one-wire bus communication
  • Power supply range: 3.0V to 5.5V
  • Operating temperature range: -55ºC to +125ºC
  • Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)

For more information consult the DS18B20 datasheet.

Parts Required

To follow this tutorial you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring the DS18B20 Temperature Sensor to the Raspberry Pi Pico

The DS18B20 temperature sensor comes with three pins: GND, data, and VCC. the DS18B20 temperature sensor can be powered through the VDD pin (normal mode), or it can derive its power from the data line (parasite mode). You can choose either mode. We prefer using the normal mode.

DS18B20 Temperature Sensor Pinout Pins

If you’re using the waterproof version, you can identify each pin by its color:

  • black: GND
  • red: VDD
  • yellow: data line
DS18B20Raspberry Pi Pico
GNDGND
Data (DQ)Any digital GPIO (we’ll be using GPIO22);
you also need to connect a 4.7KOhm resistor between the data line and VCC
VDD3V3(OUT)
You can also take a look at the following schematic diagram:
Raspberry Pi Pico DS18B20 Single Wiring

Installing the microDS18B20 Library

To read the temperature from the DS18B20 on the Raspberry Pi Pico using Arduino IDE, we’ll use the microSD18B20 library. Follow the next steps to install the library.

  1. Open your Arduino IDE;
  2. Go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
  3. Search for microSD18B20 and install the library.
microds18b20 arduino ide install library

After installing the library, restart your Arduino IDE.

Raspberry Pi Pico with DS18B20 – Get Temperature (Arduino Code)

Open your Arduino IDE and copy the following code. It simply gets the temperature from the DS18B20 sensors and prints it in the Serial Monitor. This code is adapted from the microDS18B20 library’s examples.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-ds18b20-arduino/
  
  Example adapted from the microDS18B20 library examples folder - microSD18B20 library: https://github.com/GyverLibs/microDS18B20
*********/

#include <microDS18B20.h>

// We're using GPIO 22, change accordingly
MicroDS18B20<22> sensor;

void setup() {
  Serial.begin(115200);
}

void loop() {
  //request temperature from sensor
  sensor.requestTemp();

  delay(1000);
  
  //print the temperature
  Serial.print("Temperature (ºC): ");
  Serial.println(sensor.getTemp());

  //Temperature in Fahrenheit
  //Serial.print("Temperature (ºF): ");
  //Serial.println(sensor.getTemp()*(9.0/5.0) + 32.0);
}

View raw code

How does the code work?

Continue reading to learn how the code works, or skip to the Demonstration section.

Start by including the microDS18B20 library, which provides the necessary functions to work with the DS18B20 temperature sensor.

#include <microDS18B20.h>

The following line declares an instance of the MicroDS18B20 class, called sensor that refers to the temperature sensor connected to GPIO 22. If your DS18B20 sensor is connected to a different pin, modify the line of code accordingly.

MicroDS18B20<22> sensor;

In the setup(), initialize a serial communication at a baud rate of 115200, so that you’re able to display the readings in the Serial Monitor later on.

void setup() {
  Serial.begin(115200);
}

In the loop() function is where we get the temperature from the sensor. The following line sensor.requestTemp() is called to request the temperature reading from the DS18B20 sensor.

sensor.requestTemp();

A delay of 1000 milliseconds is used to give the sensor enough time to measure and provide the temperature reading.

delay(100);

The temperature reading in Celsius degrees is obtained by calling sensor.getTemp(). The temperature value is then printed to the serial monitor.

Serial.println(sensor.getTemp());

If you want to print the temperature in Fahrenheit, uncomment the following lines (remove the // at the beginning of those lines):

//Temperature in Fahrenheit
//Serial.print("Temperature (ºF): ");
//Serial.println(sensor.getTemp()*(9.0/5.0) + 32.0);

Uploading the Code to the Raspberry Pi Pico

For you to be able to upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.

If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.

Raspberry Pi Pico Bootloader mode

For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.

Now, select your COM port in Tools > Port. It may be the case that the COM port is grayed out. If that’s the case, don’t worry it will automatically find the port once you hit the upload button.

Raspberry Pi Pico - COM port not found Arduino IDE

Upload the code.

Arduino 2.0 Upload Button

You should get a success message.

Done uploading Arduino IDE Raspberry Pi Pico

Demonstration

Open the Serial Monitor at a baud rate of 115200. You should get new temperature readings every second.

Raspberry Pi Pico Arduino IDE - Get Temperature from DS18B20 Temperature Sensor

Raspberry Pi Pico with Multiple Temperature Sensors (Arduino IDE)

The DS18B20 temperature sensor communicates using one-wire protocol and each sensor has a unique 64-bit serial code, so you can read the temperature from multiple sensors using just one single GPIO.

Raspberry Pi Pico with Multiple Temperature Sensors (Arduino IDE)

Getting the DS18B20 Sensor Address

Each DS18B20 temperature sensor has a unique serial number. First, you need to find that number to label each sensor accordingly. You need to do this, so that later you know from which sensor you’re reading the temperature. Upload the following code:

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-ds18b20-arduino/
  
  Example from the library microSD18B20.h: https://github.com/GyverLibs/microDS18B20/tree/main/examples
  Get the address of DS18B20 sensor (one sensor connected at a time)
*********/

#include <microDS18B20.h>

// We're using GPIO 22, change accordingly
MicroDS18B20 <22> sensor;  

// Array to store the sensor address
uint8_t address[8];       

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (sensor.readAddress(address)) {
    Serial.print('{');
    for (uint8_t i = 0; i < 8; i++) {
      Serial.print("0x");
      Serial.print(address[i], HEX); 
      if (i < 7) Serial.print(", ");
    }
    Serial.println('}');

  } else Serial.println("Not connected");
  delay(1000);
}

View raw code

Wire just one sensor at a time to the Raspberry Pi Pico on GPIO22 to find its address. The address of each sensor will be printed in the Serial Monitor.

DS18B20 Get Sensor Address Arduino IDE Raspberry Pi Pico

Wiring Multiple Temperature Sensors to the Raspberry Pi Pico

To read temperature from multiple sensors on the same one-wire bus, you just need to wire all data lines together as shown in the following schematic diagram (as an example we’re using four sensors, but you can use two, three, or more) just make sure you have a 4.7k Ohm resistor on the data line:

Raspberry Pi Pico Wiring Multiple DS18B20 Sensors

Getting Temperature from Multiple DS18B20 Sensors

multiple DS18B20 temperature sensors

The following code reads the temperature from multiple DS18B20 temperature sensors. To define each sensor, you need to modify the code with the sensors’ addresses you found in the previous step.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-ds18b20-arduino/
*********/

#include <microDS18B20.h>

#define SENS_AMOUNT 4

// Add the address of each sensor
const uint8_t addr[][8] PROGMEM = {
  {0x28, 0xFF, 0xA0, 0x11, 0x33, 0x17, 0x3, 0x96},
  {0x28, 0xFF, 0xB4, 0x6, 0x33, 0x17, 0x3, 0x4B},
  {0x28, 0xAE, 0x26, 0x45, 0x92, 0x1, 0x2, 0x4B},
  {0x28, 0xFF, 0x11, 0x15, 0x33, 0x17, 0x3, 0x1A}
};

// All sensors are connected to GPIO22, adjust accordingly
MicroDS18B20<22, DS_ADDR_MODE, SENS_AMOUNT, DS_PROGMEM> sensors;

float sensor1TempC, sensor2TempC, sensor3TempC, sensor4TempC;
float sensor1TempF, sensor2TempF, sensor3TempF, sensor4TempF;

void setup() {
  Serial.begin(115200);
  
  // set the addresses
  sensors.setAddress((uint8_t*)addr);
}

void loop() {
  delay (1000);

  // Get temperature from each sensor
  sensor1TempC =  sensors.getTemp(0);
  sensor2TempC =  sensors.getTemp(1);
  sensor3TempC =  sensors.getTemp(2);
  sensor4TempC =  sensors.getTemp(3);

  //Convert temperatures to Fahrenheit
  sensor1TempF = sensor1TempC *(9.0/5.0) + 32.0;
  sensor2TempF = sensor2TempC *(9.0/5.0) + 32.0;
  sensor3TempF = sensor3TempC *(9.0/5.0) + 32.0;
  sensor4TempF = sensor4TempC *(9.0/5.0) + 32.0;
  
  Serial.print("Sensor 1 (temp ºC): "); 
  Serial.print(sensor1TempC);
  Serial.print(" Sensor 1 (temp ºF): "); 
  Serial.println(sensor1TempF);

  Serial.print("Sensor 2 (temp ºC): "); 
  Serial.print(sensor2TempC);
  Serial.print(" Sensor 2 (temp ºF): "); 
  Serial.println(sensor2TempF);

  Serial.print("Sensor 3 (temp ºC): "); 
  Serial.print(sensor3TempC);
  Serial.print(" Sensor 3 (temp ºF): "); 
  Serial.println(sensor3TempF);

  Serial.print("Sensor 4 (temp ºC): "); 
  Serial.print(sensor4TempC);
  Serial.print(" Sensor 4 (temp ºF): "); 
  Serial.println(sensor4TempF);

  Serial.print("");
  Serial.println("-----------------------------------");

  // request new temperature from all sensors
  sensors.requestTempAll();
}

View raw code

Let’s take a quick look at how the code works.

Start by including the microDS18B20 library to be able to read from the DS18B20 temperature sensors.

#include <microDS18B20.h>

The code defines a constant variable SENS_AMOUNT with a value of 4, indicating the number of sensors connected. If you’re using a different number of sensors, change accordingly.

#define SENS_AMOUNT 4

The addr array holds the address of each sensor. The addresses are specified in hexadecimal format. Modify the code with the addresses you got from the previous step.

// Add the address of each sensor
const uint8_t addr[][8] PROGMEM = {
  {0x28, 0xFF, 0xA0, 0x11, 0x33, 0x17, 0x3, 0x96},
  {0x28, 0xFF, 0xB4, 0x6, 0x33, 0x17, 0x3, 0x4B},
  {0x28, 0xAE, 0x26, 0x45, 0x92, 0x1, 0x2, 0x4B},
  {0x28, 0xFF, 0x11, 0x15, 0x33, 0x17, 0x3, 0x1A}
};

Create an instance of the MicroDS18B20 class called sensors, specifying the GPIO to which all the sensors are connected, the address mode, the number of sensors, and the memory type for storing the addresses. We’re connecting the sensors to GPIO22, if you’re using a different GPIO, modify the code accordingly.

// All sensors are connected to GPIO22, adjust accordingly
MicroDS18B20<22, DS_ADDR_MODE, SENS_AMOUNT, DS_PROGMEM> sensors;

We create variables to hold the temperature from the different sensors both in Celsius and Fahrenheit degrees. Adjust the number of variables accordingly to the number of sensors you’re using.

float sensor1TempC, sensor2TempC, sensor3TempC, sensor4TempC;
float sensor1TempF, sensor2TempF, sensor3TempF, sensor4TempF;

In the setup() set the addresses of the sensors using the setAddress() method on the sensors object.

sensors.setAddress((uint8_t*)addr);

In the loop(), we’ll get the temperature readings from each sensor. The temperature values are obtained from each sensor using the getTemp() method on the sensors object. The argument refers to the address of the sensor (0 corresponds to the first address of the array, 1 corresponds to the second address, and so on).

// Get temperature from each sensor
sensor1TempC = sensors.getTemp(0);
sensor2TempC = sensors.getTemp(1);
sensor3TempC = sensors.getTemp(2);
sensor4TempC = sensors.getTemp(3);

We also convert the temperature to Fahrenheit.

sensor1TempF = sensor1TempC *(9.0/5.0) + 32.0;
sensor2TempF = sensor2TempC *(9.0/5.0) + 32.0;
sensor3TempF = sensor3TempC *(9.0/5.0) + 32.0;
sensor4TempF = sensor4TempC *(9.0/5.0) + 32.0;

Finally, all readings are printed on the serial monitor.

Serial.print("Sensor 1 (temp ºC): "); 
Serial.print(sensor1TempC);
Serial.print(" Sensor 1 (temp ºF): "); 
Serial.println(sensor1TempF);

Serial.print("Sensor 2 (temp ºC): "); 
Serial.print(sensor2TempC);
Serial.print(" Sensor 2 (temp ºF): "); 
Serial.println(sensor2TempF);

Serial.print("Sensor 3 (temp ºC): "); 
Serial.print(sensor3TempC);
Serial.print(" Sensor 3 (temp ºF): "); 
Serial.println(sensor3TempF);

Serial.print("Sensor 4 (temp ºC): "); 
Serial.print(sensor4TempC);
Serial.print(" Sensor 4 (temp ºF): "); 
Serial.println(sensor4TempF);

In the end, don’t forget to call the requestTempAll() method to request new temperature readings from all the sensors.

sensors.requestTempAll();

The code repeats this process indefinitely in the loop() function, continuously reading the temperatures from the sensors and printing the values to the serial monitor every one second.

Demonstration

After uploading the code to your Raspberry Pi Pico, open the Serial Monitor at a baud rate of 115200. You’ll get the temperature readings from all sensors both in Celsius and Fahrenheit degrees.

Raspberry Pi Pico with Multiple DS18B20 Temperature Sensors - Arduino IDE Serial Monitor

Wrapping Up

In this tutorial, you learned how to interface the DS18B20 temperature sensor with the Raspberry Pi Pico and how to get temperature readings using Arduino IDE. The DS18B20 is an addressable temperature sensor. This means you can wire multiple sensors on the same GPIO and get temperature readings from all sensors all at once. We also covered that in our tutorial.

If you like the Raspberry Pi Pico, make sure you take a look at some of the other tutorials on our blog:

Check out all our Raspberry Pi Pico Guides »

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

3 thoughts on “Raspberry Pi Pico: DS18B20 Temperature Sensor (Arduino IDE) – Single and Multiple”

  1. Trying out this project but unable to find MicroDS18B20 library. I am using Arduino IDE 2.3.2.
    Any help would be appreciated.
    Thank you.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.