Ds18b20 returning wrong CRC

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.

Here's an example for the output:

Scratchpad: 78 1 85 5 127 165 129 102 189 
Calc. CRC: 120
Temp: 20.87 ºC.

Scratchpad: 77 1 85 5 127 165 129 102 207 
Calc. CRC: 189
Temp: 20.81 ºC.

Scratchpad: 77 1 85 5 127 165 129 102 190 
Calc. CRC: 189
Temp: 20.81 ºC.

Scratchpad: 78 1 85 5 127 165 129 102 153 
Calc. CRC: 120
Temp: 20.87 ºC.

Scratchpad: 77 1 85 5 127 165 129 102 160 
Calc. CRC: 189
Temp: 20.81 ºC.

Scratchpad: 77 1 85 5 127 165 129 102 187 
Calc. CRC: 189
Temp: 20.81 ºC.

(I just replaced the "º" sign that was not showing, everything else is copy pasted from the terminal window)

I also replaced the sensor with a different one and got the same readings.
So, any idea will be much appreciated, thanks in advance..

Edit: I forgot to mention that it's the only device, powered in normal mode, in TO-92 format in a breadboard, with a 4,7K pull-up resistor.

Welcome to the forum!

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();
        }

This is the relevant part of the code:

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.

Offtopic:
This is not a UTF-8 degree symbol, U+00BA º
This is a UTF-8 degree symbol, U+00B0 °
This is a HTML &deg; °

It is 2021, if you put a UTF-8 character in the source code, it should be visible on the Serial Monitor and on this forum in the same way.

It is visible, but as a solid circle instead of just the outline.

I think that's Incorrect, you may need:

                //bool test = crc8(scratchPad, 7) == scratchPad[7];
                Serial.print("Calc CRC: ");
                Serial.println(OneWire::crc8(scratchPad, 7));

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.