Arduino Mega - Temperature Sensor - LCD

This guide shows you how to use an Arduino Mega to read temperature from the DS18B20 one-wire sensor and show it on an I2C LCD display.

Arduino Mega Temperature Sensor LCD I2C

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×LCD I2C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Temperature Sensor and LCD

If you're new to using the Temperature Sensor, LCD, and Arduino Mega, please check out these tutorials:

These tutorials explain how Temperature Sensor and LCD work, their pinouts, how to connect them to the Arduino Mega, and how to program Arduino Mega to work with the Temperature Sensor and LCD.

Wiring Diagram

The wiring diagram between Arduino Mega Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-temperature-sensor-lcd */ #include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal_I2C.h> #define SENSOR_PIN 2 // The Arduino UNO R4 pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows float temperature_C; // temperature in Celsius float temperature_F; // temperature in Fahrenheit void setup() { DS18B20.begin(); // initialize the sensor lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius temperature_F = temperature_C * 9 / 5 + 32; // convert Celsius to Fahrenheit lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print(temperature_C); // print the temperature in Celsius lcd.print((char)223); // print ° character lcd.print("C"); lcd.setCursor(0, 1); // start to print at the second row lcd.print(temperature_F); // print the temperature in Fahrenheit lcd.print((char)223); // print ° character lcd.print("F"); delay(500); }

※ NOTE THAT:

The LCD’s I2C address can be different depending on who makes it. In our code we used 0x27, which is the address given by the manufacturer DIYables: https://diyables.io/products/lcd-i2c-16x2.

Detailed Instructions

Do these steps in order:

  • Connect the Arduino Mega to the DS18B20 temperature sensor and the LCD I2C, as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the right board (for example, Arduino Mega) and the serial port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Search for DallasTemperature and find the library by Miles Burton.
  • Click Install to add the DallasTemperature library.
Arduino Mega Dallas Temperature library
  • You need to install a library the project needs.
  • Click the Install All button to install the OneWire library.
Arduino Mega onewire library
  • Look for "LiquidCrystal I2C" and find the LiquidCrystal_I2C library by Frank de Brabander.
  • Click the Install button to install the LiquidCrystal_I2C library.
Arduino Mega LiquidCrystal I2C library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
Arduino IDE Upload Code
  • Put the sensor in hot or cold water, or hold it in your hand.
  • Look at the screen to see the reading.

If the screen is blank, check the LCD I2C troubleshooting guide: LCD I2C troubleshooting.

Code Explanation

Look at the notes in the code comments to understand what each line does.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!