DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display

DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display (Last Updated On: February 22, 2019)

DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display:

In this project we will learn how to design DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display. Simply we will interface Arduino with DS18B20 Digital Waterproof Temperature Sensor and display the temperature values in degree celsius on Common Cathode Display.

The DS18B20 temperature sensor is a 1-wire digital temperature sensor. This comes with sealed package lets precisely measure temperatures in wet environments with a simple 1-Wire interface. It communicates on common bus. It means it can connect several devices and read their values using just one digital pin of the Arduino.


DS18B20 Waterproof Digital Temperature Sensor:

This is a pre-wired and waterproofed version of the DS18B20 sensor. Handy for when you need to measure something far away, or in wet conditions. The Sensor can measure the temperature between -55 to 125°C (-67°F to +257°F). The cable is jacketed in PVC.


Because it is digital, there is no any signal degradation even over long distances. These 1-wire digital temperature sensors are fairly precise, i.e ±0.5°C over much of the range. It can give up to 12 bits of precision from the onboard digital-to-analog converter. They work great with any microcontroller using a single digital pin.

Digital Thermometer Using Arduino & DS18B20 Temperature Sensor

The only downside is they use the Dallas 1-Wire protocol, which is somewhat complex, and requires a bunch of code to parse out the communication. We toss in a 4.7k resistor, which is required as a pullup from the DATA to VCC line when using the sensor.


LM35 vs DS18B20:

A DS18B20 is factory calibrated to output the right temperature. An LM35 is factory calibrated for voltage (not temperature), and the Arduino has to convert this to temperature.

LM35 is an analog temperature sensor, so any distortion in input can rapidly affect the reading. But DS18B20 is a digital temperature sensor, so input doesn’t affect the output reading.

If long wire is used for measuring temperature at certain distance, wire length can deviate the value in analog LM35 Sensor. But DS18B20 is a digital sensor, there is no affect in any output value.



LM35 can measure the temperature between −55°C to 150°C but DS18B20 can measure between −50°C to 125°C.

1 digital output pin of Arduino can be used to connect multiple DS18B20 but it doesn’t happen with LM35.


Components Required for Digital Thermometer:

1. Arduino UNO Board
2. DS18B20 Waterproof Temperature Sensor (Buy Online from Amazon)
3. Common Anode 7 Segment Display - 4
4. 7HC595 IC (8 bit Shift Resistor)
5. 4.7K Resistor
6. 330 ohm Resistor - 8
7. Breadboard
8. Connecting Jumper Wires

Circuit Diagram & Connections:

DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display

Connect VDD pin of DS18B20 to 5V and GND Pin to Ground. Connect its data pin to digital pin 2 of Arduino and also to 4.7K Resistor (Connect other end of 4.7K Resistor to 5V) as shown in the figure below.


Working of the Project:

The DS18B20 Digital Thermometer provides 9 to 12-bit (configurable) temperature readings which indicate the temperature of the device. It communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition it can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

The core functionality of the DS18B20 is its direct-to-digital temperature sensor. The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively. The default resolution at power-up is 12-bit.



Source Code/Program for Displaying Temperature in °Celsius:

DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display

To design DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display& to display temperature in degree Celsius you need two different library
1. Download 1 Wire Library
2. Download Dallas Temperature Library

