Site icon Renzo Mischianti

Dallas ds18b20 with Raspberry Pi Pico (rp2040): introduction and parasite mode

Spread the love

The DS18B20 is a digital temperature sensor that can be easily integrated with a Raspberry Pi Pico (rp2040) microcontroller. In this response, I will provide an introduction to the DS18B20 sensor and explain how to use it in parasite mode with a Raspberry Pi Pico.

Raspberry Pi Pico (rp2040) and DS18B20: introduction and parasite mode

I already show some temperature and humidity sensors in this series of articles: “Temperature and humidity sensors: how to and comparison“, but now I’d like to explain better Dallas ds28b20. This one-wire temperature sensor can be managed in series without a big limit.

In particular, It has good precision (±0.5°) and a considerable range (-55°C to +125°C or -67°F to +257°F), but you can add in a single wire a lot of devices, It also supports a parasite power mode that allows to you to use only two wire in connection.

But now I’m going to add the explanation from the datasheet for better comprehension.

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, using one microprocessor to control many DS18B20s distributed over a large area is simple. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.

Datasheet

Features

Datasheet

Pinout

Dallas ds18b20 pinout

The sensor has more variants, but the wiring remains the same.

GND: connect to GND.
DATA: One-Wire Data bus.
VCC: power supply (3,3 – 5 V), but in parasite mode, you can connect to GND.

Here the sensor AliExpress

Wiring

The One-Wire library Is a standard for all microcontrollers, so the device can be used with many microcontrollers.

Raspberry Pi Pico

Here is the pinout of Raspberry Pi Pico prototype boards.

Raspberry Pi Pico rp2040 pinout low resolution

Or WeAct variant:

WeAct Studio RP2040 pinout

Here some rp2040 prototype board Official Pi Pico - Official Pi Pico W - Waveshare rp2040-zero - WeAct Studio rp2040

Normal mode

In normal mode, we are going to use a power line at 3.3v, so the connection becomes like so.

Raspberry Pi Pico (rp2040) and ds18b20: wiring

The connection is very simple.

esp32ds18b20
GNDGND
3.3vVCC
D22 pulled-upDATA
Schema

Here is the simple schema.

Raspberry Pi Pico (rp2040) and ds18b20: schema

Parasite mode

In the parasite mode, you are going to put GND also in the VCC line of the sensor, and It gets the power from the Data line.

Raspberry Pi Pico (rp2040) and ds18b20: parasite mode wiring
esp32ds18b20
GNDGND
GNDVCC
D22 pulled-upDATA

Next, we will analyze this modality in-depth, but you don’t need more information now.

Schema

Here is the schema.

Raspberry Pi Pico (rp2040) and ds18b20: parasite mode schema

Library

Now, when I write this article, there is a bug in the most famous library OneWire, so I insert the link to this library, so don’t use It.

Use OneWireNg not the standard OneWire library

Don’t use it until the bug is fixed.

First of all, you must remember that this sensor uses the One-Wire protocol, so you must add the OneWire library.

Arduino IDE: OneWire library on libraries manager

Use this for now

Arduino IDE OneWireNg library on libraries manager

After that, you must install a library that implements the protocol and sensor features.

I chose the most widely used, the DallasTemperature (in the past Dallas, now Maxim).

Arduino IDE: libreria DallasTemperature per il ds18b20 dal libraries manager

Devices supported

As described in the repository, this library supports the following devices :

You will need a pull-up resistor of about 4.7KOhm between the 1-Wire data line and your VCC. if you are using the DS18B20, you can use parasite mode, and you can put to GND the VCC as described.

Troubleshooting

In case of temperature conversion problems (the result is -85), a strong pull-up setup may be necessary. See section Powering the DS18B20 in DS18B20 datasheet (page 7) and use DallasTemperature(OneWire*, uint8_t) constructor.

Powering the DS18B20 (normal/parasite)

The DS18B20 can be powered by an external supply on the VDD pin, or it can operate in “parasite power” mode, which allows the DS18B20 to function without a local external supply (you connect the VDD to the GND). DS18B20 “steals” power from the 1-Wire bus via the DQ pin when the bus is high. The stolen charge powers the DS18B20 while the bus is high, and some of the charges are stored on the parasite power capacitor (CPP) to provide power when the bus is low.

Raspberry Pi Pico (rp2040) and ds18b20: parasite mode schema

But when the DS18B20 performs temperature conversions or copies data from the scratchpad memory to EEPROM, the operating current can be as high as 1.5mA. This current can cause an unacceptable voltage drop across the weak 1-Wire pullup resistor and is more current than CPP can supply.

Strong pull-up for parasite mode

To assure that the DS18B20 has sufficient current supply, it is necessary to provide a strong pullup on the 1-Wire bus whenever temperature conversions occur or data is being copied from the scratchpad to EEPROM.
This can be accomplished by using a MOSFET to pull the bus directly to the rail.

Raspberry Pi Pico (rp2040) and ds18b20: parasite strong pull-up P-MOSFET schema

The 1-Wire bus must be switched to the strong pullup within 10µs (max) after temperature conversions, and the bus must be held high by the pullup for the duration of the conversion or data transfer. No other activity can take place on the 1-Wire bus while the pullup is enabled.

An example of m

The DS18B20 can also be powered by the conventional method of connecting an external power supply to the VDD pin, as shown. The advantage of this method is that the MOSFET pull-up is not required, and the 1-Wire bus is free to carry other traffic during the temperature conversion time.

Raspberry Pi Pico (rp2040) and ds18b20: parasite strong pull-up P-MOSFET wiring

The use of parasite power is not recommended for temperatures above +100°C since the DS18B20 may not be able to sustain communications due to the higher leakage currents that can exist at these temperatures.

Reduce the size of the library

We have included a REQUIRESNEW and REQUIRESALARMS definition. If you want to slim down the code, feel free to use either of these by including

#define REQUIRESNEW 

or

#define REQUIRESALARMS

at the top of DallasTemperature.h

Code

And now, we can start with some examples.

Get sensor temperature

First of all, the fastest way to put in work the sensor.

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port D22
#define ONE_WIRE_BUS D22

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/*
 * The setup function. We only start the sensors here
 */
void setup(void)
{
  // start serial port
  Serial.begin(115200);
  while (!Serial) { delay(100); }

  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

/*
 * Main function, get and show the temperature
 */
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  float tempC = sensors.getTempCByIndex(0);

  // Check if reading was successful
  if(tempC != DEVICE_DISCONNECTED_C)
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
  }
  else
  {
    Serial.println("Error: Could not read temperature data");
  }

  delay(2000);
}

The Serial output is very simple.

Dallas Temperature IC Control Library Demo
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 20.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 20.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 20.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 20.50
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 20.44
Requesting temperatures...DONE
Temperature for the device 1 (index 0) is: 20.44

Get sensor info and temperature (by index)

Raspberry Pi Pico (rp2040) and ds18b20 on breadboard

First of all, we are going to retrieve the address, the connection information (parasite or not), and the temperature.

/**
 * Retrieve information about a single sensor
 * minor change by Renzo Mischianti <www.mischianti.org>
 *
 * www.mischianti.org
 */

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port D22
#define ONE_WIRE_BUS D22

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;

/*
 * Setup function. Here we do the basics
 */
void setup(void)
{
  // start serial port
  Serial.begin(112500);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");

  // Assign address manually. The addresses below will beed to be changed
  // to valid device addresses on your bus. Device address can be retrieved
  // by using either oneWire.search(deviceAddress) or individually via
  // sensors.getAddress(deviceAddress, index)
  // Note that you will need to use your specific address here
  // insideThermometer = { 0x28, 0xFF, 0x64, 0x0E, 0x79, 0x69, 0x3A, 0x1A };
  // we can check if the address is correct and if It's all ok
  // if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");

  // Search for devices on the bus and assign based on an index. Ideally,
  // you would do this to initially discover addresses on the bus and then
  // use those addresses and manually assign them (see above) once you know
  // the devices on your bus (and assuming they don't change).
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  Serial.print("Device 0 HEX Address: ");
  printHEXAddress(insideThermometer);
  Serial.println();

  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC);
  Serial.println();
}

/*
 * Main function. It will request the tempC from the sensors and display on Serial.
 */
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  // It responds almost immediately. Let's print out the data
  printTemperature(insideThermometer); // Use a simple function to print out the data

  delay(4000);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

void printHEXAddress(DeviceAddress deviceAddress)
{
    Serial.print("  { ");
    for (uint8_t i = 0; i < 8; i++)
    {
      Serial.print("0x");
      if (deviceAddress[i] < 0x10) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
      if (i < 7) Serial.print(", ");
    }
    Serial.println(" }");
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(sensors.getTempC(deviceAddress));
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  if(tempC == DEVICE_DISCONNECTED_C)
  {
    Serial.println("Error: Could not read temperature data");
    return;
  }
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}

We receive this when the serial outputs.

Dallas Temperature IC Control Library Demo
Locating devices...Found 1 devices.
Parasite power is: OFF 
Device 0 Address: 28FF640E79693A1A
Device 0 HEX Address:   { 0x28, 0xFF, 0x64, 0x0E, 0x79, 0x69, 0x3A, 0x1A }

Device 0 Resolution: 9
Requesting temperatures...DONE
Temp C: 22.50 Temp F: 72.50
Requesting temperatures...DONE
Temp C: 22.50 Temp F: 72.50
Raspberry Pi Pico (rp2040) and ds18b20 on breadboard

As you can see, I set the OneWire pin to 22:

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 22

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

Check how many sensors we retrieve in the line:

  // locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

Check If we do a parasite connection:

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");

Put a in insideThermometer the address of sensor 0:

  // Search for devices on the bus and assign based on an index. Ideally,
  // you would do this to initially discover addresses on the bus and then
  // use those addresses and manually assign them (see above) once you know
  // the devices on your bus (and assuming they don't change).
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");

Now we can read the temperature (in Celsius):

  float tempC = sensors.getTempC(deviceAddress);

Then convert in Fahrenheit:

  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit

You can do a read of Fahrenheit directly, but inside, do the same operations:

  Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

Thanks

  1. Raspberry Pi Pico and rp2040 boards: pinout, specs, and Arduino IDE configuration
  2. Raspberry Pi Pico and rp2040 boards: integrated LittleFS filesystem
  3. Raspberry Pi Pico and rp2040 board: ethernet w5500 with plain (HTTP) and SSL (HTTPS) requests
  4. Raspberry Pi Pico and rp2040 boards: WiFiNINA with ESP32 WiFi Co-Processor
  5. Raspberry Pi Pico and rp2040 boards: how to use SD card
  6. Dallas ds18b20
  7. Connecting the EByte E70 to Raspberry Pi Pico (rp2040) devices and a simple sketch example

Spread the love
Exit mobile version