Jag har äntligen löst detta fel genom att ändra på upplösningen på min DS18B20.
Hittade lösningen genom en slump i ett annat forum om problem med fel temperatur med en DS18B20 som kunde vara fel upplösning på sensorn.
Jag googlade lite och hittade den här sidan
http://mydomotic.blogspot.se/2015/10/co ... -save.html.
Min lösning
- Köpte en Anduino Uno och ett 4.7kOhm motstånd
- Laddade hem OneWire biblioteket och kopiera det till libraries.
- Koppla sensor till 5V, GND och PIN2.
Koppla motståndet mellan 5V och PIN2.
- Använde koden nedan och laddade upp till Anduino Unon.
Programmet letar upp sensorn och ändrar den till 12 bit upplösning.
Kod: Markera allt
#include <OneWire.h>
#define OUT_PIN 2 // output digital pin
OneWire ds(OUT_PIN); // Connect your 1-wire device to pin "OUT_PIN"
byte addr[10][8];
byte device_nb = 0;
void setup(void) {
Serial.begin(9600);
SearchDevices(); // search 1-wire devices
// Set configuration
for( device_nb = 0; device_nb < 10; device_nb++) {
if (addr[device_nb][0] == 0x28){
Serial.print("\r\nScratch Pad before modification: \r\n");
ReadScratchPad(addr[device_nb]); // display scratchpad
WriteScratchPad(addr[device_nb], 12); // write scratchpad configuration, ask for a 9, 10, 11 or 12 bits resolution
Serial.print("\r\nScratch Pad after modification: \r\n");
ReadScratchPad(addr[device_nb]); // display scratchpad
Serial.print("\r\n");
}
}
}
void loop(void) {
// do nothing
}
void SearchDevices(){
byte i;
Serial.print("Looking for 1-Wire devices...\n\r");
while(ds.search(addr[device_nb])) {
if ( OneWire::crc8( addr[device_nb], 7) != addr[device_nb][7]) {
Serial.print("CRC is not valid!\n");
break;
}
if (device_nb < 10){
device_nb++;
} else {
Serial.print("More than 10 devices, unable to load them all\n");
break;
}
}
ds.reset_search();
if (device_nb < 9){
addr[device_nb][0] = 0; // erase last 1-wire device as it is accounted 2 times
}
for( device_nb = 0; device_nb < 10; device_nb++) {
if (addr[device_nb][0] == 0x28){
Serial.print("Device Number ");
Serial.print(device_nb);
Serial.print(" : ");
for( i = 0; i < 8; i++) {
Serial.print(addr[device_nb][i], HEX);
Serial.print(" ");
}
Serial.print("\n\r");
}
}
}
void ReadScratchPad(byte addr[8]){
byte data[12];
byte i;
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
}
void WriteScratchPad(byte addr[8], byte res_bit){
byte reg_cmd;
switch(res_bit){
case 9: reg_cmd = 0x1F; break;
case 10: reg_cmd = 0x3F; break;
case 11: reg_cmd = 0x5F; break;
case 12: reg_cmd = 0x7F; break;
default: reg_cmd = 0x7F; break;
}
ds.reset();
ds.select(addr);
ds.write(0x4E); // Write scratchpad command
ds.write(0); // TL data
ds.write(0); // TH data
ds.write(reg_cmd); // Configuration Register (resolution) 7F=12bits 5F=11bits 3F=10bits 1F=9bits
ds.reset(); // This "reset" sequence is mandatory
ds.select(addr); // it allows the DS18B20 to understand the copy scratchpad to EEPROM command
ds.write(0x48); // Copy Scratchpad command
}