09-14-2022, 01:49 AM
these 10pcs of DS18B20 temperature sensors work with one ESP32 GPIO. program will print 10 sensor's value at the same time.
Code:
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 13
#define TEMPERATURE_PRECISION 9
// 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 addresses
DeviceAddress sensor1,sensor2,sensor3,sensor4,sensor5,sensor6,sensor7,sensor8,sensor9,sensor10;
void setup(void)
{
// start serial port
Serial.begin(115200);
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
//
// method 1: by index
if (!sensors.getAddress(sensor1, 0)) Serial.println("Unable to find address for Device 0");
if (!sensors.getAddress(sensor2, 1)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor3, 2)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor4, 3)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor5, 4)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor6, 5)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor7, 6)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor8, 7)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor9, 8)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(sensor10, 9)) Serial.println("Unable to find address for Device 1");
// set the resolution to 9 bit per device
sensors.setResolution(sensor1, TEMPERATURE_PRECISION);
sensors.setResolution(sensor2, TEMPERATURE_PRECISION);
sensors.setResolution(sensor3, TEMPERATURE_PRECISION);
sensors.setResolution(sensor4, TEMPERATURE_PRECISION);
sensors.setResolution(sensor5, TEMPERATURE_PRECISION);
sensors.setResolution(sensor6, TEMPERATURE_PRECISION);
sensors.setResolution(sensor7, TEMPERATURE_PRECISION);
sensors.setResolution(sensor8, TEMPERATURE_PRECISION);
sensors.setResolution(sensor9, TEMPERATURE_PRECISION);
sensors.setResolution(sensor10, TEMPERATURE_PRECISION);
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
// sensors.requestTemperatures();
float tempC1 = sensors.getTempC(sensor1);
float tempC2 = sensors.getTempC(sensor2);
float tempC3 = sensors.getTempC(sensor3);
float tempC4 = sensors.getTempC(sensor4);
float tempC5 = sensors.getTempC(sensor5);
float tempC6 = sensors.getTempC(sensor6);
float tempC7 = sensors.getTempC(sensor7);
float tempC8 = sensors.getTempC(sensor8);
float tempC9 = sensors.getTempC(sensor9);
float tempC10 = sensors.getTempC(sensor10);
Serial.println(tempC1);
Serial.println(tempC2);
Serial.println(tempC3);
Serial.println(tempC4);
Serial.println(tempC5);
Serial.println(tempC6);
Serial.println(tempC7);
Serial.println(tempC8);
Serial.println(tempC9);
Serial.println(tempC10);
Serial.println("*************");
}