• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

esp32 Module 5, Unit 5: DS18B20 calibration

Q&A Forum › Category: ESP32 › esp32 Module 5, Unit 5: DS18B20 calibration
0 Vote Up Vote Down
Jack Douglas asked 5 years ago

My temperature readings from the DS18B20 sensor is off by a large amount.  How can it be calibrated?

Question Tags: ESP32
11 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Jack.
To calibrate the sensor, you need to measure something with known temperature and check it against the value you get.
The easiest things are ice (0ºC) and boiling water (100ºC).
Take a look at this article that may help you with that:https://www.instructables.com/id/Calibration-of-DS18B20-Sensor-With-Arduino-UNO/
Regards,
Sara  

0 Vote Up Vote Down
Jack Douglas answered 5 years ago

Then how do you incorporate that information into the code, so the output is corrected?

In other words, when we work with sensors in general, the code needs to include a means for approximating a standard curve, ie, a “reading” vs voltage or current.

Sometime a standard curve is not linear, so ideally there would be a means to use multiple samples or use a function other than y=mx+b.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Jack.
I was thinking that y=mx+b is the easiest way to go. Unless you have other means to calibrate the sensor. With the water reference you’ll only get to values to adjust a calibrating function.
If you have another calibrated temperature sensor or a thermometer that you can compare the readings with, you can get a better adjusted function.
For example, there are software that adjusts a mathematical function to your readings, so you can use it to get the right temperature readings (you can do that with excel for example).
But I guess that the real issue is that usually people don’t have a calibrated source to use as reference.
Regards,
Sara

0 Vote Up Vote Down
Jack Douglas answered 5 years ago

I was thinking ahead in terms of calibrating other sensors, such as gas sensors that might be curvilinear. For temp I will stick with y=mx+b.
Regardless, back to my question, I still do not understand how/where you insert the two calibration data points into the DS18B20 code?

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
I’m not really sure that I understood your doubt. But here’s the steps:
First, you need to run the code to measure known temperatures. Get several data points and calculate the average value.
For example, imagine that when you measure the following temperatures, the mean values are:

  • 0ºC(real value): 3 ºC (value measured with the sensor)
  • 100ºC (real value): 95 ºC (value measured with the sensor)

Your function we’ll be something as follows:
y = 1.087x – 3.26
Where y is the real value and x is the value you get from the sensor.
So, then, you can incorporate that equation in your projects.
For example:

float temperature = sensors.getTempCByIndex(0);
float realTemperature = 1.087*temperature - 3.26;

I hope you understand.
Regards,
Sara
 
 
 

0 Vote Up Vote Down
Jack Douglas answered 5 years ago

This makes sense, thanks.  I asked because I don’t see this in any of the code in the esp32 book for Module 4, Units 8 and 11 or Module 5, Units 3-5.   In fact, I didn’t see any discussion of ‘calibration’ for any of the sensors.  Perhaps you might consider adding this in the next version?
 
By the way, I also see the use of “readTemp” and “getTemp”.  How do they differ and when to use either?

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

You’re right, we don’t have any content about calibrating sensors.
Regarding the functions used, it depends on which sensor you are using. To interface with each sensor, you use different libraries. So, you use the functions provided by the library.
the readTemperature() is used with the BME sensor and the getTemp is used with the DS18B20.
Regards,
Sara
 

0 Vote Up Vote Down
Jack Douglas answered 5 years ago

Thanks Sara  ;   )

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question.
Regards,
Sara

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

In ice:  0°C = -127 C
In boiling water = -127
3.3 v
Wire sda    gpio 21

 

Start your code here

#include “Arduino.h”
#include <OneWire.h>
#include <DallasTemperature.h>

const int oneWireBus = 21;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

void setup() {
Serial.begin(115200);

}

void loop() {

sensors.requestTemperatures();

Serial.println(String(sensors.getTempCByIndex(0)));

delay(200);
}

 

It’s no better on pin 32

Regards

 

JPD

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.

The -127 is an error that usually happens when the sensor was not able to get readings.
Many times it is caused by “bad” wires or wrong connections. Additionally, if the wires are very long, it can also be an issue.

If the wiring is correct, you can try to add an if statement in your code that checks if the temperature is -127. If it is, try to get another reading.

You may also need to add a bigger delay time between readings in your loop.

Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.