Onewire Temp Sensor Does Not Work on latest 1.20.2.r6 Firmware



  • Hi

    I've been using the Onewire DS18X20 temp sensor driver provided by @robert-hh for a while now, always running firmware 1.20.2.rc6, no problems at all. Just recently upgraded to 1.20.2.r6, the latest revision, and the temp sensor readings return None after the first initial reading.

    I am running this very simple main.py:

    import time
    from machine import Pin
    from ds18x20 import DS18X20
    from onewire import OneWire
    
    #DS18B20 data line connected to pin P10
    ow = OneWire(Pin('P22'))
    temp = DS18X20(ow)
    roms = temp.scan()
    
    temp.convert_temp()
    while True:
        time.sleep(1)
        for rom in roms:
            print(temp.read_temp(rom), end=" ")
        print()
        temp.convert_temp()
    

    And I am using the onewire.py and ds18x20.py libraries provided here: https://github.com/robert-hh/Onewire_DS18X20. These are the ones I have always used, because the Pycom-provided libraries had a bug in temperature conversion.

    The problem is, if I remove the print() statements in the while loop, the conversions are executed correctly. For example, if I modify the above loop to save the conversions to a list, instead of printing them:

    temp.convert_temp()
    ans = []
    while True:
        time.sleep(1)
        for rom in roms:
            ans.append(temp.read_temp(rom))
        temp.convert_temp()
        #print(ans)
    

    And I later probe the value of ans by stopping the loop and writing ans in the REPL, I get:

    >>> ans
    [22.5, 22.5, 22.5, 22.5, 22.5625, 22.5625, 22.5625, 22.5625]
    

    If instead I uncomment the print(ans) line in the above code, I get:

    [22.5]
    [22.5, None]
    [22.5, None, None]
    [22.5, None, None, None]
    [22.5, None, None, None, None]
    [22.5, None, None, None, None, None]
    

    So every other conversion aside from the first one is incorrect, because of an AssertionError: CRC error (I noticed this by raising the exception in the read_temp() method of the ds18x20 class).

    This does not happen on my past firmware, 1.20.2.rc6. I am using a LoPy4.

    Any ideas, anyone? This seems very odd, why should the print statement affect the communication with the DS18X20 sensor? If it were a timing issue, I'd understand a print would affect if executed in time-critical parts, but this print is called after the temperature has already been read.

    There is, however, one very interesting addition: If I change the print statement to print("foo"), the conversion works again. It only fails when the print is related to ans (the temp conversions list).

    By this point, I am already very frustrated and this seems metaphysical. Someone, please tell me if I am crazy. Thanks.



  • @d-alvrzx I'v tested that, and it seems to work fine. Sometimes the conversion fails. But that's to be expected. The timing seems acceptable.

    The print problem you talk about indicates a memory corruption somewhere else in the code. Try to switch of WiFi and Bluetooth.

    P.S.: It took me a while and a handful of curses to compile the firmware. Quite a few compile errors and warnings. At least one of them shows a bug. I do not know if people at Pycom verify, that the code the publish builds at least. It seems that no.



  • Hey, @robert-hh, any chance you could try to reproduce this, just to see if it's just an issue on my side?


Log in to reply
 

Pycom on Twitter