DS18B20 Temperature Module: The temperature module is used to sense the temperature at a much accurate level than the other temperature sensors. The temperature module consists of a one wire temperature sensor (DS18B20) also known as (Dallas). This module can be used with any if the microcontroller like raspberry pi and Arduino but we are using the Arduino. The temperature module has wide range accuracy. It is accurate up to ± 0.5 ᴼC over the range of -10 ᴼC to +85 ᴼC.
You can locate these sensors at a much larger distance from your Arduino, almost 100 meter away from your Arduino. If you will use this sensor alone, then you will have to use a 5 K ohm pull up resistor but in our module you do not need to use it. It has built in pull up resistor in it.It has a resolution of 12 bit. It gets powers up in a low power idle state. To measure the temperature and to do the A to D conversion, it should get a convert T [ 44h ] command from the master. After the conversion, the data will be stored in the 2-byte temperature register and the sensor will return to idle state.
Pin out of DS18B20 Temperature Module
The DS18B20 temperature module has only three pins so it is very easy to use but it highly accurate as compared to other sensors.
The three pins of the Temperature module are
- Ground : This will be connected to the ground of Arduino
- VCC : This will be connected to the 5v of Arduino
- Signal : This will be connected to any digital pin of Arduino.
Connection of DS18B20 Temperature Module with arduino
The connection scheme is very easier. We have only three pins to connect. Connect the temperature module to the Arduino as shown in the figure.
Installing the Library for DS18B20 Temperature Module code
Before uploading the code you will have to install the library of the DS18B20 temperature sensor.
Download the library from here and place it in your Arduino library folder.
Copy the file from the ZIP folder and place it in the library folder.
After placing the files in the library folder, the Arduino library folder should have new folders named onewire and Dallas temperature containing. After that copy the following code in the Arduino IDE and upload the code.
Code of DS18B20 Temperature Module with arduino
// This code is for the DS1820 Temperature module.
// Do not forget to install library before running the code.
#include < OneWire.h > // Including the library of DS1820 Temperature module #include < DallasTemperature.h > // Including the library of DS1820 Temperature module #define ONE_WIRE_BUS 2 // Initializing the Arduino pin 2 for temperature module OneWire ourWire(ONE_WIRE_BUS); // Declaring a variable named our wire DallasTemperature sensors ( &ourWire ) ; // Asking the Dallas temperature library to use the one wire library void setup ( ) // Void setup runs only one. So the code written in it will run only one time { Delay ( 1000 ) ; // Wait for one second Serial.begin ( 9600 ) ; // Setting the baud rate at 9600 Serial.println ( " Microcontrollerlab.com : This is the test code " ) ; Serial.println ( " Temperature Sensor : DS18B20 " ) ; Delay ( 1000 ) ; // Wait for one second sensors.begin ( ) ; // The sensor will start working here void loop ( ) // Void loop runs repeatedly. So the code written in it will run repeatedly { Serial.println ( ) ; // This will give some space in the output Serial.print ( " Waiting for the temperature module to give value ... " ) ; // This will print “Waiting for the temperature module to give value … ” on the display. sensors.requestTemperatures ( ) ; // Sending the commands to get the temperature values from sensor Serial.println ( " DONE " ) ; // This will print “ done “ on the display Serial.print ( " Temperature in degree C is : " ) ; // This will print " Temperature in degree C is :" on the display Serial.print ( sensors.getTempCByIndex ( 0 ) ) ; // This will show the temperature in degree C on the display Serial.println ( " Degrees C " ) ; // This will print " Degrees C " on the display Serial.print ( " Temperature in degree Fahrenheit is : " ) ; // This will print " Temperature in degree Fahrenheit is : " on display Serial.print ( sensors.getTempFByIndex ( 0 ) ) ; // This will show the temperature in Fahrenheit on display Serial.println ( " Degrees F " ) ; // This will print " Degrees F " on the display Delay ( 5000 ) ; // Waiting for 5 seconds. }
If you sensor is working alright then the output should look like this
microcontrollerslab.com: This is the test code Temperature Sensor: DS18B20 Waiting for the temperature module to give value ... DONE Temperature in degree C is : 19.12 Degrees C Temperature in degree Fahrenheit is : 60.22 Degrees F
While if you are seeing this on the output then there is a connection problem or sensor problem.
microcontrollerslab.com: This is the test code Temperature Sensor: DS18B20 Waiting for the temperature module to give value ... DONE Temperature in degree C is : -127.00 Degrees C Temperature in degree Fahrenheit is : -196.00 Degrees F
you can also add feature of gsm interfacing in this article to make it wireless temperature sensor. This is all about using DS18B20 Temperature sensor and its code. If you have any issue after reading this article comment on this post with your issues. We will try our best to resolve your issue.
Excellent explanation, for us neophytes it is very pleasant to find this type of invaluable help that with their efforts helps us to better understand this world of the Arduino. I am trying to make a program with FreeRTOS to sense the temperature with the same DS18B20, Arduino uno, LCD I2C, and two tasks; one for the sensor and one for the display. I have problems because it tells me that I have too many libraries and it does not compile for UNO, I must have compiled for MEGA 2560. I appreciate if you can help me.
Check this FreeRTOS tutorial
http://microcontrollerslab.com/arduino-freertos-queues-create-read-write-examples/