Menu

Please help me with the ds18b20 sensor.

Jury
2026-06-15
2026-06-16
1 2 > >> (Page 1 of 2)
  • Jury

    Jury - 2026-06-15

    Hi. I'm just a beginner. I can't get the ds18b20 to work. I wrote the code as described in the help. Besides, I watched the video here. https://www.youtube.com/watch?v=iBojUPEJO2g
    And that's where it works. But I don't. If I use ReadDigitalTemp, the display shows 0.0625. And if readtemp, then the display is 128. Here is my code. What am I doing wrong?

     
  • Jury

    Jury - 2026-06-15

    I've seen it. Moreover, I copied the code from there for mega328. I couldn't get him to work either.
    And I would like to understand what is wrong with my code?

     
  • Roger Jönsson

    Roger Jönsson - 2026-06-15

    What is PC3? How about:
    #define DQ PortC.3

     

    Last edit: Roger Jönsson 2026-06-15
  • Anobium

    Anobium - 2026-06-16

    @Jury

    Post the ASM file being generated. I want to see the version of the compiler in use.


    So, you have the I2C LCD working. That is good.
    PC3 should work as the port. As this resolves to PORTC.3


     
  • Anobium

    Anobium - 2026-06-16

    A quick search of the demos shows many published/verified demos.

    PS C:\GCstudio\gcbasic\demos> Get-ChildItem -Recurse -File |
    >>     Where-Object {
    >>         (Select-String $_.FullName -Pattern '(?i)\bmega\w*\b' -Quiet) -and
    >>         (Select-String $_.FullName -Pattern '(?i)DS18B20' -Quiet)
    >>     } |
    >>     Select-Object -ExpandProperty FullName
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_ROM_Read_to_LCD\DS18B20_ROM_Read_to_LCD_1Wire_supporting_LCD_IO_0_mega168.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_ROM_Read_to_LCD\DS18B20_ROM_Read_to_LCD_1Wire_supporting_LCD_IO_10_mega328p.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_GLCD\Temperature_Sensor_to_GLCD_mega168.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_Hardware_Serial_Terminal\Temperature_Sensor_to_Hardware_Serial_Terminal_mega168.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_Hardware_Serial_Terminal\Temperature_Sensor_to_Hardware_Serial_Terminal_mega328P.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_LCD\4Wire_LCD\Temperature_Sensor_to_LCD_mega168.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_LCD\I2C_LCD\Temperature_Sensor_to_Software_I2C_LCD_mega328p.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_LCD\I2C_LCD\Temperature_Sensor_to_Software_I2C_Multiple_LCDs_mega328p.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_Software_Serial_Terminal\Temperature_Sensor_to_Software_Serial_Terminal_mega168.gcb
    C:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_to_Software_Serial_Terminal\Temperature_Sensor_to_Software_Serial_Terminal_mega328p.gcb
    

    Let me see the ASM.

     
  • Jury

    Jury - 2026-06-16

    My GCBasic\demos folder looks like this

     

    Last edit: Jury 2026-06-16
  • Anobium

    Anobium - 2026-06-16

    Re the empty(ish) demo folder. Download and install the Demo Pack shown on GCSTUDIO startup application.


    ASM looks ok in terms of updates. Yes, there are later builds but this is the recent general release build.


    Things that you can verify for me.

    LCD shows strings and values. This tells me the I2C hardware is working.

    Do a operation. What DSData return?

    You have a 4k7 pullup from the Portc.3 to 5v0 as per the device datasheet.

    There are no errors shown when compiling.

    Has this circuit worked with another compiler?


    Things I do know.

    Your code works in a Simulator.

    There are many working demos. But, this is not to say that in this specific use case there is not an error. :-)


    Evan

     
  • Jury

    Jury - 2026-06-16

    I2C is working. If I output any text, it is displayed correctly on the screen. ReadDigitalTemp displays a value of 0.0625. A 4.7K resistor is present, according to the datasheet. I have not used another compiler. Which compiler should I try?

     
  • Anobium

    Anobium - 2026-06-16

    Was not recommending another compiler. This is a very good toolchange.


    As you are get 0.0625 back this means that the values obtained by the read routines equate to 1
    And, that would be very cold.

    What did DSData return on the LCD?

    Check you have that 4k7 connected correctly. Post a foto of the setup.

     
    • Anobium

      Anobium - 2026-06-16

      Minimum code to read the sensor would be. This should return the raw value. Depends what this returned will determine the next step.

      Do forever
      
          DS18B20DeviceRead
          CLS 
          Print DSDATA
          wait 1 s
      
       Loop
      
       
  • Jury

    Jury - 2026-06-16

    I can take a photo if necessary, but I'm not sure if it will help. There's a bunch of wires with a minimum board for the atmega8. A 4.7k resistor is connected between the red and yellow wires. I've attached a drawing. This sensor works correctly when connected to the arduino.
    Your code returns 65535.

     
    • Anobium

      Anobium - 2026-06-16

      Arduino is another compiler. That is good to know it works with this setup.

      But, with this exact setup, these connections/pinout?


      This is puzzling. As it works on simulation.

       
  • Jury

    Jury - 2026-06-16

    I made a circuit with an analog LM335 sensor with output to an LED display. Everything is working as it should. But I can't get the ds18b20 to work.
    I took the code from here.
    c:\GCstudio\gcbasic\demos\Temperature_Sensor_Solutions\DS18B20_Sensor_Solutions\Sensor_ROM_Read_to_LCD\DS18B20_ROM_Read_to_LCD_1Wire_supporting_LCD_IO_10_mega328p.gcb
    Just changed atmega328 to atmega8. I have installed the Proteus Free Trial.

    Got this.

     

    Last edit: Jury 2026-06-16
  • Anobium

    Anobium - 2026-06-16

    OK.

    What did this return? DSDATA specificially

    Do forever

    DS18B20DeviceRead
    CLS 
    Print DSDATA
    wait 1 s
    

    Loop

     
  • Jury

    Jury - 2026-06-16

    that is

     

    Last edit: Jury 2026-06-16
  • Anobium

    Anobium - 2026-06-16

    Oh. This is not on real silicon? Real hardware?

     
  • Jury

    Jury - 2026-06-16

    No, it's a simulator. I'll try it on a real microcontroller later.

     
  • Anobium

    Anobium - 2026-06-16

    I just had a look at the library.

    As the Arduino runs at much slower speeds ( it does ) then you may have to add some delays into the library.

    Give me 30 mins to change the library.

     
    • Jury

      Jury - 2026-06-16

      You gave me an idea. I configured the atmega8 to run at 16 MHz. And, unexpectedly, the sensor showed the correct temperature. After that, I tried an 8 MHz configuration. At this frequency, the sensor also shows the correct temperature. Then I reduced the frequency to 4 MHz and got a result of 0.0625. The atmega8 default configuration runs at 1 MHz.
      So, the conclusion is that the code doesn't work at low frequencies.

       
      • Anobium

        Anobium - 2026-06-16

        Try the code below. I want to know if this resolves at these other frequencies.

         
        • Jury

          Jury - 2026-06-16

          If I add this code, the display is empty

          And yes, if you explicitly specify the frequency as 1MHz in the original code, it also works. I didn't know that if you don't explicitly specify the frequency, it takes the highest possible value.

           

          Last edit: Jury 2026-06-16
          • Anobium

            Anobium - 2026-06-16

            Add what code?

             
  • Jury

    Jury - 2026-06-16

    Here on the real hardware. Only it's not an Arduino, it's just an atmega8.

     
  • Anobium

    Anobium - 2026-06-16

    So.,the value equates to -1.

    I do not have a device here, but, I see that that the library has been optimised for speed.

    Add the following to your program. This will be called replacing the existing the library sub routine.

    This is restores the time delays.

    Test please.

    Sub DS18B20DeviceRead
      ' This routine makes available a variable called DSdata
      ' DSdata is the raw data value from the sensor
    
       wait 250 ms ' Not really needed but left in just in case ***
       MasterRST   ' Reset ds18b20
       PPulse      ' Request a presence pulse
       wait 1 ms
       OWout SkipRom  ' Bypass ds18b20 serial number read
       wait 1 ms
       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
                    wait 60 us    'minimum 60 us time slots between reads
       Loop
    
       MasterRST
       PPulse
       wait 1 ms
       OWout SkipRom         ' Bypass ds18b20 serial number read
       wait 1 ms
       OWout ReadScratch     ' Read temperature from db18b20
       wait 1 ms
       Owin                  ' Receive temperature low byte from db18b20
       DSdata = RxData       ' First byte  is low byte
       wait 1 ms
       Owin                  ' Receive temperature high byte from db18b20
       DSdata_H = RxData     ' Second byte is high byte  (auto combined into 16 bit word)
    end sub
    
     
1 2 > >> (Page 1 of 2)

Log in to post a comment.

MongoDB Logo MongoDB