Hi there, I'm trying to use a ds18b20 sensor and temperature reading was working fine so I decided to go all the way and add CRC checking to make the program more robust.
When reading the whole memory I see that the sensor's CRC is always a different value even for the same set of bytes. The calculated CRC (from the ds18b20 library) is always consistent, don't know if it's right or not but at least it's always the same for the same first 8 bytes.
I don't post any code for now because I highly doubt there's a problem there, or with the physical connection, considering that the temperature reading is fine, the scratchpad reading is always consistent and the only thing that changes is the last byte.
Please post ALL the code (using code tags), as that is the most likely place to find the problem. Of course, you likely have counterfeit or reject sensors, if you bought cheap ones from Alibaba or Amazon.
Hi, in fact I bought them on Amazon. But could a counterfeit sensor give a right temperature and wrong CRC? The reading seems okay, just touching it with the finger causes it to rise immediately and then go down again when I release it.
This is the relevant part of the code:
if (Read_bit())
{
if (ds18b20_start())
{
Write_byte(0xCC); // Send skip ROM command
Write_byte(0xBE); // Send read command
for (int i = 0; i < 9; i++)
{
scratchPad[i] = Read_byte();
}
Serial.print("Scratchpad: ");
for (int i = 0; i < 9; i++)
{
Serial.print(scratchPad[i]);
Serial.print(" ");
}
Serial.println();
//bool test = crc8(scratchPad, 8) == scratchPad[8];
Serial.print("Calc CRC: ");
Serial.println(crc8(scratchPad, 8));
}
temp = ReadSensor() / 16.0;
Serial.print("Temp: ");
Serial.print(temp);
Serial.println(" ºC.");
Serial.println();
}
Post ALL the code. Your opinion of relevance is unlikely to be relevant.
Of course!
If you want genuine sensors, the only way to be sure is buy them from a reputable distributor like Digikey, Mouser, Adafruit, Sparkfun, etc.. That way you get product support, too.
However I may be misinterpretting the datasheet, there's both ROM and RAM that use this. Without seeing which crc function you used I explicitly used the OneWire library version which should be the correct one (there are different CRC8's around).
[ Whoops, didn't notice the results were varying for the same set of bytes! ignore me! ]
Yes, the calculated CRC is consistent, it's the one read from the sensor that always has a different value.
Anyway, seeing that the temperature reading works fine I'm just not bothering with the CRC anymore.