Salve a tutti , ho acquistato un sensore di temperatura DS18B20, l’ ho collegato all’ arduino seguendo questo schema :
ho caricato sull’ arduino questo sketch che utilizza la libreria OneWire e la libreria DallasTemperature :
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// 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 address
DeviceAddress insideThermometer;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// locate devices on the bus
Serial.print("Locating devices...");
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// assign address manually. the addresses below will beed to be changed
// to valid device addresses on your bus. device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// Method 1:
// 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).
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
// show the addresses we found on the bus
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 9);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// It responds almost immediately. Let's print out the data
printTemperature(insideThermometer); // Use a simple function to print out the data
}
// 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);
}
}
Come potete vedere dal log della seriale il sensore non viene riconosciuto :
Requesting temperatures...Dallas Temperature IC Control Library Demo
Locating devices...Found 0 devices.
Parasite power is: OFF
Unable to find address for Device 0
Device 0 Address: 0000000000000000
Device 0 Resolution: 0
Requesting temperatures...DONE
Temp C: -127.00 Temp F: -196.60
Qualcuno ha qualche idea su come fare per risolvere ?
I collegamenti hardware sono corretti, la R sei sicuro che sia effettivamente da 4k7? Se fosse più bassa potrebbe bloccare il sensore su HIGH. Hai provato con un tester che i contatti siano garantiti? tante volte la pessima qualità dei fili e delle breadboard usate fa sì che un circuito non funzioni.
Nemmeno a me quel codice ha mai funzionato…
Io ho risolto con questo Funziona benissimo.
IDE 022.
Auguroni!
/*
Trova indirizzo delle sonde DS18x20
Originale by TutorialPedia : http://tutorialpedia.org/tutorials/Working+with+Dallas+DS18S20+and+DS18B20+temperature+sensors+from+Arduino.html
Modifiche minori e traduzione in italiano
a cura di Bernardo Giovanni (http://www.settorezero.com)
*/
#include <OneWire.h>
// inizializza il bus onewire sulla porta n°10
OneWire ow(16);
void setup(void)
{
Serial.begin(9600); // inizializza la porta seriale a 9600
lookUpSensors(); // avvia la ricerca delle sonde di temperatura
}
void lookUpSensors()
{
byte address[8]; // questo array conterrà l'indirizzo delle sonde
int i=0;
byte ok = 0, tmp = 0;
// avvia la ricerca
Serial.println("--Ricerca avviata--");
while (ow.search(address))
{
tmp = 0;
// Se il primo byte dell'indirizzo è 0x10, si tratta di una sonda DS18S20
if (address[0] == 0x10)
{
Serial.println("A questo indirizzo si trova una DS18S20 : ");
tmp = 1; // ricerca andata a buon fine
}
else
{
// Se il primo byte dell'indirizzo è 0x28, si tratta di una sonda DS18B20
if (address[0] == 0x28)
{
Serial.println("A questo indirizzo si trova una DS18B20 : ");
tmp = 1; // ricerca andata a buon fine
}
}
// Se il flag tmp si trova a 1, mostro l'indirizzo
if (tmp == 1)
{
if (OneWire::crc8(address, 7) != address[7]) // faccio il controllo del CRC8
{
Serial.println(" (ma CRC8 non valido)");
}
else
{
// tutto ok, mostro l'indirizzo in formato esadecimale
for (i=0;i<8;i++)
{
Serial.print("0x");
if (address[i] < 9)
{
Serial.print("0");
}
Serial.print(address[i],HEX);
if (i<7)
{
Serial.print(",");
}
}
Serial.println("");
ok = 1;
}
} //end if tmp
}//end while
if (ok == 0)
{
Serial.println("Non ho trovato sonde di temperatura");
}
Serial.println("--Ricerca terminata--");
}
void loop(void)
{
// non faccio niente... ho fatto tutto nel setup!
}
risolto parzialmente, errore mio avevo (ri)montato male il sensore, il primo sketch rileva la sonda ma rileva esattamente la temperatura solo se il sensore è in parasite mode altrimenti rileva 127.50 C e 261.50 F lo sketch di DanielaES funziona perfettamente (ma rileva soltanto la presenza della sonda).Grazie dell' aiuto e auguri di buon anno a tutti.
encrypt:
lo sketch di DanielaES funziona perfettamente (ma rileva soltanto la presenza della sonda).Grazie dell’ aiuto e auguri di buon anno a tutti.
Una volta che hai l’indirizzo, devi utilizzare questo codice
/*
Temperatura con Arduino e DS18S20
spunti per stazione meteo
original by Bernardo Giovanni (http://www.settorezero.com)
Funzioni per lettura DS18S20 prese da tutorialpedia:
http://tutorialpedia.org/tutorials/Working+with+Dallas+DS18S20+and+DS18B20+temperature+sensors+from+Arduino.html
*/
#include <OneWire.h>
float T; // Valore di temperatura in °C dalla sonda DS18S20
OneWire ONE_WIRE_BUS(10); // Bus One-Wire (sonda DS18S20) sul pin 10
// Indirizzo della sonda DS18S20
// Nota: trova l'indirizzo della tua sonda col programma precedente e incollalo al posto di questo
byte T_SENSOR[8] = {0x10,0x72,0x08,0xBA,0x01,0x08,0x00,0x64};
void setup()
{
Serial.begin(9600); // Inizializzo la porta seriale
}
void writeTimeToScratchpad(byte* address)
{
ONE_WIRE_BUS.reset(); // Resetto il bus
ONE_WIRE_BUS.select(address); // Seleziono la mia sonda DS18S20
ONE_WIRE_BUS.write(0x44,1); // Richiamo la funzione di conversione temperatura (44h)
// che posiziona il valore di temperatura nello scratchpad (comando Convert T)
delay(1000); // Attendo un secondo che la scrittura sia completa
}
void readTimeFromScratchpad(byte* address, byte* data)
{
ONE_WIRE_BUS.reset(); // Resetto il bus
ONE_WIRE_BUS.select(address); // Seleziono la mia sonda DS18S20
ONE_WIRE_BUS.write(0xBE); // Comando di lettura dello scratchpad (comando Read Scratchpad)
for (byte i=0;i<9;i++)
{
data[i] = ONE_WIRE_BUS.read(); // Leggo i 9 bytes che compongono lo scratchpad
}
}
float getTemperature(byte* address)
{
int tr;
byte data[12];
writeTimeToScratchpad(address); // Richiamo conversione temperatura
readTimeFromScratchpad(address,data); // effettuo la lettura dello Scratchpad
tr = data[0]; // Posiziono in TR il byte meno significativo
// il valore di temperatura è contenuto nel byte 0 e nel byte 1
// Il byte 1 contiene il segno
if (data[1] > 0x80) // Se il valore è >128 allora la temperatura è negativa
{
tr = !tr + 1; // Correzione per complemento a due
tr = tr * -1; // Ottengo il valore negativo
}
int cpc = data[7]; // Byte 7 dello scratchpad: COUNT PER °C (10h)
int cr = data[6]; // Byte 6 dello scratchpad: COUNT REMAIN (0Ch)
tr = tr >> 1; // Rilascio il bit 0 come specificato dal datasheet per avere una risoluzione > di 9bit
// Calcolo la temperatura secondo la formula fornita sul datasheet:
// TEMPERATURA = LETTURA - 0.25 + (COUNT PER °C - COUNT REMAIN)/(COUNT PER °C)
return tr - (float)0.25 + (cpc - cr)/(float)cpc;
}
// Conversione da Fahrenheit a Celsius
float f2c(float val)
{
float aux = val - 32;
return (aux * 5 / 9);
}
// COnversione da Celsius a Fahrenheit
float c2f(float val)
{
float aux = (val * 9 / 5);
return (aux + 32);
}
void loop()
{
// Lettura temperatura (la DS18S20 fornisce il valore in °C)
T = getTemperature(T_SENSOR);
// Stampo i risultati su terminale seriale
Serial.print("Temperatura: ");
Serial.print(T,1); // Scrivo il valore di temperatura con un decimale
Serial.println(" C");
// Attendo 1 secondi per ricominciare
delay(1000);
}