One of the easiest and cheap way to add temperature sensor in Arduino project is to use DS18B20 1-Wire Temperature Sensor. This sensor is very easy to use and needs no external components to work. So, by using this sensor and few connections some Arduino code you’ll be sensing temperature in no time!
DS18B20 1-Wire Temperature Sensor
DS18B20 is 1-Wire interface Temperature sensor requires only one digital pin for two way communication with Arduino.
DS18B20 is 1-Wire interface Temperature sensor requires only one digital pin for two way communication with Arduino.
DS18B20 temperature sensor is precise, very easy to use and needs no external components to work. It can measure temperatures from -55°C to +125°C with ±0.5°C Accuracy.
User can configure the resolution of the sensor to 9, 10, 11, or 12 bits. However, the default resolution is 12-bit (i.e. 0.0625°C precision).
The power require to the sensor is 3V to 5V and consumes 1mA during active temperature conversions.
Here are the complete specifications:
Power Supply | 3V to 5.5V |
Current Consumption | 1mA |
Temperature Range | -55 to 125°C |
Accuracy | ±0.5°C |
Resolution | 9 to 12 bit (selectable) |
Conversion Time | < 750ms |
Multiple DS18B20 on Single Bus
We can use multiple DS18B20 sensor to a single wire because each DS18B20 has a unique 64-bit serial code. And it is easy to differentiate from one another.
This feature is very useful when you want to control many DS18B20s distributed over a large area.
DS18B20 Sensor Pinout
GND is a ground pin.
DQ is 1-Wire Data Bus should be connected to a digital pin on Arduino.
VDD pin supplies power to sensor from 3.3 to 5V.
Wiring DS18B20 Temperature Sensor to Arduino
Now we understand how DS18B20 Temperature Sensor works. Let’s hook up wires with Arduino.
The connections is very simple. First we connect VDD to 5V and GND to GND to Arduino. Next connect the digital signal pin DQ to the digital pin # 2 on Arduino. Also you need to include the 4.7k pull up resistor between power and signal pin to make stable connection for data transfer.
If you are using the waterproof version of the DS18B20, connect Red wire to 5V, Black wire connects to GND and Yellow wire is data that goes to digital pin # 2 on Arduino. You also need to connect a 4.7K pull up resistor from data to 5V.
Installing Library For DS18B20
The Dallas 1-Wire protocol is complex, and requires some code to do the communication. To hide this complexity we will install DallasTemperature.h library so that we can use simple commands to get temperature readings from the sensor.
To install the library click on this link and download the library https://github.com/matmunk/DS18B20.
After downloading the library go to the Sketch > Include Library > Add .ZIP library. Wait to install the library.
Arduino Code
#include OneWire.h
#include DallasTemperature.h
Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2
Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup(void)
{
sensors.begin(); Start up the library
Serial.begin(9600);
}
void loop(void)
{
Send the command to get temperatures
sensors.requestTemperatures();
print the temperature in Celsius
Serial.print(Temperature );
Serial.print(sensors.getTempCByIndex(0));
Serial.print((char)176);shows degrees character
Serial.print(C );
print the temperature in Fahrenheit
Serial.print((sensors.getTempCByIndex(0) 9.0) 5.0 + 32.0);
Serial.print((char)176);shows degrees character
Serial.println(F);
delay(500);
}
The output of code on serial monitor
Code Explanation
First we include OneWire.h and DallasTemperature.h libraries and defining the Arduino pin in which sensor is connected.
#include<OneWire.h>
#include<DallasTemperature.h>
#define ONE_WIRE_BUS 2
Now we create object for one-wire by passing sensor’s signal pin to its constructor. This object allow us to communicate with any one-wire device. To communicate with sensor, we create object of DallasTemperature library and pass reference of one-wire object as a parameter.
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Once a DallasTemperature object is created, we can issue below simple commands to interact with the sensor.
• begin() function searches for connected sensors on the bus and sets bit resolution (12 bits) for each.
• requestTemperatures() function sends command for all sensors on the bus to perform a temperature conversion.
• getTempCByIndex(deviceIndex) function reads and returns temperature reading from the sensor. deviceIndex is nothing but the location of the sensor on the bus. If you are using only one DS18B20 on the bus, set it to 0.