Interfacing DS18b20 Temperature sensor with Arduino + LCD to measure in Celsius and Fahrenheit
Hello, and welcome to this quick tutorial where I use the digital temperature sensor DS18B20 with an Arduino Board, I’m using the UNO board. The DS18b20 can output the temperature in 9 bit up to 12 bit signal, the DS18B20 communicates over a 1-Wire bus and has a unique 64bits serial code, you can use a lot to create a network using the same wire.
Sensor features (Datasheet):
- Unique 1-Wire® Interface Requires Only One Port Pin for Communication
- Reduce Component Count with Integrated Temperature Sensor and EEPROM
- Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
- ±0.5°C Accuracy from -10°C to +85°C
- Programmable Resolution from 9 Bits to 12 Bits
- No External Components Required
- Parasitic Power Mode Requires Only 2 Pins for Operation (DQ and GND)
- Simplifies Distributed Temperature-Sensing Applications with Multidrop Capability
- Each Device Has a Unique 64-Bit Serial Code Stored in On-Board ROM
- Flexible User-Definable Nonvolatile (NV) Alarm Settings with Alarm Search Command Identifies Devices with Temperatures Outside Programmed Limits
Make sure to check the tutorial video above for more information.
Wiring:
The wiring is pretty simple Vdd/Gnd with 3.3V/Gnd (you can use 5v) and then one wire with a digital pin, LCD uses the i²c bus, therefore the SDA/SCL are wired with A4/A5. And of course the LCD is optional and can be replaced with anything from your choice.

Libraries:
The libraries I’ve used are:
- DS18b20 library:Â Download here from Github or Download Here.
- OneWire library:Â Download here from Github or Download Here.
- LCD i²c library: Download here.
Codes:
The codes I’ve used are 3, Download codes here or check below:
Code 1:
the first one is just a simple test to show the temperature in Celsius or Fahrenheit on the Serial monitor, just comment or uncomment the necessary lines.
|
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 |
/* This code is to use with DS18b20 Temperature sensor * It shows the temperature in °C or °F on the serial monitor */ #include <OneWire.h> #include <DS18B20.h> OneWire oneWire(2); DS18B20 sensor(&oneWire); float f; void setup(void) { Serial.begin(9600); sensor.begin(); } void loop(void) { sensor.requestTemperatures(); while (!sensor.isConversionComplete()); Serial.print("Temperature:"); Serial.print("\t"); // Serial.print(sensor.getTempC()); //Uncomment these lines to print in °C // Serial.println(" °C"); f=(sensor.getTempC() * 1.8) + 32; //°F Comment these three lines if you don't want °F Serial.print(f); Serial.println(" °F"); delay(1000); } |
Code 2:
the second one shows the temperature on the serial monitor but this time using different sensor format 9-12 bits.
|
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 |
/* This code is to use with DS18b20 temperature sensor * It shows the temperature in different bit formats */ #include <OneWire.h> #include <DS18B20.h> OneWire oneWire(2); DS18B20 sensor(&oneWire); float f; void setup(void) { Serial.begin(9600); sensor.begin(); } void loop(void) { for(int r=9 ; r<13 ; r++){ sensor.setResolution(r); sensor.requestTemperatures(); while (!sensor.isConversionComplete()); Serial.print("Temperature:"); Serial.print("\t"); Serial.print(sensor.getTempC()); Serial.print(" °C"); // f=(sensor.getTempC() * 1.8) + 32; // Serial.print(f); // Serial.print(" °F"); Serial.print("\t"); Serial.println(r); } Serial.println(); delay(2000); } |
Code 3:
and the last one concerns the LCD i²c screen, and here you have your thermometer easy peasy as always.
|
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 |
/* This code is to use with DS18b20 temperature sensor and LCD i²c screen * It shows the temperature in °C or °F on the LCD screen * Refer to http://www.SurtrTech.com for more information */ #include <OneWire.h> #include <DS18B20.h> #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here #define BACKLIGHT_PIN 3 // Declaring LCD Pins #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); OneWire oneWire(2); DS18B20 sensor(&oneWire); float f; void setup(void) { sensor.begin(); lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //Lighting backlight lcd.home (); } void loop(void) { sensor.requestTemperatures(); while (!sensor.isConversionComplete()); lcd.print(" Temperature:"); lcd.setCursor(6,1); // lcd.print(sensor.getTempC(),1); // lcd.setCursor(11,1); // lcd.print("C"); f=(sensor.getTempC() * 1.8) + 32; lcd.print(f,1); lcd.setCursor(11,1); lcd.print("F"); delay(1000); lcd.clear(); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.

One thought on “Interfacing DS18b20 Temperature sensor with Arduino + LCD to measure in Celsius and Fahrenheit” Leave a comment ›