Menu

How do I read a DS18B20 without delaying my program?

2025-03-21
2025-04-25
  • Gert Klemmensen

    Gert Klemmensen - 2025-03-21

    Hi,
    In the library for DS18B20 (ds18b20.h) there is something about polling,
    although I don't know what polling is, I think it might be the solution to my problem.
    Can someone explain to me how to do it?

     
  • Anobium

    Anobium - 2025-03-21

    Can you explain what you mean by 'my problem' ?


    Any read of any device ( internal or external to the microcontroller ) takes time., so, to better understand and potentially resolve, I’d greatly appreciate your insights on the following:

    Could you explain what "polling" means in the context of the DS18B20 library and how it operates in your mind?

    Are there specific use cases or scenarios where polling is especially useful with DS18B20 sensors?

    How does polling compare to other methods, such as interrupt-based approaches, or a straight read operation?

    Is there any particular setup or configuration required to enable polling in the ds18b20.h library? Are there examples or resources you’d recommend to help implement polling effectively with DS18B20 sensors?

    I need more guidance, resources, or examples you can provide to help me better grasp the concept and its application.


    I just use the DS1bB20 library.

     
    • Gert Klemmensen

      Gert Klemmensen - 2025-03-21

      Sorry, I can see that my problem is poorly formulated.
      I'll try to see if I can do it better.

      My problem is the time it takes to get a measurement from my DS18B20 sensor.

      When I run this program fragment and measure with my scope on pin B.3

      Do
      value = ReadTemp
      PORTB.3 = ! PORTB.3
      Loop
      

      It takes 850 ms to get a measurement.

      So my main program "stops" for 850 ms every time I need to get a measurement from my DS18B20.

      How can I get a measurement from my DS18B20 without my main program "stopping" for more than max. 20 ms?

       
  • Anobium

    Anobium - 2025-03-21

    The DS18B20 operates on the 1-Wire protocol, which requires precise timing for reliable communication. These timing values are important when implementing communication with the sensor, especially as we are bit-banging the protocol rather than using a dedicated 1-Wire interface.

    The current library is based on https://www.analog.com/media/en/technical-documentation/tech-articles/1wire-communication-with-a-microchip-picmicro-microcontroller.pdf


    See ..demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\MultiPort_Sensors_to_Hardware_Serial_Terminal\DS18X20 ID Search with Multidrop and Serial Output ATMega.gcb

    This looks like it is using the ConvertT and it has lots of optmised operations.


    Or, you adapt the library to give you full control of the timings. This would not break the existing use of the library. With the adaption you can work out exactly where each delay is.


    Or, you use the I2C to OneWire converter. The DS2482 is the most commonly used I²C to 1-Wire bridge chip. It allows you to connect 1-Wire devices (like the DS18B20 temperature sensor) to an I²C bus.

    Key features of the DS2482:

    Converts I²C protocol to 1-Wire protocol 
     DS2482-100: Single 1-Wire channel
     Operates at standard I²C bus speeds
    Has built-in 1-Wire timing generation
    Includes programmable strong pullup for powering 1-Wire devices
    Has active-low presence detect output
    

    So, you can read via I2C. I use this one on of the test beds. Not sure of the conversion timing.

     
  • Gert Klemmensen

    Gert Klemmensen - 2025-03-31

    @Anobium

    Thank you very much for the good advice. I don't know DS2482 but I need to look into it more.

    I have solved my problem. I looked at the library for DS18B20 repeatedly and suddenly I saw the light.

    I adopted the library into my code and it gave me full control over the times as you suggested.

    My question was poorly formulated and not elaborated enough. I will try to do better next time.

     
    • Anobium

      Anobium - 2025-03-31

      Well done.

      My pleasure to help.

      What was the exact solution?

       
      • Gert Klemmensen

        Gert Klemmensen - 2025-04-08

        Thanks!

        I adopted the library into my code.
        And in the loop below from the DS18B20 library where pin DQ is checked to be high for complete acquisition. I have inserted my code to retrieve data from the a/d converter and reach my goal of a maximum of 20 milliseconds between samples.

        OWout ConvertT  ' Instruct ds18b20 to begin temperature conversion to digital
        
           Do
                        dir DQ out
                        Set DQ Off
                        wait 4 us
                        Dir DQ In
                        wait 7 us
                        If DQ = 1 Then Exit Do
        
                        SetTemp = Scale (ReadAD(AN0), 0, 254, 10, 40) ;Read AN0 while waiting 
                        wait 60 us    'minimum 60 us time slots between reads
           Loop
        

        In my main program I read the temperature to the GLCD display and right after a new ReadTemp command is send.

        I hope I have explained myself so that it can be understood.

         
  • Anobium

    Anobium - 2025-04-08

    I would move the adapted function in the library to your main program.

    Rename the function, then use #DEFINE oldfunction newfunction. Else you will lose you work.

     
    • Gert Klemmensen

      Gert Klemmensen - 2025-04-25

      Good advice thanks a lot!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.