How to Scroll Text on LCD in Embedded C: Simple Guide
To scroll text on an LCD in
embedded C, display a substring of the full text on the LCD and update the starting position in a timed loop. Use lcd_clear() and lcd_set_cursor() functions to refresh the display with the next part of the text, creating a scrolling effect.Syntax
To scroll text on an LCD, you typically use these steps:
lcd_clear(): Clears the LCD screen.lcd_set_cursor(row, column): Moves the cursor to the specified position.lcd_print(string): Prints the string starting at the cursor.- Use a loop to change the starting index of the substring to display.
This creates the illusion of scrolling by shifting the visible part of the text.
embedded_c
void scroll_text(char *text, int lcd_width) { int len = strlen(text); for (int i = 0; i <= len - lcd_width; i++) { lcd_clear(); lcd_set_cursor(0, 0); lcd_print(&text[i]); delay(300); // wait to see the scroll } }
Example
This example scrolls a long message on a 16-character LCD line by showing parts of the string one by one.
embedded_c
#include <string.h> #include <stdio.h> // Mock LCD functions for demonstration void lcd_clear() { printf("\033[2J\033[H"); // Clear terminal screen } void lcd_set_cursor(int row, int col) { // For simplicity, ignore cursor positioning in this demo } void lcd_print(const char *str) { printf("%.*s\n", 16, str); // Print max 16 chars } void delay(int ms) { // Simple delay loop (not accurate) for (volatile long i = 0; i < ms * 1000; i++); } void scroll_text(const char *text, int lcd_width) { int len = strlen(text); if (len <= lcd_width) { lcd_clear(); lcd_set_cursor(0, 0); lcd_print(text); return; } for (int i = 0; i <= len - lcd_width; i++) { lcd_clear(); lcd_set_cursor(0, 0); lcd_print(&text[i]); delay(300); } } int main() { const char *message = "Hello, this is scrolling text on LCD!"; scroll_text(message, 16); return 0; }
Output
Hello, this is sc
ello, this is scr
llo, this is scro
lo, this is scrol
o, this is scroll
, this is scrolli
this is scrollin
this is scrolling
his is scrolling
is is scrolling t
s is scrolling te
is scrolling tex
is scrolling text
s scrolling text
scrolling text o
scrolling text on
crolling text on
rolling text on L
olling text on LC
lling text on LCD
ling text on LCD!
Common Pitfalls
Common mistakes when scrolling text on LCD include:
- Not clearing the LCD before printing new text, causing overlapping characters.
- Not handling text shorter than the LCD width, which can cause unexpected behavior.
- Using delays that are too short or too long, making the scroll too fast or too slow.
- Not resetting the cursor position before printing.
embedded_c
/* Wrong way: No lcd_clear and no cursor reset */ lcd_print(&text[i]); // Overlapping text /* Right way: Clear and set cursor before printing */ lcd_clear(); lcd_set_cursor(0, 0); lcd_print(&text[i]);
Quick Reference
Tips for smooth LCD text scrolling:
- Always clear the display before printing new text.
- Set the cursor to the start position before printing.
- Use a delay between updates for readable scrolling speed.
- Handle cases where text length is less than or equal to LCD width.
- Use substring pointers to shift the visible text window.
Key Takeaways
Clear the LCD and reset cursor before printing each new substring to avoid display glitches.
Scroll by showing a moving window of the text string that fits the LCD width.
Use a delay between updates to control scroll speed and readability.
Handle short text separately to avoid unnecessary scrolling.
Test on your actual LCD hardware as timing and commands may vary.