How to Display Sensor Data on LCD in Arduino Easily
To display sensor data on an LCD in Arduino, first connect the sensor and LCD to the Arduino board, then use the
LiquidCrystal library to control the LCD. Read the sensor value using analog or digital input, and print the value on the LCD using lcd.print() inside the loop() function.Syntax
Here is the basic syntax to display sensor data on an LCD using Arduino:
#include <LiquidCrystal.h>: Includes the LCD library.LiquidCrystal lcd(rs, en, d4, d5, d6, d7);: Creates an LCD object with pin connections.lcd.begin(cols, rows);: Initializes the LCD size.int sensorValue = analogRead(pin);: Reads sensor data from an analog pin.lcd.setCursor(col, row);: Sets the cursor position on the LCD.lcd.print(value);: Prints text or numbers on the LCD.
arduino
#include <LiquidCrystal.h> // Initialize the library with the numbers of the interface pins LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { lcd.begin(cols, rows); // Set LCD size } void loop() { int sensorValue = analogRead(sensorPin); // Read sensor lcd.setCursor(0, 0); // Set cursor to first column, first row lcd.print("Value: "); lcd.print(sensorValue); // Display sensor value delay(500); // Wait half a second }
Example
This example reads a sensor connected to analog pin A0 and displays its value on a 16x2 LCD screen connected to Arduino pins 12, 11, 5, 4, 3, and 2.
arduino
#include <LiquidCrystal.h> // LCD pin connections: rs=12, en=11, d4=5, d5=4, d6=3, d7=2 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); const int sensorPin = A0; // Analog pin for sensor void setup() { lcd.begin(16, 2); // Initialize 16x2 LCD lcd.print("Sensor Value:"); } void loop() { int sensorValue = analogRead(sensorPin); // Read sensor lcd.setCursor(0, 1); // Move to second row lcd.print(" "); // Clear previous value lcd.setCursor(0, 1); // Reset cursor lcd.print(sensorValue); // Show sensor value delay(500); // Update every 0.5 seconds }
Output
Sensor Value:
512
Common Pitfalls
- Wrong pin connections: Make sure LCD pins match the pins declared in
LiquidCrystal lcd(). - Not initializing LCD size: Always call
lcd.begin(cols, rows)insetup(). - Not clearing old data: Overlapping prints can cause messy display; clear or overwrite old text.
- Reading wrong sensor pin: Confirm sensor is connected to the correct analog or digital pin.
arduino
// Wrong way (no lcd.begin): #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // lcd.begin(16, 2); // Missing initialization } void loop() { lcd.print("Hello"); // Won't display properly } // Right way: #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); // Initialize LCD } void loop() { lcd.print("Hello"); // Displays correctly }
Quick Reference
Remember these key points when displaying sensor data on an LCD with Arduino:
- Use
LiquidCrystallibrary for LCD control. - Initialize LCD size with
lcd.begin(). - Read sensor data with
analogRead()ordigitalRead(). - Set cursor position before printing with
lcd.setCursor(). - Clear or overwrite old LCD text to avoid clutter.
Key Takeaways
Connect your sensor and LCD pins correctly and declare them in code.
Use the LiquidCrystal library and call lcd.begin() to initialize the LCD.
Read sensor values with analogRead() and display them using lcd.print().
Set the cursor position with lcd.setCursor() before printing to control text placement.
Clear or overwrite old LCD text to keep the display clean and readable.