B4R Question Dallas Onewire/DS18B20 probe issue

Kevin

Well-Known Member
Licensed User
Longtime User
I've been using my first B4R project for a while now and so far it is pretty solid except for one problem that keeps recurring. I am using these probes. Overall they have a very good rating so I am unsure where the problem lies: Code, circuitry or just a bad batch of probes. This was a 5 pack, and I have now gone through 4 of them. These probes are in pool water, but they are supposed to be waterproof and on the first probe, I actually dipped it in plasti-dip to further waterproof it. After the first failed, I no longer bothered with the plasti-dip but do eventually plan to use it again to prolong probe life in pool water.

What happens: Initially the probe works fine. I get no errors and they track perfectly. Eventually, after several hours or days, I start to get errors that the probe is not connected or more typically, I get incorrect readings. Some examples I have seen:
  • 31.9 F (essentially 0 C) - Supposedly this can happen with a bad "read", such as not giving the probe enough time between requesting a read and getting the result. I tried increasing the delay to no avail.
  • 194.3 F (essentially 90.2 C)
  • 193.4 F
  • 195.8 F
I use 5v to power everything in the project (ESP-12E, 20x4 LCD display, DS18B20 Temp probe, DH22 Temp/Hum sensor, and a relay). Everything works fine except for the DS18B20 probes.

Initially, the connections to the DS18B20 were straightforward: 5v to Power, Gnd to Gnd, ESP pin to data. After the third time the probe stopped working, I did some Googling and decided to try a 4.7k resistor between power and data. I still don't understand why it should be necessary, but had nothing to lose. Again, it worked for a day or two then acted up once again.

The only thing I haven't tried yet is to change over to 3v for the DS18B20, but it is supposed to be able to take 5v. Has anyone else ever had problems with these probes as described above? What should my next step be? I do plan on buying a different brand of probe, but since these got such good ratings, I am not too confident that it will fix the problem. What do others recommend regarding supply voltage (5v vs 3v) and whether or not a resistor is needed between power and data?

The only noteable changes I made to the onewire sample code is that I increased the delay from 1000 to 1350 ms between requesting the temp and reading it, and I also moved the "onewireTemp.ResetSearch" line in order to allow for me to detect if no probe is connected. If there is a better way to do this then any advice is welcome. Before I moved it, it would always skip a reading on every other call to this sub. Side note: Is there is way to simply read 'index' 0 (which would be the first probe detected - and the ONLY probe in a single-probe onewire configuration)?

Any warning messages are stored with GlobalStore and the UpdateDisplay sub (not shown due to complexity) displays them on the last line of the LCD on a cycle every time it is updated (every 5 seconds it shows the next message as needed then loops).

B4X:
Private Sub ReadTemp (u As Byte)
    onewireTemp.Reset
    onewireTemp.Select(address)
    onewireTemp.Write(0xBE, False)
    Dim data(12) As Byte
    onewireTemp.ReadBytes(data, 9)
    Dim raw As Int = Bit.Or(Bit.ShiftLeft(data(1), 8), data(0))
    If type_s Then
        raw = Bit.ShiftLeft(raw, 3)
        If data(7) = 0x10 Then
            raw = Bit.And(raw, 0xFFF0) + 12 - data(6)
        End If
    Else
        Dim cfg As Byte = Bit.And(data(4), 0x60)
        If cfg = 0 Then
            raw = Bit.And(raw, Bit.Not(7))
        Else if cfg = 0x20 Then
            raw = Bit.And(raw, Bit.Not(3))
        Else if cfg = 0x40 Then
            Bit.And(raw, Bit.Not(1))
        End If
    End If
    
    Dim celsius As Double = raw / 16
    If celsius <> 85 Then '85C means it failed to get temperature so ignore it.
                PoolTemperature = ((celsius * 9) / 5) + 32
    End If
        
    UpdateDisplay
End Sub

