My temperature readings from the DS18B20 sensor is off by a large amount. How can it be calibrated?
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
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.
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
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?
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
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.
Regards,
Sara
Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question.
Regards,
Sara
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
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