This guide shows how to use the DS18B20 temperature sensor with the Arduino board. You’ll learn how to wire the sensor, install the required libraries and get temperature from one or multiple DS18B20 sensors.
You might also like reading other DS18B20 guides:
- ESP32 DS18B20 Temperature Sensor with Arduino IDE
- ESP8266 DS18B20 Temperature Sensor with Arduino IDE
- ESP32/ESP8266 DS18B20 Temperature Sensor with MicroPython
- ESP32 with Multiple DS18B20 Temperature Sensors
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 the Arduino.
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.
The following table shows how you should wire the DS18B20 sensor to your Arduino board:
DS18B20 | Arduino |
GND | GND |
DQ | Any digital pin (with 4.7k Ohm pull-up resistor) |
VDD | 5V (normal mode) or GND (parasite mode) |
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 Arduino digital pin.
The DS18B20 temperature sensor is also available in 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 show you how the sensor works, we’ll build a simple example that reads the temperature from the DS18B20 sensor with the Arduino and displays the values on the Arduino Serial Monitor.
Here’s a list of parts you need to complete this tutorial
- Arduino UNO â read Best Arduino Starter Kits
- DS18B20 temperature sensor (one or multiple sensors) â waterproof version
- 4.7k Ohm resistor
- Breadboard
- Jumper wires
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!
Schematic
The sensor can operate in two modes:
- Normal mode: 3-wire connection is needed. You provide power to the VDD pin. Hereâs the schematic you need to follow:
- Parasite mode: You only need data and GND. The sensor derives its power from the data line. In this case, hereâs the schematic you need to follow:
You can read the temperature of more than one sensor at the same time using just one Arduino digital pin. For that, you just need to wire together all the sensors data pins to an Arduino digital pin.
Upload Code – Single DS18B20
To interface with the DS18B20 temperature sensor, you need to install the One Wire library by Paul Stoffregen and the Dallas Temperature library. Follow the next steps to install those libraries.
Installing Libraries
1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
2. Type âOneWireâ in the search box and install the OneWire library by Paul Stoffregen.
3. Then, search for “Dallas” and install the Dallas Temperature library by Miles Burton.
After installing the needed libraries, upload the following code to your Arduino board. This sketch is based on an example from the Dallas Temperature library.
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
Based on the Dallas Temperature Library example
*********/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Start serial communication for debugging purposes
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop(void){
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
delay(1000);
}
There are many different ways to get the temperature from DS18B20 temperature sensors. If you’re using just one single sensor, this is one of the easiest and simplest ways.
How the Code Works
Start by including the OneWire and the DallasTemperature libraries.
#include <OneWire.h>
#include <DallasTemperature.h>
Create the instances needed for the temperature sensor. The temperature sensor is connected to Pin 4.
// Data wire is conntec to the Arduino digital pin 4
const int oneWireBus = 4;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
In the setup(), initialize the Serial Monitor at a baud rate of 9600.
Serial.begin(9600);
Initialize the DS18B20 temperature sensor:
sensors.begin();
In the loop() is where you’ll get the temperature. You need to call the requestTemperatures() method before getting the actual temperature value.
sensors.requestTemperatures();
Then, get and print the temperature in Celsius. To get the temperature in Celsius, use the getTempCByIndex() method :
Serial.print(sensors.getTempCByIndex(0));
Or use the getTempFByIndex() to get the temperature in Fahrenheit.
Serial.println(sensors.getTempFByIndex(0));
The getTempCByIndex() and the getTempFByIndex() methods accept the index of the temperature sensor. Because we’re using just one sensor its index is 0. If you have more than one sensor, you use index 0 for the first sensor, index 1 for the second sensor, and so on.
New temperature readings are requested every second.
delay(5000);
Demonstration
After uploading the code, open the Arduino IDE Serial Monitor at a 9600 baud rate. You should get the temperature displayed in both Celsius and Fahrenheit:
Getting Temperature from Multiple DS18B20 Sensors
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 Arduino digital Pin.
Schematic
To read the temperature from multiple sensors, you just need to wire all data lines together as shown in the following schematic diagram:
Upload Code – Multiple DS18B20
Then, upload the following code. It scans for all devices on Pin 4 and prints the temperature for each one. This sketch is based on the example provided by the DallasTemperature library.
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// 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);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup(void) {
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void loop(void) {
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
}
delay(5000);
}
// 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);
}
}
How the code works
The code uses several useful methods to handle multiple DS18B20 sensors.
You use the getDeviceCount() method to get the number of DS18B20 sensors on the data line.
numberOfDevices = sensors.getDeviceCount();
The getAddress() method finds the sensors addresses:
if(sensors.getAddress(tempDeviceAddress, i)){
The address is unique for each sensor. So each sensor can be identified by its address.
Then, you use the getTempC() method that accepts as argument the device address. With this method you can get the temperature from a specific sensor:
float tempC = sensors.getTempC(tempDeviceAddress);
To get the temperature in Fahrenheit degrees, you can use the getTemF(). Alternatively, you can convert the temperature in Celsius to Fahrenheit as follows:
DallasTemperature::toFahrenheit(tempC)
Wrapping Up
The DS18B20 temperature sensor is a one-wire digital sensor. To use this sensor with the Arduino, you need the OneWire and the DallasTemperature libraries. You can use one sensor or multiple sensors on the same data line because you can identify each sensor by its unique address.
Now, you can take this project further and display your sensor readings in an OLED display, for example.
We have more tutorials for other Arduino compatible sensors that you may find useful:
- DHT11/DHT22 Humidity and Temperature Sensor With Arduino
- BME280 (Temperature, Humidity, Pressure) with Arduino
- Relay Module with Arduino
- Ultrasonic Sensor HC-SR04 with Arduino
We hope you’ve found this guide useful.
If you want to learn more about Arduino, take a look at our resources:
Thanks for reading!
Updated July 2, 2019
Hello J uses your tutorial . J have errors at compile on dallastemperature.h Can you help me by giving me the dallastemperature library works
Best regards
Pierre
What’s the error?
hello,
error is
error: WConstants.h: No such file or directory
why
do you speak and write french
best regards
dowload the onewire and dallas temperature from the libaray…
Hi Rui,
Thanks for your tutorial.
I need to compare two temperature sensors, lets say T1 and T2 and control a relay when T1 e higher than T2.
How can i achieve this?
Kind regards,
Rodrigo Catarino
Hi Rodrigo. You need an if statement that compares both temperatures and turns the relay on when T1 > T2.
Controlling a relay is the same thing as controlling an LED but with inverted logic.
digitalWrite(relay, HIGH); –> turns the relay off
digitalWrite(relay, LOW); –> turns the relay on
You may want to take a look at this tutorial to see how to wire the relay to the Arduino: https://randomnerdtutorials.com/guide-for-relay-module-with-arduino/
I hope this helps.
Regards,
Sara đ
hello sir,
i want to control the fan according to the temperature variation.
Hi.
We don’t have any tutorial about that specific subject.
If your fan is controlled using a relay, you can follow our relay tutorial that might help: https://randomnerdtutorials.com/guide-for-relay-module-with-arduino/
Regards,
Sara
Hello,
I’m working on a project using an Arduino Zero that as an Operating Voltage at 3.3 V. Do you think it may varied the pull-up resistor value?
Thanks you !
Hi.
I think you can use the same resistor and it will work fine.
Regards,
Sara
EXCELENTE POSTAGEM, PARABĂNS.
Thank you đ
Thank you so much, was almost giving up on this one.
As of 19/12/2019 this code works
thanks.
Great Tutorial ! Thanks a lot.
DS18B20 have four resolutions of reading (9,10,11 and 12 bits).
You may change the resolution of DS18B20 with this command.
https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/master/examples/Single/Single.pde
example (12bits resolution):
sensors.setResolution(insideThermometer, 12);
Regards.
While compiling your temperature sensor sketch (https://randomnerdtutorials.com/guide-for-ds18b20-temperature-sensor-with-arduino/)
I get this error message:
Sketch uses 5924 bytes (18%) of program storage space. Maximum is 32256 bytes.
Global variables use 290 bytes (14%) of dynamic memory, leaving 1758 bytes for local variables. Maximum is 2048 bytes.
Hi Alan.
That’s a normal message when you compile the code.
Regards,
Sara
Ok, but it won’t download to the arduino because of that.
where is zip or header file of ds18b20
plz me link of that header file or zip file
You need this two libraries: https://github.com/PaulStoffregen/OneWire
https://github.com/milesburton/Arduino-Temperature-Control-Library
Regards,
Sara
Hi, I’m getting the values like -127 C and -196.60 F. what will be the issue? can you please help me?
Hi.
There’s probably something wrong with your circuit wiring.
Regards,
Sara
i have the same problem, if you have found a fix please share. i have tried couple of temperature sensors. but the temp. is still -127. and the circuit wiering is ok.
Yes, I have solved this problem.
Make sure that the power of 5 v, 300mA reaches well to ds18b20. and use a small wire-like Telephone-cable to 5 miter
Evidently -127 is an error code produced when the sensor is disconnected. However, I do not think that this is ‘strictly’ true because you get no data from a disconnected sensor. I had this problem when using the 5V output from my homemade PSU. The PSU contains a 12V switched mode power supply (to drive motors, fans etc.) and a cheap DC to DC buck converter to produce 5V. I switched the 5V sensor supply from my PSU to the 5V output from my UNO and have not had a -127 reading since. I assume the buck converter is either putting noise on the sensor power line or (occasionally) momentarily disconnecting the sensor during data transmission.
Also check that you’re using the right component. I just discovered I mixed up my ds18b20 with my 62ab lm35dz because they look identical
Dear Sara, thank you for the program, it works great!
I was wondering if there is a way to increase the speed of the functions.
Either requestTemperatures or getTempCbyIndex works rather slowly and i’d prefer something quicker.
Cheers,
Hi.
To be honest, I don’t know.
Take a look at some of the library examples and see if they have something that would help with your project.
Here are the library examples: https://github.com/milesburton/Arduino-Temperature-Control-Library/tree/master/examples
Regards,
Sara
Hi Wytse,
According to the DS18B20 datasheet (see pages 3 and 9), the sampling frequencies depend on the signal acquisition resolution:
https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
Unless I missed something the Normal and the Parasite mode schematics are identical.
Hi.
Look closely.
They are different.
In Normal mode you provide power to the VDD pin.
In parasite mode the sensor derives its power from the data line.
Regards,
Sara
Ah… they have the same wires running from the bread board to the arduino. BUt are wired differently on the breadboard.
That’s right!
hi
have tried 3xDS18B20 i works but i get 3 different temperaturens with about a 4 degrees one each 16, 24,20 degrees i dont understand why that is ???
pls post the code for three ds18b20 sensor
im facing some issue
Hi, I’m an engineering student and was hoping to use this code to collect temperature averages which can trigger a relay command, I tried changing the if loop to give each sensor reading its own individual tempC1, tempC2, tempC3, which will then be added into an average. Could someone help me with me please?
Need to see your code.
Hi, I managed to get it working, I had failed to add the needed } at the end and caused a failure to verify. I have posted my code below, I will now try and find a way to take the average of each Temp(0-2) C entries and if that average is above a specified temp, activate a relay.
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// 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);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We’ll use this variable to store a found device address
void setup(void) {
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print(“Locating devices…”);
Serial.print(“Found “);
Serial.print(numberOfDevices, DEC);
Serial.println(” devices.”);
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print(“Found device “);
Serial.print(i, DEC);
Serial.print(” with address: “);
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print(“Found ghost device at “);
Serial.print(i, DEC);
Serial.print(” but could not detect address. Check power and cabling”);
}
}
}
void loop(void) {
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i=0)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC0 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp0 C: ");
Serial.print(tempC0);
Serial.print(" Temp0 F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC0)); // Converts tempC to Fahrenheit
}
if(sensors.getAddress(tempDeviceAddress, i=1)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC1 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp1 C: ");
Serial.print(tempC1);
Serial.print(" Temp1 F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC1)); // Converts tempC to Fahrenheit
}
if(sensors.getAddress(tempDeviceAddress, i=2)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC2 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp2 C: ");
Serial.print(tempC2);
Serial.print(" Temp2 F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit
}
}
delay(5000);
}
// 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);
}
}
I suggest you read up on indenting your code, it will make it easier to find those kinds of errors. You should have received an error from the compiler and no code would have been generated. There is also no need for the uint8_t, int will work.
Here is an example:
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (int i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16)
Serial.print(â0â);
Serial.print(deviceAddress[i], HEX);
}
}
Thanks for the advice, i hadnt messed with that part. That uint8_t is from the articles code. Ill make the suggested change, whats the difference between int and uint8_t?
int is a signed value and its size (number of bits) depends on the CPU. Usually 16, 32 or 64.
uint8_t is unsigned (cannot have a negative value) and is always 8 bits, so the largest value it can represent is 2 ** 8 – 1. You only need to use it when you really care about the size and sign.
I’ve tried creating the average by using float as shown on the code below, but im getting a message saying tempC0, tempC1, tempC2 are not declared, how can i keep the variables outside of their if statements?
if(sensors.getAddress(tempDeviceAddress, i=2)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC2 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp2 C: ");
Serial.print(tempC2);
Serial.print(" Temp2 F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit
}
float x = tempC0+tempC1+tempC2;
}
delay(5000);
}
The post lost the indents I added! LOL
// Put this above all refs to the tempCns
float tempC1, tempC2, tempC3;
if(sensors.getAddress(tempDeviceAddress, i=2)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
// Remove the floats here
tempC2 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp2 C: ");
Serial.print(tempC2);
Serial.print(" Temp2 F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit
}
float x = tempC0+tempC1+tempC2;
}
delay(5000);
}
thank you for your help, ive tried using the float before the if statements but it returns x as 0 because the tempCns are 0 values out side the if statements, the full for statement is below. The if statements are not “sharing” their variables with the for statements.
void loop(void) {
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++) {
float tempC0, tempC1, tempC2;
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i=0)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC0 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp0 C: ");
Serial.println(tempC0);
}
if(sensors.getAddress(tempDeviceAddress, i=1)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC1 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp1 C: ");
Serial.println(tempC1);
}
if(sensors.getAddress(tempDeviceAddress, i=2)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
float tempC2 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp2 C: ");
Serial.println(tempC2);
}
float x = tempC0+tempC1+tempC2;
Serial.println(x);
}
delay(5000);
}
is it possible to use dallastemperature without the if?
You missed one change and I left out one detail:
float tempC1, tempC2, tempC3 = 0.0;
if(sensors.getAddress(tempDeviceAddress, i=2)){
// Output the device ID
Serial.print(“Temperature for device: “);
Serial.println(i,DEC);
// Print the data
// Remove the floats here
/* no float here */ tempC2 = sensors.getTempC(tempDeviceAddress);
Serial.print(“Temp2 C: “);
Serial.print(tempC2);
Serial.print(” Temp2 F: “);
Serial.println(DallasTemperature::toFahrenheit(tempC2)); // Converts tempC to Fahrenheit
}
Thanks for all the help, got the code working with the relay and OLED screen, ive added my code below for anyone who wants it in the future.
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
* Modified by Hector Isava so it averages 3 thermistors
* triggers a relay if above a set temperature
* and displays each temperature on an OLED screen
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
#define OLED_RESET 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define NUM_PAGE 8
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 0
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(OLED_RESET);
// 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);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We’ll use this variable to store a found device address
// Add relay pin on arduino
int RELAY_PIN = 3;
void setup(void) {
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print(“Locating devices…”);
Serial.print(“Found “);
Serial.print(numberOfDevices, DEC);
Serial.println(” devices.”);
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print(“Found device “);
Serial.print(i, DEC);
Serial.print(” with address: “);
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print(“Found ghost device at “);
Serial.print(i, DEC);
Serial.print(” but could not detect address. Check power and cabling”);
}
}
// Set relay as digital output
pinMode(RELAY_PIN, OUTPUT);
// Initializing OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(“License to Chill”);
display.display();
delay(2000);
}
void loop(void) {
sensors.requestTemperatures(); // Send the command to get temperatures
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++) {
float tempC0, tempC1, tempC2 = 0.0;
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i=0)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
tempC0 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp0 C: ");
Serial.println(tempC0);
}
if(sensors.getAddress(tempDeviceAddress, i=1)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
tempC1 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp1 C: ");
Serial.println(tempC1);
}
if(sensors.getAddress(tempDeviceAddress, i=2)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// Print the data
tempC2 = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp2 C: ");
Serial.println(tempC2);
}
float x = (tempC0+tempC1+tempC2)/3;
Serial.print("Average Temperature: ");
Serial.println(x);
Serial.println();
if(x > 22){ // change number for desired refrigeration temperature
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
//clear display
display.clearDisplay();
// display average temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("Average Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(x);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.display();
delay(2000);
// display temperature from thermistor 0
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature 0: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(tempC0);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.display();
delay(2000);
// display temperature from thermistor 1
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature 1: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(tempC1);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.display();
delay(2000);
// display temperature from thermistor 2
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature 2: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(tempC2);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.display();
delay(2000);
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (int i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print(“0”);
Serial.print(deviceAddress[i], HEX);
}
}
hi
got the 3xDS10B20 to work but i get 3 different readings 16,24,20 degrees (all in same room) is that right
Hello every one. please help me to connect 7 DS18B20 sensors to arduino uno. It detects until 6th sensor but after 7th sensor it can not detect any of them. is there any limits for number of these sensors?
Hi im ussing 4 sensor everything work , but : many pin that i use are making glitch, i am using only pin 2 on the nano for sensor, and i found this problem with the servo command
i am unising :
#include <Servo.h>
Servo myservo;// define servo variable name
and the servo mouve glitch, (on all pmw of the nano) every loop when call: sensors.requestTemperatures();
i try all pmw pin they are all impacted by sensors.requestTemperatures();
whats can be the probleme sect up ?
Hi.
I don’t know how your code or setup is structured. So, it is not easy to guess what might be wrong.
It may be an issue related to the power supply.
Or the code hangs waiting for temperatures to be requested before proceeding to what it was doing before (running the servo motor).
I recommend you post your issue in the Arduino forum. I think you’ll get better help there regarding this issue: https://forum.arduino.cc/
Regards,
Sara
Hi
I have 12 sensors ,I tend to use 8 sensors on one pin and the others on the other pin and I want to find their temperature by their address I could find their temperatures when they were pluged just on one pin, but now I am not able to do this actuallyÙ have problems in writting a good code for arduino
Can you help me about this issue?
Hi Sara.
I’m watching your site. I did experiments with Arduino and DS1820. It all works on the table. I want to measure a temperature about 5 m away from Arduino. Where is it better to put a pull-up resistor? On the Arduino board or on the DS 1820 pins? Thank you for your response.
Ivan
“Parasite mode: You only need data and GND. The sensor derives its power from the data line. In this case, hereâs the schematic you need to follow:” The drawing is not correct because it has three lines.
Hi,
I am a bit new on this temperature code with several sensors.
May you please confirm the following;
if the project works perfect as coded, & suddenly the power cuts completely & returned back after awhile (say an hour), will the project run again automatically without reprograming or restarting manually ??
Is there a tutorial to add some connectivity to this like wi-fi or wwan for sending this temp data to a cloud server?
-Vishal
When adding more thermal probes got this error message:
I switched the positive and negative wires and it works. I have the 1M cabled version of the DS18B20 and had to hook positive to the red, and negative to the black leads. It’s also really sensitive to loose hookup wires. I will hardwire this with 22AWG solid.
I love your website – so well done. Thanks again Sarah and Rui!
WOOOOO HOUI !
adding multiple thermal sensors – still have problems with getting number of devices.
hard coding solved it for the short term.
// Grab a count of devices on the wire
numberOfDevices = 2;
// sensors.getDeviceCount();
again – swapped wires with red to +5v; black to gnd.
Firstly, why does the single-sensor version use
getTempCByIndex
, but the multi-sensor version suddenly decides to use device addresses instead? Wouldn’t it be simpler and more consistent to use “by index” method in both versions of the code?Secondly, how do I know which address/which index corresponds to which sensor? A temperature-measuring application with multiple sensors simply does not make sense unless you can tie program handles to specific sensors.
You’ve answered your own question. They use the device addresses so you can tell which sensor is which.
No, I haven’t.
One more time: in order to meaningfully communicate with multiple sensors by their addresses (or by their indices, for that matter), one has to figure out which physical sensor corresponds to which address (or index). The article above does not explain how to do that.
im only getting one temperature 196.60f can u tell me why pls
I had the same problem. I changed from parasitic power to actually using the sensor’s 5V and GND separately and it worked perfectly. Otherwise it should be a connection problem or the sensor is faulty. A slightly stronger pull-up may help if there is noise on the board.
Great guide for using DS temperature sensor. I have no coding experience but I now have breadboarded 5 sensors displaying on a 2 line LCD display.
Question… Is there a minimum gauge wire to use between an esp32 and the DS? I am using bare sensors and will place them through the insulation onto the copper cylinder.
My application is to tell the top, middle, and bottom temperatures of my insulated hot water cylinder. We will be able to see how much hot water is available – I hope it will reduce our energy use.