bonnyr / wokwi-ds1820-custom-chip

MIT License
2 stars 3 forks source link

Temperature range goes down to -127; should stop at -55 #15

Open TimMathias opened 1 year ago

TimMathias commented 1 year ago

In simulation mode with the temperature slider at the far left (-55):

image

the Serial output displays: Temperature is: -127.00

It should display: Temperature is: -55.00

bonnyr commented 1 year ago

Can you share some info about your project? We've had some spurious behaviour when used with ESP32.

Temperature of -127 usually means that the 1-wire communication is not successful - this is the default value that the host library initialises the temperature setting to.

TimMathias commented 1 year ago

It's the DS18B20 demo project using an Arduino Uno.

image

image

The temperature range from -54.875 to +125 is printed correctly, but -55 is printed as -127.

bonnyr commented 1 year ago

@TimMathias

I spent some time looking around at the DallasTemperature library and I noticed several commits related to handling negative numbers.

I think the issue lies with the library since it defines the following (in DallasTemperature.h):

define DEVICE_DISCONNECTED_RAW -7040

And has the following to say about that value in the implementation (DallasTemperature.cpp):

// returns temperature in 1/128 degrees C or DEVICE_DISCONNECTED_RAW if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {

    ScratchPad scratchPad;
    if (isConnected(deviceAddress, scratchPad))
        return calculateTemperature(deviceAddress, scratchPad);
    return DEVICE_DISCONNECTED_RAW;

However, -55 deg C is exactly -7040 (if you multiply it by 128 which the code does all over)

Thus, when you request a reading the next time, the value matches a DISCONNECTED sensor when in reality it is not.

I have opened an issue here: https://github.com/milesburton/Arduino-Temperature-Control-Library/issues/236

Hopefully, this can be resolved either by fixing this bug (according to me :) ) or telling me what is wrong in my assertion... I will leave this issue open until I hear from the maintainer of DallasTemperature

TimMathias commented 1 year ago

As a workaround, could you constrain the scaled range to -7039 to +16000?

That would be -54.9921875 to +125°C which would be rounded off by many programmes.

bonnyr commented 1 year ago

the minimum is -54.875 simply because how the sensor reports the temperature, there's no way to specify this with the same precision as the library.

I would rather not do this in the sensor if I can avoid this.

Since this is a simulation, the controls specify the temperature, wouldn't that be sufficient to set the temp to the value you desire? Or is there something else in your project that controls the temp?