How to Display Temperature on LCD with Arduino
To display temperature on an LCD with Arduino, connect a temperature sensor like the LM35 to an analog pin and an LCD to digital pins. Use the
LiquidCrystal library to control the LCD and read the sensor value with analogRead(), then convert it to temperature and show it on the LCD using lcd.print().Syntax
The basic syntax involves initializing the LCD, reading the sensor value, converting it to temperature, and printing it on the LCD.
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);: Creates an LCD object with pin connections.lcd.begin(cols, rows);: Sets LCD size.analogRead(pin);: Reads analog sensor value.lcd.print(value);: Prints text or numbers on LCD.
arduino
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); lcd.begin(16, 2); int sensorValue = analogRead(A0); lcd.print(sensorValue);
Output
Displays the sensor value number on the LCD screen.
Example
This example reads temperature from an LM35 sensor connected to analog pin A0 and displays the temperature in Celsius on a 16x2 LCD.
arduino
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); const int tempPin = A0; void setup() { lcd.begin(16, 2); lcd.print("Temp: "); } void loop() { int sensorValue = analogRead(tempPin); float voltage = sensorValue * (5.0 / 1023.0); float temperatureC = voltage * 100.0; // LM35 outputs 10mV per degree Celsius lcd.setCursor(6, 0); lcd.print(temperatureC, 1); // print with 1 decimal place lcd.print(" C "); // clear leftover chars delay(1000); }
Output
Temp: 23.4 C (example temperature shown on LCD)
Common Pitfalls
Common mistakes include:
- Not initializing the LCD with
lcd.begin(). - Forgetting to set the cursor before printing new values, causing overlapping text.
- Incorrect sensor voltage to temperature conversion.
- Not clearing leftover characters on LCD when new temperature has fewer digits.
autocad
/* Wrong way: No cursor set, leftover chars remain */ lcd.print(temperatureC); /* Right way: Set cursor and clear leftover chars */ lcd.setCursor(6, 0); lcd.print(temperatureC, 1); lcd.print(" C ");
Quick Reference
| Function/Method | Purpose |
|---|---|
| LiquidCrystal lcd(rs, en, d4, d5, d6, d7) | Create LCD object with pin numbers |
| lcd.begin(cols, rows) | Initialize LCD size |
| analogRead(pin) | Read analog sensor value |
| lcd.setCursor(col, row) | Set cursor position on LCD |
| lcd.print(value) | Print text or numbers on LCD |
| delay(ms) | Pause program for ms milliseconds |
Key Takeaways
Use the LiquidCrystal library to control the LCD display.
Read temperature sensor analog values and convert them to Celsius correctly.
Always set the LCD cursor before printing new data to avoid overlapping text.
Clear leftover characters on the LCD when updating displayed values.
Use delay to update the temperature reading at a readable pace.