My temperature readings from the DS18B20 sensor is off by a large amount. How can it be calibrated?
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/
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.
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.
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?
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.
float temperature = sensors.getTempCByIndex(0);
float realTemperature = 1.087*temperature - 3.26;
I hope you understand.
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?
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.