Arduino Nano - Temperature Sensor - LCD

This tutorial instructs you how to use Arduino Nano to acquire the temperature from a DS18B20 sensor and show it on an LCD I2C display.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×LCD I2C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

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 are not familiar with temperature sensor and LCD (pinout, how it works, how to program ...), the following tutorials can help you:

Wiring Diagram

  • Wiring diagram using a breadboard.
The wiring diagram between Arduino Nano and Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram using a terminal adapter (recommended).
The wiring diagram between Arduino Nano and DS18B20 Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring.

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-temperature-sensor-lcd */ #include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal_I2C.h> #define SENSOR_PIN 2 // The Arduino Nano 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 I2C display 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 address of the LCD may differ depending on the manufacturer. In the code, we have used 0x27 as specified by DIYables manufacturer.

Detailed Instructions

  • Plug the USB cable into the Arduino Nano and your PC.
  • Launch the Arduino IDE, then select the appropriate board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Dallas”, then locate the DallasTemperature library created by Miles Burton.
  • Press the Install button to install the DallasTemperature library.
Arduino Nano Dallas Temperature library
  • You will be asked to install the dependency. Click Install All button to install OneWire library.
Arduino Nano onewire library
  • Search for “LiquidCrystal I2C” and locate the LiquidCrystal_I2C library by Frank de Brabander.
  • Then, click the Install button to install the library.
Arduino Nano LiquidCrystal I2C library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
Arduino IDE Upload Code
  • Attach the sensor to both hot and cold water, or hold it in your hand.
  • Check the LCD for the results.

If the LCD does not show anything, . check out Troubleshooting on LCD I2C . for guidance.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial