My full sketch is as follows:
/*
* FILE: Multi_DS18B20_Temperatures_OneWireBus_102
* AUTHOR: RPvN
* VERSION: 1.04
* DATE: 22 September 2021 (initial version)
* HISTORY: 25 September 2021 Update (V1.01)
* 28 September 2021 Resolution settings added (V1.02)
* 05 October 2021 Software heardbeat monitor LED on pin 13 added (V1.03)
* 06 October 2021 Software upgraded to 20 DS18B20 devices on a single 1-Wire Bus (V1.04)
* REF.No: 061600OCT21_104
* PURPOSE: This sketch sets up 1-Wire Bus communication between an Arduino Uno Rev3 and multiple DS18B20 temperature sensors.
* It shows:
* - Baudrate setting for the serial monitor,
* - GPIO-pin used for collecting data from the 1-Wire Bus,
* - Number of connected devices,
* - Unique 64-bit lasered ROM codes of each connected DS18B20 device,
* - Resolution setting (9, 10, 11 or 12 bits) and corresponding conversion time
* prior to measuring and displaying the temperature of each device.
* Temperature is shown in Kelvin (K), degrees Farenheid (°F) an degrees Centigrade (°C).
* This sketch is NOT sponsored by the well known fastfood restaurant, any simularities are just by coincident ;-)
*
* Original sketch from: https://www.best-microcontroller-projects.com/ds18b20.html (Arduino Sketch 1; OneWire DS18S20, DS18B20, DS1822 Temperature Example)
*
* CONNECTIONS: Put a 10 µF electrolytic capacitor between VDD and GND that acts as an power buffer at the start of the 1-Wire Bus.
* When large numbers of devices are connected to the same 1-Wire Bus, additional capacitors are used at regular intervals (minimum of one per eight to ten devices).
*
* A single 4K7 Bus Pull-Up resistor is necessary between VDD and DQ, which should be placed between the Arduino and first device on the Data-line of the 1-Wire Bus.
* Lower the value of the Bus Pull-Up resistor to a minimum of 2K2 when using very long wires to pass more current.
*
* Fit each device with a 100 nF decoupling capacitor, this will reduce High Frequency noise signals and prevent bad readings.
* Position it as close as it can be to the corresponding ds218b20 device.
*
* Connect a resistor of 100 to 120 Ohms to the DQ-pin of each device, before connecting it to the 1-Wire Bus DQ-line.
* This will reduce reflections significally, especially when more than ten devices are connected to the same 1-Wire Bus.
*
* Keep in mind, that large number of devices on the 1-Wire Bus, will slow down the update rate due to the conversion time of each device and displaying temperatures.
*/
// Declare specific constants and/or variables:
int gpioPin = 2; // Arduino GPIO-pin for connecting to 1-Wire Bus
int baudRate = 9600; // Baud rate for serial port
int deviceCount = 0; // Initializes/resets device counter
// int res9 = 9; // 9-bits resolution (0.5 ºC) conversion time = 93.75 ms
// int res10 = 10; // 10-bits resolution (0.25 ºC) conversion time = 187.5 ms
// int res11 = 11; // 11-bits resolution (0.125 ºC) conversion time = 375 ms
int res12 = 12; // 12-bits resolution (0.0625 ºC) conversion time = 750 ms
float versionSketch = 1.04; // Keeping track of software version
// Initializes variables for temperatures in degrees Kelvin, Fahrenheid and Celsius ("KFC"-format)
float tempK;
float tempF;
float tempC;
// Hexadecimal addresses of all the DS18B20 sensors connected to the 1-Wire bus:
byte sensor1[8] = {0x28, 0x20, 0x49, 0xCB, 0x0A, 0x00, 0x00, 0x75};
byte sensor2[8] = {0x28, 0x48, 0xF6, 0xC9, 0x0A, 0x00, 0x00, 0x30};
byte sensor3[8] = {0x28, 0xBC, 0xAC, 0xCA, 0x0A, 0x00, 0x00, 0x3E};
byte sensor4[8] = {0x28, 0x62, 0x28, 0xCC, 0x0A, 0x00, 0x00, 0x34};
byte sensor5[8] = {0x28, 0x1A, 0x03, 0xCA, 0x0A, 0x00, 0x00, 0x8C};
byte sensor6[8] = {0x28, 0x9A, 0xF1, 0xCC, 0x0A, 0x00, 0x00, 0x4A};
byte sensor7[8] = {0x28, 0xFA, 0xD8, 0xCA, 0x0A, 0x00, 0x00, 0x8B};
byte sensor8[8] = {0x28, 0x8E, 0xDA, 0xCA, 0x0A, 0x00, 0x00, 0x4C};
byte sensor9[8] = {0x28, 0xC1, 0xA8, 0xCA, 0x0A, 0x00, 0x00, 0xF3};
byte sensor10[8] = {0x28, 0x59, 0x04, 0xCB, 0x0A, 0x00, 0x00, 0x7E};
byte sensor11[8] = {0x28, 0xB5, 0x85, 0xCA, 0x0A, 0x00, 0x00, 0xA3};
byte sensor12[8] = {0x28, 0x6D, 0xF8, 0xC9, 0x0A, 0x00, 0x00, 0xCF};
byte sensor13[8] = {0x28, 0xED, 0xF0, 0xC9, 0x0A, 0x00, 0x00, 0x1B};
byte sensor14[8] = {0x28, 0x03, 0xF6, 0xCC, 0x0A, 0x00, 0x00, 0x3C};
byte sensor15[8] = {0x28, 0xD3, 0xC7, 0xCC, 0x0A, 0x00, 0x00, 0xB1};
byte sensor16[8] = {0x28, 0x8B, 0x54, 0xCA, 0x0A, 0x00, 0x00, 0xCE};
byte sensor17[8] = {0x28, 0x9B, 0x22, 0xCC, 0x0A, 0x00, 0x00, 0x6D};
byte sensor18[8] = {0x28, 0xF7, 0x1E, 0xCC, 0x0A, 0x00, 0x00, 0x76};
byte sensor19[8] = {0x28, 0x0F, 0x53, 0xCA, 0x0A, 0x00, 0x00, 0xA9};
byte sensor20[8] = {0x28, 0xFF, 0x8F, 0xCC, 0x0A, 0x00, 0x00, 0xAD};
// Include the required Arduino libraries:
#include <OneWire.h>
#include <DallasTemperature.h>
// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2
// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);
DeviceAddress deviceAddress; // variable to store the device address
void setup() {
// Start of program, setup only runs once after power-up or a reset
// initialize digital pin 13 (LED_BUILTIN) as an output
pinMode(LED_BUILTIN, OUTPUT);
// turn the LED located on the Arduino board and connected to GPIO-pin 13 (LED_BUILDIN) on, by making the voltage HIGH (turning on and later off repeatedly, is used as a software heartbeat)
digitalWrite(LED_BUILTIN, HIGH);
// Begin serial communication at a baud rate of 9600:
Serial.begin(baudRate);
//Show sketch name and software version
Serial.println("*** Multiple DS18B20's on 1-Wire Bus ***");
// Show software version of the sketch
Serial.print("*** Software version: ");
Serial.print(versionSketch);
Serial.println(" ***");
Serial.println("****************************************");
//Show Arduino board being used
Serial.println();
Serial.println("> UNO_R3> Arduino reports READY!");
// Show Baud rate
Serial.print("> UNO_R3> Baud rate for serial port set to: ");
Serial.print(baudRate);
Serial.println(" Baud");
// Show GPIO-pin used for connecting to 1-Wire Bus
Serial.print("> UNO_R3> 1-Wire Bus connected to GPIO-pin: ");
Serial.println(gpioPin);
Serial.println();
Serial.println();
// Start up the library:
sensors.begin();
Serial.print("UNO_R3> Checking for devices on 1-Wire Bus ");
for (int i = 0; i < 2; i++) {
delay(500);
Serial.print(". ");
}
delay(750);
Serial.println(".!");
Serial.print("> UNO_R3> * Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount);
Serial.println(" device(s)");
Serial.println();
// Identifying and showing devices on the 1-Wire Bus:
Serial.print("UNO_R3> Identifying and showing connected devices by their unique 64-bit lasered ROM code ");
for (int i = 0; i < 2; i++) {
delay(500);
Serial.print(". ");
}
delay(750);
Serial.println(".!");
DeviceAddress deviceAddress; // variable to store the device address
for (int i = 0; i < deviceCount; i++) {
Serial.print("> UNO_R3> * Sensor ");
Serial.print(i + 1);
Serial.print(" (Hexadecimals): ");
sensors.getAddress(deviceAddress, i);
printAddress(deviceAddress);
// Set resolution of a specific device to; 9, 10, 11 or 12 bits
sensors.setResolution(deviceAddress, res12);
Serial.println();
Serial.print("> UNO_R3> * Sensor ");
Serial.print(i+1);
Serial.print(" set to ");
Serial.print(res12);
Serial.println("-bits resolution (0.0625 ºC), conversion time = 750 ms");
Serial.println();
}
}
void loop() {
// put your main code here, to run repeatedly:
// turn the LED off the Arduino board connected to pin 13 (LED_BUILDIN) on by making the voltage LOW
digitalWrite(LED_BUILTIN, LOW);
Serial.println();
Serial.println("UNO_R3> Measuring and displaying temperature(s) from connected device(s):");
// Send the command for all devices on the bus to perform a temperature conversion:
sensors.requestTemperatures();
// turn the LED on the Arduino board connected to pin 13 (LED_BUILDIN) on by making the voltage HIGH
digitalWrite(LED_BUILTIN, HIGH);
// Show mesurments after a short delay and start again
delay(750);
Serial.print("> UNO_R3> * Sensor 1 temperature: ");
printTemperature(sensor1); // call the printTemperature function with the address of sensor1 as input
Serial.print("> UNO_R3> * Sensor 2 temperature: ");
printTemperature(sensor2); // call the printTemperature function with the address of sensor2 as input
Serial.print("> UNO_R3> * Sensor 3 temperature: ");
printTemperature(sensor3); // call the printTemperature function with the address of sensor3 as input
Serial.print("> UNO_R3> * Sensor 4 temperature: ");
printTemperature(sensor4); // call the printTemperature function with the address of sensor4 as input
Serial.println();
Serial.print("> UNO_R3> * Sensor 5 temperature: ");
printTemperature(sensor5); // call the printTemperature function with the address of sensor5 as input
Serial.print("> UNO_R3> * Sensor 6 temperature: ");
printTemperature(sensor6); // call the printTemperature function with the address of sensor6 as input
Serial.print("> UNO_R3> * Sensor 7 temperature: ");
printTemperature(sensor7); // call the printTemperature function with the address of sensor7 as input
Serial.print("> UNO_R3> * Sensor 8 temperature: ");
printTemperature(sensor8); // call the printTemperature function with the address of sensor8 as input
Serial.print("> UNO_R3> * Sensor 9 temperature: ");
printTemperature(sensor9); // call the printTemperature function with the address of sensor9 as input
Serial.print("> UNO_R3> * Sensor 10 temperature: ");
printTemperature(sensor10); // call the printTemperature function with the address of sensor10 as input
Serial.print("> UNO_R3> * Sensor 11 temperature: ");
printTemperature(sensor11); // call the printTemperature function with the address of sensor11 as input
Serial.print("> UNO_R3> * Sensor 12 temperature: ");
printTemperature(sensor12); // call the printTemperature function with the address of sensor12 as input
Serial.println();
Serial.print("> UNO_R3> * Sensor 13 temperature: ");
printTemperature(sensor13); // call the printTemperature function with the address of sensor13 as input
Serial.print("> UNO_R3> * Sensor 14 temperature: ");
printTemperature(sensor14); // call the printTemperature function with the address of sensor14 as input
Serial.print("> UNO_R3> * Sensor 15 temperature: ");
printTemperature(sensor15); // call the printTemperature function with the address of sensor15 as input
Serial.print("> UNO_R3> * Sensor 16 temperature: ");
printTemperature(sensor16); // call the printTemperature function with the address of sensor16 as input
Serial.print("> UNO_R3> * Sensor 17 temperature: ");
printTemperature(sensor17); // call the printTemperature function with the address of sensor17 as input
Serial.print("> UNO_R3> * Sensor 18 temperature: ");
printTemperature(sensor18); // call the printTemperature function with the address of sensor18 as input
Serial.print("> UNO_R3> * Sensor 19 temperature: ");
printTemperature(sensor19); // call the printTemperature function with the address of sensor19 as input
Serial.print("> UNO_R3> * Sensor 20 temperature: ");
printTemperature(sensor20); // call the printTemperature function with the address of sensor20 as input
Serial.println("> UNO_R3> Starting measurements again . . .");
Serial.println();
}
void printAddress(DeviceAddress deviceAddress) {
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(", ");
}
}
}
void printTemperature(DeviceAddress address) {
// Fetch the temperature in degrees Celsius for device address:
float tempC = sensors.getTempC(address);
// Fetch the temperature in degrees Fahrenheit for device address:
float tempF = sensors.getTempF(address);
// Calculate the temperature in Kelvin from fetched tempC
float tempK = tempC + 273;
// Print the temperature in Kelvin
Serial.print(tempK);
Serial.print(" K | ");
// Print the temperature in Fahrenheit
Serial.print(tempF);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("F | ");
// Print the temperature in Celsius
Serial.print(tempC);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.println("C");
}