How to Measure temperature with Arduino and DS18B20 sensor?

In this example project we will be combining an Arduino and DS18B20 sensor. The DS18B20 is also called 1-wire digital temperature sensor

Arduino and DS18B20 Temperature Sensor The DS18B20 comes in different forms and shapes, so you have plenty of choice when deciding which one works best for you. There are 3 variations available: 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92.

I have used waterproof version as shown below.

ds18b20-waterproof

Note: DS18B20 is quite versatile. It can be powered through the data line (so called “parasite” mode, which requires only 2 wires versus 3 in normal mode), it operates in a 3.0V to 5.5V range, measures Temperatures from -55°C to +125°C (-67°F to +257°F) with and ±0.5°C Accuracy (from -10°C to +85°C). It converts a temperature in 750ms or less to a up to 12 bits value. Another cool feature is that you can connect up to 127 of these sensors in parallel, and read each individual temperature.

Things you need to get Arduino and DS18B20 sensor work:

  1. Arduino IDE to program the code and upload
  2. OneWire and DallasTemperatre library for the Arduino and DS18B20
  3. One DS18B20 digital temperature sensor
  4. Arduino UNO R3
  5. Jumper wires
  6. Breadboard/PC/General purpose board
  7. Arduino UNO cable

Below is the schematic diagram for the same.

Schemaic_arduino_ds18b20_temperature_sensor

 

 

Step 2: Installing and loading OneWire and DallasTemperature Library

Unzip the downloaded zip file. Make sure that folder name is OneWire, which contains the library. Drag it into the Library folder of Arduino IDE. Alternatively you can use Sketch-> Import Library -> Add Library option of Arduino IDE and select the Zip file.

Step3: Writing code and uploading

#include<OneWire.h>
#include<DallasTemperature.h>

// Data wire is plugged into digital pin2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
void setup(void)
{
 Serial.begin(9600);
 Serial.println("Temperature Demo");
 sensors.begin();
}

void loop()
{
 Serial.print(" Fetching temperature...");
 sensors.requestTemperatures(); // send command to get temperatures
 Serial.println("Done..");
 Serial.println("Temperature is ");
 Serial.println(sensors.getTempCByIndex(0));
 delay(1000);
}


Output will be shown as follows:

Arduino DS18b20 - Output

Screenshot of the above example:

Arduino and a DS18B20 sensor 1

We can modify this to display it in LCD display. For details on how to display the temperature on LCD display visit my post How to connect 16*2 LCD display Arduino UNO

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.