Private Sub THScanTimer_Tick '<---- main loop timer, which is set for 5000 ms

    'Air temp & Humidity
    Dim dhtResult As Int = DHT22sensor.Read22(DHT22pin.PinNumber) '0 = OK, -3 = N/C  (Library incorrectly says 0 = fail)
    
    Select dhtResult
        Case 0 'OK
            AirHumidity=DHT22sensor.GetHumidity
            AirTemperature = (DHT22sensor.GetTemperature * 1.8) + 32
            AirHeatIndex = HeatIndex (AirTemperature, AirHumidity)
            GlobalStore.Put (5, "") 'Remove any warnings that may have shown earlier about air temp probe
        Case -3 'N/C
            If Logging Then Log ("DHT22: N/C")
            AirTemperature = 999: AirHeatIndex = 999 'UpdateDisplay sub checks for this and converts to dashes
            GlobalStore.Put (5, "Air Temp/Hum Sensor?")
    End Select

    'Pool Temp (onewire)
    onewireTemp.ResetSearch 'Should only reset before search if there is only one device connected to onewire.  Otherwise put if after if search = false below
    If onewireTemp.Search(address) = False Then
        If Logging Then Log ("OneWire: N/C")
        GlobalStore.Put (6, "Pool Temp Sensor?")
        PoolTemperature = 999  'UpdateDisplay sub checks for this and converts to dashes
        UpdateDisplay
        Return
    End If
    
    If Logging Then Log ("OneWire address found: ",bc.HexFromBytes(address))
    
    If onewireTemp.CRC8(address, 7) <> address(7) Then
        If Logging Then Log ("Pool Temp Probe: CRC is not valid")
        GlobalStore.Put (6, "Pool Temp CRC Fail")
        UpdateDisplay
        Return
    End If
    
    Select address(0)
        Case 0x10
            'WriteLog("Chip = DS18S20")
            type_s = True
        Case 0x28
            'WriteLog("Chip = DS18B20")
            type_s = False
        Case 0x22
            'WriteLog("Chip = DS1822")
            type_s = False
        Case Else
            If Logging Then Log ("Pool Temp Probe is not a DS18x20 family device.")
            GlobalStore.Put (6, "Pool Probe Incorrect")
            PoolTemperature = 999
            UpdateDisplay
            Return
    End Select
    
    GlobalStore.Put (6, "") 'Remove any warnings that may have shown earlier about pool temp probe
    onewireTemp.Reset
    onewireTemp.Select(address)
    onewireTemp.Write(0x44, True)
    'give it 1 second to read the temperature
    CallSubPlus("ReadTemp", 1350, 0) 'Used to wait 1000ms, increased delay a bit
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
the resistor is needed to cut back the 2 wires (one wire hug?) capacitance and thus create a network termination.
many sensor have this resistor internally. one thing you do not mention is at what distance is the sensor from the controller, and what type or cable did you use to connect the sensor.
One wire sensors are sensitive to those parameters as explained in phillips white paper about one wire sensors networks
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Regarding length: The included wire is approx 100 cm (40 in) long and I extend it by another 50 cm (20 in) or so (approx 60 in total) using 22 AWG 3-conductor cable, soldered together.

It would seem to me that if it works initially, then it should continue to work. How it can work for a few hours or days then repeatedly fail just makes no sense to me.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I just took a look at it and it was reading 80.8, which is around 5 degrees (F) lower than what the heater shows so I cycled power on the device and now it only shows 0.00, which based on how the code is structured, would tell me that it hasn't received any "errors" but also has not received any kind of a real temperature reading. I would guess it is coming back as 85 C, which indicates a failure to read the temperature according to everything I have read about them.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I can't say for certain, but I can't imagine that there could be more EMI interference where it's at now as opposed to when it was a tangle of wires heaped onto a couch sitting next to my laptop PC. :D
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
:D Decided to look at it one more time before going to bed. As this isn't even close to within range of the temperature probe, I seriously don't have a clue what is going on. Interference? Data/memory corruption?

Screenshot_20180725-223102.jpg
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I have experience with two of type ds18b20, connected to RPI and measuring a fish-tank temperature.
The first worked well until water penetrated... the second is working for more then a year without problems.
Connected to 5v with the 4.7k resistor, cable length is what it came with.
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I've got some with longer cable attached on the way. I wouldn't think the splice would make a difference but who knows.

As the signal is digital, I would think it would either work or not work at all.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
From what I've read, it should already not be working in parasitic mode. I haven't tested it to confirm this though. In parasitic mode, the power wire would not be used at all.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Yes, all three are connected: power, ground & data. With a 4.7k resistor between power and data.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Just figured I'd post an update to this in case anyone else ever has similar issues. As mentioned above, I bought new probes with longer cables. Although they were supposedly made by the same manufacturer (they were the only "brand" that had the longer cables on Amazon that I found), they had completely different part numbers and the probe enclosure was a different length and shape at the end. I suspect they were just re-branded which is one of the problems with these parts - often times you just don't know exactly what you are getting or who actually manufactured it.

Long story short, I have had zero problems with these new probes. It took me a while to get around to replacing the probe, but I have had the new probe in service (in my pool) for almost 2 weeks now and it is working just fine. The others started acting up after hours or days.
 
Upvote 0
Top