Le cablage est bon, je suis sur la broche 2, et si je pose le doigt sur un capteur la valeur change.
Il est bien reconnu comme DS18B20.
Le fichier exemple est Multiple.ino de la librairie Dallastemperature.
J'ai fais un programme qui logue sur SD la valeur de 4 capteurs, et j'ai les mêmes températures:
Time(s),BattTemp(deg),MotorTemp(deg),ESCTemp(deg),AmbientTemp(deg)
1,-40.00,-40.50,-39.50,-39.50
2,-40.00,-40.50,-39.50,-39.50
3,-40.00,-40.50,-39.50,-39.50
4,-40.00,-40.50,-39.50,-39.50
5,-40.00,-40.50,-39.50,-39.50
6,-40.00,-40.50,-39.50,-39.50
7,-40.00,-40.50,-39.50,-39.50
Mon code est
/*
Temperature and SD card datalogger
This example shows how to log data from four DS18B20 sensors
to an SD card using the SD library.
The circuit:
four DS18B20 on pin 2
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS/SS - pin 4 on UNO, 10 on MINI PRO
*/
#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const byte CS_SD = 4;
const char filename[] = "datalog.csv";
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
const byte NUMBER_ONE_WIRE_DEVICES = 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);
// arrays to hold device addresses
DeviceAddress BattTemp, MotorTemp, ESCTemp, AmbientTemp;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time data was updated
// constants won't change :
const long interval = 1000; // interval at which to measure (milliseconds)
// Enable debug prints to serial monitor
#define MY_DEBUG
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#ifdef MY_DEBUG
Serial.println(F("Initialisation of DS18B20 sensors"));
#endif
// Start up the library
sensors.begin();
#ifdef MY_DEBUG
// locate devices on the bus
Serial.print(F("Locating devices..."));
Serial.print(F("Found "));
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(F(" devices."));
#endif
// 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).
//
// method 1: by index
if (!sensors.getAddress(BattTemp, 0)) {
Serial.println(F("Unable to find address for Device 0"));
}
if (!sensors.getAddress(MotorTemp, 1)) {
Serial.println(F("Unable to find address for Device 1"));
}
if (!sensors.getAddress(ESCTemp, 2)) {
Serial.println(F("Unable to find address for Device 2"));
}
if (!sensors.getAddress(AmbientTemp, 3)) {
Serial.println(F("Unable to find address for Device 3"));
}
#ifdef MY_DEBUG
// show the addresses we found on the bus
Serial.print(F("Device 0 Address: "));
printAddress(BattTemp);
Serial.print(F(", Resolution: "));
Serial.print(sensors.getResolution(BattTemp));
Serial.print(F(","));
switch (BattTemp[0]) {
case 0x10:
Serial.println(F("DS18S20"));
break;
case 0x28:
Serial.println(F("DS18B20"));
break;
case 0x22:
Serial.println(F("DS1822"));
break;
case 0x3B:
Serial.println(F("DS1825"));
break;
}
Serial.print(F("Device 1 Address: "));
printAddress(MotorTemp);
Serial.print(F(", Resolution: "));
Serial.println(sensors.getResolution(MotorTemp));
Serial.print(F(","));
switch (MotorTemp[0]) {
case 0x10:
Serial.println(F("DS18S20"));
break;
case 0x28:
Serial.println(F("DS18B20"));
break;
case 0x22:
Serial.println(F("DS1822"));
break;
case 0x3B:
Serial.println(F("DS1825"));
break;
}
Serial.print(F("Device 2 Address: "));
printAddress(ESCTemp);
Serial.print(F(", Resolution: "));
Serial.println(sensors.getResolution(ESCTemp));
Serial.print(F(","));
switch (ESCTemp[0]) {
case 0x10:
Serial.println(F("DS18S20"));
break;
case 0x28:
Serial.println(F("DS18B20"));
break;
case 0x22:
Serial.println(F("DS1822"));
break;
case 0x3B:
Serial.println(F("DS1825"));
break;
}
Serial.print(F("Device 3 Address: "));
printAddress(AmbientTemp);
Serial.print(F(", Resolution: "));
Serial.println(sensors.getResolution(AmbientTemp));
Serial.print(F(","));
switch (AmbientTemp[0]) {
case 0x10:
Serial.println(F("DS18S20"));
break;
case 0x28:
Serial.println(F("DS18B20"));
break;
case 0x22:
Serial.println(F("DS1822"));
break;
case 0x3B:
Serial.println(F("DS1825"));
break;
}
#endif
#ifdef MY_DEBUG
Serial.println(F("Initializing SD card..."));
#endif
// see if the card is present and can be initialized:
if (!SD.begin(CS_SD)) {
Serial.println(F("Card failed, or not present"));
// don't do anything more:
return;
}
// Check to see if the file exists and delete it:
if (SD.exists(filename)) {
#ifdef MY_DEBUG
Serial.print(filename);
Serial.println(F(" File found, delete it"));
#endif
SD.remove(filename);
}
const char firstline[] = "Time(s),BattTemp(deg),MotorTemp(deg),ESCTemp(deg),AmbientTemp(deg)";
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open(filename, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println(firstline);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.print(F("Error opening "));
Serial.println(filename);
}
#ifdef MY_DEBUG
Serial.println(F("SD card initialized."));
Serial.println(F("======================="));
#endif
//Print the first line
Serial.println(firstline);
}
#ifdef MY_DEBUG
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print(F("0"));
Serial.print(deviceAddress[i], HEX);
}
}
#endif
// Main loop
void loop() {
unsigned long currentMillis = millis();
digitalWrite(LED_BUILTIN, LOW);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
digitalWrite(LED_BUILTIN, HIGH);
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures();
#ifdef MY_DEBUG
Serial.println(sensors.getTempC(BattTemp));
Serial.println(sensors.getTempC(MotorTemp));
Serial.println(sensors.getTempC(ESCTemp));
Serial.println(sensors.getTempC(AmbientTemp));
#endif
// make a string for assembling the data to log:
String dataString = "";
//store the timestamp
dataString += String(currentMillis / 1000);
dataString += ",";
// read three sensors and append to the string:
for (int sensor_number = 0; sensor_number < NUMBER_ONE_WIRE_DEVICES; sensor_number++) {
float temperature = sensors.getTempCByIndex(sensor_number);
#ifdef MY_DEBUG
Serial.println(temperature);
#endif
if (temperature == -127) {
dataString += "N/A";
} else {
dataString += String(temperature);
}
if (sensor_number < (NUMBER_ONE_WIRE_DEVICES - 1)) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open(filename, FILE_WRITE);
// if the file is available, write to it:
if (myFile) {
myFile.println(dataString);
myFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.print(F("Error opening "));
Serial.println(filename);
}
}
}
datalog_multiple_DS18B20.ino (7.67 KB)