Class 4 - Counter using LCD

May 08, 2025

Hi buddies!!
In our previous class, we built a 7-segment display counter using an 8051 microcontroller. In this tutorial, let’s see how to display the same counter output on an LCD display.

📺 How Does an LCD Work?

An LCD consists of three major parts:

  • Backlight – A small LED that provides light behind the display.
  • Liquid Crystals – These crystals align when voltage is applied, allowing them to either block or twist light.
  • Polarizer – A special filter that controls light visibility. When crystals twist the light, it passes through and appears blank. If light is blocked, the characters are visible.

🔌 LCD Pin Details (16 Pins)

  • Pin 1 & 2: Power supply (Vss, Vdd)
  • Pin 3: Contrast adjust (via potentiometer)
  • Pins 4–6: Control Pins (RS, RW, E)
  • Pins 7–14: Data Pins (D0 to D7)
  • Pin 15 & 16: Backlight (LED+ and LED–)

We’ll operate the LCD in 4-bit mode to save microcontroller I/O pins.

📘 8-bit vs 4-bit Mode

  • 8-bit mode: Sends 8 bits (1 byte) at a time – uses all 8 data lines.
  • 4-bit mode: Sends data in two nibbles (4 bits at a time) – uses only 4 data lines. Ideal when I/O ports are limited.

Control Pins Usage:

  • RS (Register Select): 0 for command, 1 for data
  • RW (Read/Write): 0 for write, 1 for read (mostly we use write)
  • Enable (E): LCD latches data on rising edge (0 to 1 transition)

⏱ LCD Timing & Write Mode

Normally, LCD takes around 40µs to process a command or data. For special commands like clear, it might take up to 2ms.

Since timings are predictable, we usually keep RW tied to 0 (write mode) and use delays instead of reading LCD status.

⚙️ Hardware Setup

  • LCD used in 4-bit mode
  • RW pin connected to GND (always in write mode)
  • Current-limiting resistors used for backlight
  • Data lines D4–D7 connected to MCU (P0 or any suitable port)

🛠 LCD Functions Overview

You’ll need these 3 basic functions in your code:

  1. LCD_Init() – Initializes the LCD (Display ON, Cursor ON, 4-bit mode)
  2. LCD_Command() – Sends commands to LCD (clear, cursor control, etc.)
  3. LCD_Data() – Sends ASCII characters to be displayed

Internally, both LCD_Command() and lcd_data() work similarly, but the RS pin decides whether it’s a command or data.

💡 Program Explanation

We’re reusing our old counter logic with small changes:

  1. Declare a buffer array
  2. Initialize LCD using LCD_Init()
  3. On button press, increment the counter
  4. Clear the display using LCD_Command(LCD_CLEAR)
  5. Convert the counter value to ASCII using sprintf()
  6. Set cursor position with LCD_Goto()
  7. Display value using LCD_Puts()

Once uploaded to Buddy51 Mini board, press the button to see the counter increment from 0 to 9 on the LCD!

📺 Output

  • Button press → count increments
  • Value displayed on the LCD
  • Clears and refreshes on each increment

❓ Have Doubts?

If you have any questions about LCD interfacing, 4-bit mode timing, or code setup, feel free to ask in the comments!

Stay tuned, and thanks for reading!
– Team BuddyKit 🚀

Leave a reply
Class 3 – 7-Segment Display CounterClass 5 – 4×4 Matrix Keypad & LCD

Leave Your Reply