0
0
Power-electronicsHow-ToBeginner · 3 min read

How to Display Number on LCD in Embedded C: Simple Guide

To display a number on an LCD in embedded C, convert the number to a string using sprintf() or similar, then send the string to the LCD using your LCD's display function like LCD_String(). This approach works because LCDs display characters, not raw numbers.
📐

Syntax

To display a number on an LCD, first convert the number to a string, then send it to the LCD display function.

  • sprintf(buffer, "%d", number); converts an integer number to a string stored in buffer.
  • LCD_String(buffer); sends the string to the LCD to be shown.
c
char buffer[16];
int number = 123;
sprintf(buffer, "%d", number);
LCD_String(buffer);
💻

Example

This example shows how to initialize the LCD, convert a number to a string, and display it on the LCD.

c
#include <stdio.h>
#include "lcd.h"  // Assume this header controls your LCD

int main() {
    char buffer[16];
    int number = 4567;

    LCD_Init();               // Initialize the LCD
    sprintf(buffer, "%d", number);  // Convert number to string
    LCD_String(buffer);       // Display string on LCD

    while(1) {}
    return 0;
}
Output
LCD displays: 4567
⚠️

Common Pitfalls

Common mistakes when displaying numbers on LCD include:

  • Trying to send numbers directly without converting to string, which LCD cannot display.
  • Not allocating enough space in the buffer for the string, causing overflow.
  • Forgetting to initialize the LCD before sending data.
c
/* Wrong way: sending number directly */
int number = 123;
LCD_Char((char)number);  // Incorrect: LCD expects characters, not integers

/* Right way: convert number to string first */
char buffer[16];
sprintf(buffer, "%d", number);
LCD_String(buffer);
📊

Quick Reference

Tips for displaying numbers on LCD in embedded C:

  • Always convert numbers to strings before displaying.
  • Use sprintf() or itoa() for conversion.
  • Ensure LCD is initialized before use.
  • Allocate enough buffer size for the string.

Key Takeaways

Convert numbers to strings before sending to LCD because LCD displays characters.
Use functions like sprintf() to convert integers to strings safely.
Always initialize the LCD before displaying any data.
Allocate sufficient buffer size to avoid string overflow.
Avoid sending raw numbers directly to LCD display functions.