Raspberry Pi Pico C++ library for the ds18b20 temperature sensor
Note: Does not support parasite power mode
You first need to determine whether your ds18b20 has a built-in pull-up resistor or not. ds18b20's with built-in pull-up resistors usually have a small component connected across all three wires (ex. Keystudio ds18b20).
|
Without built-in pull-up resistor |
With built-in pull-up resistor |
In case your ds18b20 doesn't have a built-in pull-up resistor, you need to connect one
Connect:
- VDD: to 5V (ex. VBUS pin) or 3.3V (ex. 3V3 pin)
- GND: to ground (any GND pin)
- DQ: to any GPIO pin
For example:
|
Without built-in pull-up resistor |
With built-in pull-up resistor |
- Download the Embedded Temple Library (ETL) and copy the
includefolder to the root of this project - Compile
ninja -C /home/panagiotis/pico_projects/pico-ds18b20/build
- Set the Raspberry Pi Pico in bootloader mode by connecting it through USB while pressing its button
- Load
build/ds18b20.elfto Raspberry Pi Pico
See the examples folder for complete programs
Find all the ds18b20 devices connected to GPIO 0
#include "one_wire.hpp"
#include "ds18b20.hpp"
OneWire one_wire(0);
// Find all devices at pin 0
etl::vector<Ds18b20, 10> devices = Ds18b20::find_devices(one_wire);
// Check if they are successfully initialized
for (int i = 0; i < devices.size(); i++) {
if (!devices[i].is_successfully_initialized()) {
printf("Could not initialize device index %d. Do not use!\n", i);
}
}
// Select device index i
Ds18b20& device = devices[i];
// ...Measure and print the temperature of a device
std::optional<float> result = device.measure_temperature();
if (result.has_value()) {
float temperature = result.value();
printf("%f\n", temperature);
} else {
printf("Could not calculate temperature\n");
}Set resolution
bool success = device.set_resolution(Resolution::VeryHigh, true);
if (!success) {
printf("Could not set resolution\n");
}Set the low/high alarm temperature range values
bool success0 = device.set_temperature_low_limit(-20, true);
bool success1 = device.set_temperature_high_limit(80, true);
if (!success0 || !success1) {
printf("Could not set temperature limits\n");
}Detect temperature alarms after temperature measurements
std::optional<float> result = device.measure_temperature();
if (result.has_value()) {
if (device.is_alarm_active()) {
printf("ALARM!!!\n");
} else {
printf("All safe!\n");
}
}Check if a device is operational
// ...
bool is_operational = device.ping();
if (is_operational) {
printf("Fully operational!\n");
} else {
printf("Device not operating correctly\n");
}- Measure temperature
- ±0.5°C from -10°C to +85°C
- ±1°C from -30°C to +100°C
- ±2°C from -55°C to +125°C
- Set resolution (temperature discrete step size)
- Low: 0.0625°C steps
- Medium: 0.125°C steps
- High: 0.25°C steps
- Very High: 0.5°C steps
- Detect alarm when temperature goes out of bounds
- Set the low and high bounds of the temperature alarm range
- The range is [-128, 127] as integers
- Check if a device is operational
- Fetch the power mode of the device (external or parasite)



