Introduction: Calibration of DS18B20 Sensor With Arduino UNO
DISCLAIMER:
The device you see in the pictures is used in another project as a Thermostat for film developing process. You can find that project here. In order to calibrate a sensor, or more than one, you'll need just what you'll find in this project, nothing more, and it's pretty basic, too! Let's go!
Step 1: Prepare Your Device
Here is a list of what you need:
- Arduino UNO (or MEGA)
- DS18B20 sensor(s)
- 4kOhm - 5kOhm resistance (I used a 5k1Ohm)
- LCD screen to read values (you can also use a laptop and just read them on a serial monitor)
- A sketch that use the sensor and show somehow the values
First of all you have to connect your modules and sensor to your controller. I'll leave the complicated part of the LCD for you to search the web, and I will just tell you how to connect the sensor.
Usually those sensors come with three coloured wires: Black, Red, Yellow. The first two are for energy and the third is for the data. Connect the black to GNN, the red to Vcc (5V) and the yellow on an analogue input, let's say A0.
Now connect the resistance between the yellow and the red to complete the connections.
Plug in also the LCD (I suggest a simple 16x2 LCD with i2c connection to use just 4 wires total) and you're done with wires and cables.
Now the sketch that is super simple:
#include "OneWire.h" #include "DallasTemperature.h" #define ONE_WIRE_BUS_1 A0 OneWire ourWire1(ONE_WIRE_BUS_1); DallasTemperature sensor1(&ourWire1); #include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27,16,2); float RawValue =0;
void setup(){ lcd.init(); lcd.backlight(); sensor1.begin(); sensor1.setResolution(11); } void loop(){ sensor1.requestTemperatures(); float RawValue = sensor1.getTempCByIndex(0); lcd.setCursor(0,0); lcd.print("Sens. 1 "); lcd.print(RawValue, 1); }
As you can see we use the Dallas Temperature library and a LCD screen with i2c connection.
In the setup we iniziate LCD and sensor and in the loop we simply request the temperature and store the value inside the variable RawValue to show it on the LCD.
If you want to keep it more simple, just use the serial monitor with the following sketch
#include "Wire.h"
#include "OneWire.h" #include "DallasTemperature.h" #define ONE_WIRE_BUS_1 A0 OneWire ourWire1(ONE_WIRE_BUS_1); DallasTemperature sensor1(&ourWire1);float RawValue =0; void setup(){
delay(1000);
Serial.begin(9600);
sensor1.begin();
sensor1.setResolution(11);} void loop(){ sensor1.requestTemperatures(); float RawValue = sensor1.getTempCByIndex(0); Serial.print("Sens. 1 "); Serial.println(RawValue, 1); }
Now follow me in the core of the project to calibrate the sensor.
Step 2: Two Point Calibration
Something to know first
To calibrate a thermo-sensor, you have to measure something of which you know the temperature. The simple way to do it at home is using boiling water and a bath of melting ice, also called a "triple-point" bath. In those cases we know that water boils at 100°C on the sea level. Keep in mind that to make a precise measurement you should know your altitude and calculate the proper boiling temperature there.
You can check it here!
To be honest you should check the atmospheric pressure and not the altitude. But that way is accurate enough.
The triple-point bath, or ice bath, is the temperature at which water exists in the three states solid, liquid and gas, that temperature is 0,01°C. We will use, to simplify, 0°C.
Knowing the value that the sensor read and the value that should be, we can modify the raw value of the DS18B20 into something more correct.
NOTE: you could also use more temperature to calibrate the sensor just putting it in some other substance of which you know the boiling point like Ether (35°C), Pentane (36,1°C), Acetone (56°C) or Ethanol (78,37°C), but those boiling substances produce high inflammable gasses! SoDon't do it!
Boiling Water:
Put some water in a pot and heat it up until it boils (bubbles of gas are developing and the water is agitating itself). Immerge your sensor where it does not touch anything but water. Wait a couple of minutes and read the lcd or the serial monitor.
The temperature should remain the same for at least one minute. If so, write that value down. That is your: RawHigh value.
Triple-point bath:
Now take a big glass (you don't need anything huge nor a pot) and fill it to the border with ice cubes. Try to use small sized ice cubes. Now fill the 80% of the glass with cold water. Refill with ice if the lever try to go down.
Now put your sensor inside the water/ice thing and wait one and a half minute. Read the temperature that should remain the same for 30 seconds at least. If so, write it down, that it your RawLow value.
Step 3: Use the Values You Get in the Right Way!
So, now you got some important values:
- RawHigh
- RawLow
- ReferenceHigh
- ReferenceLow
The references value are obviously 99.9°C for the boiling water (at my altitude of 22m), and 0°C for the melting ice bath. Now calculate the ranges fo those values:
- RawRange = RawHigh - RawLow
- ReferenceRange = ReferenceHigh - ReferenceLow
Now you're all set to use that sensor in any other project being sure that it will give you a right measurement. How? Using the value you got here in the project you will create with that sensor.
In your future project you'll have to use the values you read in this one and I suggest to do it using the same names I used here.
Declare the variables before the void setup() section just like this:
float RawHigh = 99.6;
float RawLow = 0.5;
float ReferenceHigh = 99.9;
float ReferenceLow = 0;
float RawRange = RawHigh - RawLow;
float ReferenceRange = ReferenceHigh - ReferenceLow;
Than, every time you will use the sensor, you can use the following formula to calculate the CorrectedValue:
float CorrectedValue = (((RawValue - RawLow) * ReferenceRange) / RawRange) + ReferenceLow;
RawValue is obviously the reading of the sensor.
That's it!
Now you know how to calibrate your DS18B20 sensor or any other sensor that you'll use! Have fun!