Add these to library file to Arduino IDE and then simply compile and upload the code.

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
const int ledPin = 13;// LED connected to digital pin 13
const int latchPin = 8;// Pin connected to ST_CP of 74HC595
const int clockPin = 9;// Pin connected to SH_CP of 74HC595
const int dataPin = 10;// Pin connected to DS of 74HC595
const int digitPins[4] = {3,4,5,6}; //pins to control the 4 common anode pins of the display
const byte digit[12] = //seven segment digit bits + blank + minus
{
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111, //9
B00000000, //Blank
B01000000 //-
};
int digitBuffer[4] = {
1};
int digitScan = 0;
int soft_scaler = 0;
float tempC, tempF;
int tmp;
boolean sign = false;
void setup() {
TCCR2A = 0;
TCCR2B = (1<<CS21);
TIMSK2 = (1<<TOIE2);
TCNT2 = 0;
pinMode(ledPin, OUTPUT);
for(int i=0;i<4;i++)
{
pinMode(digitPins[i],OUTPUT);
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
sensors.begin();
sensors.getAddress(insideThermometer, 0);
}
ISR(TIMER2_OVF_vect) {
soft_scaler++;
if(soft_scaler==15)
{
refreshDisplay();
soft_scaler = 0;
}
};
void refreshDisplay()
{
for(byte k=0;k<4;k++)
{
digitalWrite(digitPins[k], LOW);
}
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
digitalWrite(latchPin, HIGH);
delayMicroseconds(50);
digitalWrite(digitPins[digitScan], HIGH);
digitalWrite(latchPin, LOW);
if(digitScan==1)
{
shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | B10000000)); //inserting the dot
}
else
{
shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
}
digitalWrite(latchPin, HIGH);
digitScan++;
if(digitScan>3) digitScan=0;
}
void loop()
{
digitalWrite(ledPin, HIGH);
sensors.requestTemperatures();
tempC = sensors.getTempC(insideThermometer);
tmp = int(tempC*10);
if (tempC < 0){
sign = true;
tmp = abs(tmp);
}
else{
sign = false;
}
if (int(tmp)/1000 == 0){
digitBuffer[3] = 10;
if (sign){
digitBuffer[3] = 11;
}
}
else{
digitBuffer[3] = int(tmp)/1000;
}
if (int(tmp)/1000 == 0 && (int(tmp)%1000)/100 == 0) {
digitBuffer[2] = 10;
if (sign){
digitBuffer[2] = 11;
digitBuffer[3] = 10;
}
}
else{
digitBuffer[2] = (int(tmp)%1000)/100;
}
digitBuffer[1] = (int(tmp)%100)/10;
digitBuffer[0] = (int(tmp)%100)%10;
digitalWrite(ledPin, LOW);
delay(500);
}

Source Code/Program for Displaying Temperature in °Fahrenheit:

DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display

To design DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display& to display temperature in degree Fahrenheit you need two different library
1. Download 1 Wire Library
2. Download Dallas Temperature Library

Add these to library file to Arduino IDE and then simply compile and upload the code.

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

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

const int ledPin = 13;// LED connected to digital pin 13
const int latchPin = 8;//Pin connected to ST_CP of 74HC595
const int clockPin = 9;//Pin connected to SH_CP of 74HC595
const int dataPin = 10;//Pin connected to DS of 74HC595

const int digitPins[4] = {
3,4,5,6}; //pins to control the 4 common anode pins of the display

const byte digit[10] = //seven segment digit bits
{
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111 //9
};

int digitBuffer[4] = {
0};
int digitScan = 0;
int soft_scaler = 0;
float tempC, tempF;
int tmp;

void setup() {
TCCR2A = 0;
TCCR2B = (1<<CS21);
TIMSK2 = (1<<TOIE2);
TCNT2 = 0;

pinMode(ledPin, OUTPUT);
for(int i=0;i<4;i++)
{
pinMode(digitPins[i],OUTPUT);
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

sensors.begin();
sensors.getAddress(insideThermometer, 0);
}

ISR(TIMER2_OVF_vect) {
soft_scaler++;
if(soft_scaler==15)
{
refreshDisplay();
soft_scaler = 0;
}
};

void refreshDisplay()
{
for(byte k=0;k<4;k++)
{
digitalWrite(digitPins[k], LOW);
}
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
digitalWrite(latchPin, HIGH);
delayMicroseconds(400);
digitalWrite(digitPins[digitScan], HIGH);

digitalWrite(latchPin, LOW);
if(digitScan==2)
{
shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | B10000000)); //inserting the dot
}
else
{
shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
}
digitalWrite(latchPin, HIGH);
digitScan++;
if(digitScan>3) digitScan=0;
}

void loop()
{
digitalWrite(ledPin, HIGH);
sensors.requestTemperatures();
tempC = sensors.getTempC(insideThermometer);
tempF = DallasTemperature::toFahrenheit(tempC);
tmp = int(tempF*100);
digitBuffer[3] = tmp/1000;
digitBuffer[2] = (tmp%1000)/100;
digitBuffer[1] = (tmp%100)/10;
digitBuffer[0] = (tmp%100)%10;

digitalWrite(ledPin, LOW);
delay(50);
}

Recommended For You

3 Comments

  1. Thanks for the detailed explanation of the project.
    Could you please help me on how to modify the sketch in order to use 2 sensors (DS18b20) at the same time (mounted in parallel).

    Thanks.

      1. Thanks for answering. Have already checked . But my wish is to combine the 2 projects , ie : interfacing multiple ds18b20 sensors and using Seven Segment display like in this project.
        Thanks again.

Ask A Question .......

Close