DS18B20 Based Thermometer using Arduino & 4 Digits 7 Segment Display:
In this project, we will learn how to design a 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 a sealed package lets precisely measure temperatures in wet environments with a simple 1-Wire interface. It communicates on a common bus. It means it can connect several devices and read their values using just one digital pin of the Arduino.
Check some of the posts related to DS18B20 here:
1. Temperature Meter using DS18B20 OLED Display & Arduino
2. Digital Thermometer Using Arduino & DS18B20 Temperature Sensor
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.
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 the 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 the long wire is used for measuring the temperature at a certain distance, wire length can deviate the value in analog LM35 Sensor. But DS18B20 is a digital sensor, there is no effect 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:
- Arduino UNO Board
- DS18B20 Waterproof Temperature Sensor
- Common Anode 7 Segment Display – 4
- 7HC595 IC (8 bit Shift Resistor)
- 4.7K Resistor
- 330 ohm Resistor – 8
- Breadboard
- Connecting Jumper Wires
Circuit Diagram & Connections:
Connect the 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 another 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:
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 the library file to Arduino IDE and then simply compile and upload the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
#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 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)); } 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:
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 the library file to Arduino IDE and then simply compile and upload the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#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)); } 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); } |
7 Comments
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.
I have already explained this here. You can check https://www.how2electronics.com/interfacing-multiple-ds18b20-temperature-sensors/
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.
Hola Alex quiero montar tu circuito termómetro pero sustituir los 4 displays por 4 displays de tira de led , por internet encontré un circuito que utiliza el 74HC595 y el ULN2003 para cada segmentos A-B-C-D-E-F-G de la tira de led , como puedo adaptar el ULN2003 a tu circuito Arduino necesitaría 3 ULN2003 ahí que modificar el programa que tu as montando , me puedes ayudar .
Hola monte tu circuito pero no me funciona bien me marcan los display 8 fijo , el código del programa esta bien ,como lo puedo solucionar revise el circuito y esta todo bien montado , pienso que es algo del programa me puedes ayudar te dejo mi correo [email protected]
did you achieve? I’m also working on it, I want to know if it can be adapted
Hello Mr. Alex , My self Mahesh from Bangalore im looking for Ds18b20 Indicator Can you please help me in this.