In embedded systems, Liquid Crystal Displays (LCDs) are widely used for displaying information such as sensor data, real-time clock values, or simple text-based messages. One of the most common LCD modules used with microcontrollers like the PIC16F877A is the 16x2 LCD, which can display 16 characters per line and has 2 rows. This blog post will guide you through interfacing a 16x2 LCD with a PIC16F877A microcontroller to display sensor data, such as temperature from an analog temperature sensor.
Components Required:
- PIC16F877A Microcontroller
- 16x2 LCD Module
- LM35 Temperature Sensor (or any analog sensor)
- 10kΩ Potentiometer (for LCD contrast adjustment)
- 10kΩ Resistor (for sensor pull-down)
- Breadboard and Jumper Wires
- Power Supply (5V)
Circuit Diagram
The LCD module needs to be connected to the PIC16F877A using 8-bit or 4-bit mode. Here, we will use 4-bit mode, which reduces the number of I/O pins required.
LCD Connections:
- RS (Register Select): Connected to RB0.
- EN (Enable): Connected to RB1.
- D4 to D7: Connected to RB2, RB3, RB4, and RB5 respectively (for 4-bit data transmission).
- VSS: Connected to Ground.
- VDD: Connected to 5V.
- VEE: Connected to the wiper of the 10kΩ potentiometer for contrast control.
- RW: Connected to Ground (write mode).
Temperature Sensor (LM35):
- VCC: Connected to 5V.
- GND: Connected to Ground.
- OUT: Connected to AN0 (RA0) on the PIC16F877A (for ADC input).
Potentiometer: Adjust the contrast of the LCD by connecting the potentiometer's middle pin to the VEE pin of the LCD.
Circuit Description:
In this setup, the LM35 temperature sensor is connected to AN0 (RA0) of the PIC16F877A. The sensor outputs an analog voltage corresponding to the ambient temperature, which is fed into the ADC of the microcontroller. The microcontroller converts the analog value to a digital value and then to a readable temperature in degrees Celsius. This temperature is displayed on the 16x2 LCD using the standard 4-bit interface. The potentiometer connected to the LCD adjusts the contrast of the characters displayed on the screen.
Code Overview
The code below initializes the LCD and ADC, reads analog sensor data from the LM35, converts it to temperature, and displays the temperature on the LCD.
Code:
Explanation:
- LCD Initialization: The
Lcd_Init()
function initializes the 16x2 LCD in 4-bit mode. It sets up the display to show two lines and hides the cursor. - ADC Setup: The
ADC_Init()
function configures the ADC module on the PIC16F877A to read analog signals from the temperature sensor (LM35) connected to AN0 (RA0). - ADC Conversion: The
ADC_Read()
function reads the analog value from the sensor, converts it to a digital value (10-bit resolution), and returns the result. - Displaying Temperature: The temperature data is formatted using the
sprintf()
function and displayed on the LCD usingLcd_Write_String()
.
Video Demonstration
Conclusion:
Interfacing an LCD with the PIC16F877A to display sensor data is a simple yet powerful way to monitor environmental or system conditions in real-time. By reading the analog data from a sensor and converting it to a human-readable form, you can create interactive projects for various applications like temperature monitoring, voltage measurement, or other sensor-based systems.
With the versatility of the PIC16F877A and ease of using LCDs, this method is great for many beginner and intermediate electronics projects.
Further Reading: