logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP8266 WiFi: Inaccurate temperature values after decimal point - 28.50 instead of 28.18

slowik359 1296 18
Best answers

Why does my ESP8266 show temperature readings with rounded decimals like 28.50 or 34.00 instead of exact values like 28.18 or 34.12?

The most likely cause is that the DS18B20 sensor resolution was set too low, so the readings only change in coarse steps such as .00 or .50; set the sensor back to 12-bit resolution with `DS18B20.begin(); DS18B20.setResolution(12);` [#20728945][#20729011] This worked for the thread starter and fixed the problem [#20729020]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20727933
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    why does it display me a not exact decimal value in esp8266 wifi e.g. 28.50 ,34.00 ,25.50 ,22.50 and before it was 34.12 or 28.18


    Console screenshot displaying temperature measurement results. @ElektrodaBot
  • ADVERTISEMENT
  • #2 20727937
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    why does it display to me not the exact decimal value in esp8266 wifi e.g. 28.50 ,34.00 ,25.50 ,22.50 and before it was 34.12 or 28.18 https://obrazki.elektroda.pl/2611699600_1694342459.png
    Welcome to the forum!

    I'm guessing that the problem you're describing relates to the incorrect display of the values read from the sensor on the ESP8266 module.

    It appears that the values are rounded to the nearest .50 or .00, which suggests that the problem may lie in the rounding function or the conversion of a floating point number to a string.

    But before we go any further, additional information is needed:

    1. What sensor are you using for the measurements?
    2. What is the code that reads the values from the sensor and displays them?
    3. Have you made any changes to the code, hardware or settings that could cause this problem?
    4. Does the problem occur regardless of which value you are trying to read?

    In most cases, the incorrect display of the result could be due to an incorrect conversion of a floating point number to a string (or vice versa), an error in the rounding function, or a problem with the precision of the calculation on the microcontroller.

    Please provide the above information so that we can better understand and help you solve this problem.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #3 20727999
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    Code: JSON
    Log in, to see the code
    [/code]

    @ElektrodaBot
  • #4 20728014
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    (...) t width height temp
    display.println(temp);
    display.println(" ");
    display.drawRect(123,30, 5, 5, WHITE); // this is from the celsius sign
    display.setCursor(110,25);
    display.setFont();
    display.display();
    Blynk.virtualWrite(V2, temp); //sending to Blynk app



    delay(5000);
    }
    Thanks for providing the full code again. It seems correct and should not cause problems with rounding to .50 or .00. The DallasTemperature library you are using returns the temperature as a `float`, so you should get a full precision reading.

    However, I noticed that you are using `display.println(temp);` to print the temperature to the display. By default, the `println()` function for the `float` type (as in the case of your `temp` variable) will only print two decimal places, which seems to be what you are seeing.

    Also, the `println()` method rounds floating-point numbers rather than truncates them, which may explain why you only see .50 or .00 - this is due to rounding.

    You could try using the `dtostrf()` function to convert `float` to `char[]` with the specified precision, and then print this string to the display. Below is an example of how to do this:

    Code: text
    Log in, to see the code


    The `dtostrf()` function converts a floating point value to a string. The second argument is the minimum width of the field, the third argument is the precision of the number (number of decimal places), and the last argument is the buffer for the result.

    Values greater than 99.99 or less than -9.99 will look slightly different because their length has exceeded 4 places. You can adjust the second argument of `dtostrf()` to match the longest expected number.

    Hopefully this solves the problem. Let me know if you need any more help!
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #5 20728031
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    further same
    Code: C / C++
    Log in, to see the code
    @ElektrodaBot
  • ADVERTISEMENT
  • #6 20728036
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    (...) t width height temp
    display.println(temp);
    display.println(" ");
    display.drawRect(123,30, 5, 5, WHITE); // this is from the celsius sign
    display.setCursor(110,25);
    display.setFont();
    display.display();
    Blynk.virtualWrite(V2, temp); //sending to Blynk app


    delay(5000);
    }
    I noticed that despite using `dtostrf(temp, 4, 2, temp_display);` in your code, you still call `display.println(temp);` to display the temperature. This line will print the `float` value of temp, not the converted `temp_display` string.

    I suggest changing this part to `display.println(temp_display);` to print the converted string, which should contain the full temperature precision. Here is the modified code snippet:

    Code: text
    Log in, to see the code


    At the end I changed `display.println(temp);` to `display.println(temp_display);` in order to use the converted string instead of the original floating point value. This way you should see the full precision of the temperature on the display.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #7 20728079
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    did not help, because on the test one with the arduino it is the same, maybe the sensor is dead @ElektrodaBot
  • #8 20728091
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    it didn't help because on the test one with the arduino it is the same maybe the sensor is dead
    I guess when you say "on the test one with the arduino it is the same", you are trying to use a different microcontroller to test the same sensor? If so, and you still see the same results, it is possible that the problem actually lies with the DS18B20 sensor.

    The DS18B20 sensors are usually quite reliable, but like any device, they can fail.

    Remember that the DS18B20 is a digital sensor and returns temperature as a 16-bit number in binary format, so almost all possible values should be available. Limiting the results to values ending in .00 and .50 is very unusual and not typical of these sensors.

    Before assuming that the sensor is faulty, however, it is worth checking a few things:
    1. Check that the circuit is correctly powered and connected. The DS18B20 operates on a voltage between 3.0V and 5.5V. Make sure that the supply voltage is stable and within this range.
    2. Check that you have correctly connected the "pull-up" resistor to the data line. Typically, a resistor between 4.7k Ohm and 10k Ohm is recommended for the DS18B20.
    3. Try reading the temperature with another DS18B20 sensor (if you have one) to see if the problem occurs with other sensors as well.
    4. Try testing your sensor with simple code that only reads and displays the temperature, with no additional libraries or functions. Does the problem still occur?

    Remember that each sensor has its own specific operation and this may vary slightly from manufacturer to manufacturer. In some cases, sensors may need additional calibration to ensure accurate readings.

    If the problem persists despite attempts to solve it, it is likely that the sensor is actually faulty and you should replace it.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • ADVERTISEMENT
  • #9 20728120
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    The problem may lie in the fact that I have flashing it several times with espe tool, I don't know if I did it right maybe I need to do it again could you tell me what and how @ElektrodaBot
  • #10 20728122
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    The problem may lie in the fact that I have flashing it several times with the espe tool I don't know if I did it right maybe you need to do it again could you provide what and how
    Flashing the ESP8266 is the process of writing new firmware to the microcontroller. If you suspect the problem lies with this, it may be worth trying to re-flash the microcontroller. Below are the steps you should take. I use a tool called "esptool" for this.


    Please note - The steps below are for the Windows operating system.

    Step 1 : Download and install the latest version of Python from the official Python website.

    Step 2 : Open cmd (Command Prompt) and install esptool using the following command:


    Code: Text
    Log in, to see the code


    Step 3 : Connect the ESP8266 module to your computer using the USB cable. Then, using Device Manager, check under which COM port your ESP8266 module is connected.

    Step 4 : Now we need to clear the ESP8266 flash memory. To do this, type the following command into cmd, replacing "COMX" with the appropriate COM port number:

    Code: Text
    Log in, to see the code



    Step 5 : Now we can reflash our ESP8266 module. To do this, we need to download the appropriate firmware binary file. This can be found on the official NodeMCU website. After downloading the file, enter the following command into cmd:


    Code: Text
    Log in, to see the code


    Place "COMX" with your COM port number and "path_to_file_binary" with the path to the downloaded binary file.

    Assuming everything went well, you should now have the ESP8266 module flashed. You can now try running your code again and see if the problem still exists.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #11 20728291
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish

    I cannot find this binary file @ElektrodaBot
    .
  • #12 20728293
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    I may not be familiar with this binary file
    I understand that you may be having trouble finding the correct firmware file for your ESP8266 module. Let me help you.

    Firmware updates for the ESP8266 module are available on the ESP8266 Community project page on GitHub. Below is a link to the latest releases:

    [ESP8266 releases on GitHub](https://github.com/esp8266/Arduino/releases)

    On this page you will see a list of different releases. The latest one is the most recent one. Click on it and then scroll down the page until you see the "Assets" section. This is where you will find the firmware binaries.

    The files are named "esp8266-xxxxxx.zip". Download the latest .zip file and then unzip it. Inside, you should find the .bin file you need to perform the flashing process.

    Memorize to modify the above command appropriately to include the correct path to the binary file on your computer.

    If you still have problems finding the file or with the flashing process, please let me know. I will be happy to help you through the process step by step.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #13 20728312
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    I cannot know this binary file @ElektrodaBot
  • #14 20728316
    ElektrodaBot
    AI OpenAI ChatGPT4
    Posts: 8722
    Help: 349
    Rate: 917
    Board Language: polish
    slowik359 wrote:
    I cannot know this binary file
    It looks like you are still having problems finding the binary file. I apologise for any confusion. Please let me direct you more accurately.


    Download the latest ESP8266 firmware

    The first step is to download the latest firmware for the ESP8266. You can download it directly from the official GitHub page of the ESP8266 project. Here is the direct link to the firmware release page:

    [https://github.com/esp8266/Arduino/releases](https://github.com/esp8266/Arduino/releases)


    How to find the binary file on the release page

    When you get to the release page, scroll down to the "Assets" section. There you will find various files to download. Look for the file that ends in ".bin" - this is the firmware binary file you need to download.

    For example, for release 2.7.4, the binary file is called "esp8266-2.7.4.bin". Click on this file to download it.


    Where are downloaded files saved

    After downloading the file, it should be in the "Downloaded" directory on your computer, unless you have changed the default download settings in your browser.


    How to find the path to the binary file

    To find the path to the binary file, open the "Downloaded" directory and find the downloaded file. Right-click on the file and select "Properties". In the "Location" section, you will find the full path to the file.

    Provide this path when flashing the ESP8266, replacing "path_to_binary_file" in step 5 of my previous instructions.

    If you still encounter problems, please let me know! I will be happy to help you through the process.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • Helpful post
    #15 20728945
    Menuet
    Level 19  
    Posts: 162
    Help: 39
    Rate: 121
    Board Language: polish
    Hi,
    Bot, bot, but isn't it sometimes the case that by chance the resolution of the temperature sensor has changed? It is configurable if I am not mistaken from 9 to 12 bits. The default after coming out of the factory is 12 bits.
    Maybe connect another sensor and check or change the resolution by software.
    Greetings,
    Menuet
  • ADVERTISEMENT
  • #16 20728991
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    I do not have a second sensor. how to change it
  • Helpful post
    #17 20729011
    Menuet
    Level 19  
    Posts: 162
    Help: 39
    Rate: 121
    Board Language: polish
    According to the "dallastemperature" library information, the function to change the resolution looks like this:
    // set resolution of all devices to 9, 10, 11, or 12 bits
    // if new resolution is out of range, it is constrained.
    void DallasTemperature::setResolution(uint8_t newResolution);


    So in your case the initialisation of the library and sensor should look like this:
    DS18B20.begin();
    DS18B20.setResolution(12);


    It should work, if this is where the problem is :)

    Greetings,
    Menuet
  • #18 20729020
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    Lord you have saved lives works great thanks
  • #19 20729024
    slowik359
    Level 4  
    Posts: 32
    Board Language: polish
    thanks to Menuet

Topic summary

✨ The discussion revolves around the issue of inaccurate temperature readings from a DS18B20 sensor connected to an ESP8266 module, where values are displayed as rounded to .00 or .50 instead of precise decimal values. Initial troubleshooting suggested that the problem might stem from the rounding function or the conversion of floating-point numbers to strings. The user provided their code, which was confirmed to be correct, but it was noted that the `println()` function used for displaying the temperature only prints two decimal places, leading to the observed rounding. A solution was proposed to use the `dtostrf()` function to convert the float to a string with specified precision. Further investigation revealed that the issue persisted even when tested with a different microcontroller, suggesting a potential fault with the DS18B20 sensor. The user was advised to check the sensor's wiring, power supply, and pull-up resistor. Additionally, it was suggested to re-flash the ESP8266 firmware if issues continued. Ultimately, the resolution involved changing the sensor's resolution setting using the DallasTemperature library, which successfully restored accurate temperature readings.
Generated by the language model.

FAQ

TL;DR: If your DS18B20 shows 28.50 instead of 28.18, its resolution likely changed (9–12-bit range). "Configurable from 9 to 12 bits." Fix by calling setResolution(12) and re-reading; format output with dtostrf for clean decimals. [Elektroda, Menuet, post #20728945]

Why it matters: This FAQ is for ESP8266/Arduino users fixing DS18B20 readings stuck at 0.5°C steps and messy OLED decimals.

Quick Facts

  • DS18B20 offers 9–12-bit resolution; 12-bit step is 0.0625°C, 9-bit is 0.5°C. [DS18B20 Datasheet]
  • Typical accuracy ±0.5°C from −10°C to +85°C; plan calibration accordingly. [DS18B20 Datasheet]
  • 12-bit conversion ~750 ms; 9-bit ~94 ms—tune loop delays. [DS18B20 Datasheet]
  • Restore precision on ESP8266 by calling DallasTemperature.setResolution(12) after begin(). [Elektroda, Menuet, post #20729011]
  • Use dtostrf(temp, 4, 2, buf) to format floats for SSD1306 or Blynk. [Elektroda, ElektrodaBot, post #20728014]

Why does my ESP8266 show temperatures like 28.50, 34.00, 25.50 instead of exact decimals?

This pattern indicates the DS18B20 is at 9-bit resolution, which yields 0.5°C increments. Set 12-bit to restore 0.0625°C steps. "The DS18B20 provides user-selectable resolutions of 9 to 12 bits." Rerun conversions after changing resolution. [DS18B20 Datasheet]

How do I force 12-bit resolution using DallasTemperature?

Add this after begin(): 1. DS18B20.begin(); 2. DS18B20.setResolution(12); 3. requestTemperatures(); then read. This restores fine readings, as shown in the thread’s solution path. [Elektroda, Menuet, post #20729011]

Do I need to reflash the ESP8266 to fix the .50 readings?

No. Reflashing does not change the DS18B20’s resolution. In the thread, setting setResolution(12) immediately fixed the issue. Focus on sensor configuration, not firmware. [Elektroda, slowik359, post #20729020]

How can I print exact decimal places on an SSD1306 OLED?

Format the float before printing. Use dtostrf(temp, 4, 2, buf); then display.println(buf). This avoids println’s default float handling and gives consistent two decimals on-screen. [Elektroda, ElektrodaBot, post #20728014]

How can I tell what resolution the DS18B20 is using?

Observe step size while temperatures change. 0.5°C steps indicate 9-bit, 0.0625°C indicates 12-bit. You can also read the scratchpad configuration register and decode R0–R1 to determine resolution. [DS18B20 Datasheet]

What pull-up resistor and wiring should I use for reliable readings?

Use a 4.7 kΩ pull-up on the 1-Wire data line. Keep wiring reasonable or apply proper bus topology for longer runs. Parasitic power and weak pull-up can cause unstable readings, though 0.5°C steps point to resolution, not noise. [DS18B20 Datasheet]

Does higher resolution slow down conversions?

Yes. 12-bit conversions take about 750 ms, while 9-bit takes about 94 ms. Plan delays accordingly on ESP8266 or Arduino to balance responsiveness and precision. [DS18B20 Datasheet]

What accuracy should I expect from the DS18B20?

Expect ±0.5°C typical accuracy between −10°C and +85°C. Outside that span, error rises. Resolution improves granularity, not absolute accuracy, so calibrate if tighter specs are required. [DS18B20 Datasheet]

I read 85.0°C after power-up. What does that mean?

85°C is the power-on default scratchpad value before the first valid conversion. Trigger a conversion and read again to obtain the real temperature. This is a normal edge case. [DS18B20 Datasheet]

Will Blynk or sprintf/println cause only .50 or .00 readings?

No. Formatting affects visible decimals, not the sensor’s step size. Only DS18B20 resolution determines 0.5°C versus 0.0625°C increments. Set 12-bit, then format output as needed. [Elektroda, Menuet, post #20728945]

What minimal test isolates the sensor from display or app code?

Print to Serial only: requestTemperatures(); then Serial.println(DS18B20.getTempCByIndex(0), 4); If needed, convert with dtostrf and print the buffer. This separates sensor reading from display rounding effects. [Elektroda, ElektrodaBot, post #20728014]

Do I need to set the resolution on every boot?

Yes, set it in setup() after begin() for consistency. The thread’s fix placed setResolution(12) right after DS18B20.begin(), ensuring stable precision on each restart. [Elektroda, Menuet, post #20729011]
Generated by the language model.
ADVERTISEMENT