Skip to main content

Lcd I2C - Advanced Examples

This section covers features like autoscrolling, cursor control, and screen shifting, allowing for dynamic text movement and improved readability. These functions help create smoother user interactions and animated effects on the display.

⚠️
Don't forget to initialize the LCD display first!

Autoscroll

This section demonstrates the use of autoscrolling on the LCD. When enabled, new characters automatically shift to the left as they are printed, creating a continuous scrolling effect. The autoscroll() function allows for smooth dynamic text flow, while noAutoscroll() can be used to stop the effect.

void loop()
{
// Autoscroll
lcd.setCursor(5, 0); // Set cursor to the 6th position of the first row (index starts at 0)
lcd.print(F("Autoscrolling")); // Print text to LCD
lcd.setCursor(10, 1); // Set cursor
lcd.autoscroll(); // Enable autoscroll

for (int i = 0; i < 10; i++) // Print first 10 integers
{
lcd.print(i);
delay(200);
}
}

lcd.autoscroll()

Enables automatic scrolling when printing new characters. The display shifts left as new characters are added, creating a scrolling effect.

Returns value: None

lcd.noAutoscroll()

Disables automatic scrolling, keeping new characters stationary when printed.

Returns value: None


Horizontal scrolling

This section demonstrates how to shift the displayed content left and right on the LCD screen. The scrollDisplayLeft() and scrollDisplayRight() functions allow for smooth horizontal scrolling of text.

void loop()
{
// Scroll left and right
lcd.setCursor(10, 0);
lcd.print(F("To the left!"));
for (int i = 0; i < 10; i++)
{
lcd.scrollDisplayLeft(); // This command shifts all content to the left by one position
delay(200);
}
lcd.clear(); // Clear content from LCD
lcd.print(F("To the right!"));
for (int i = 0; i < 10; i++)
{
lcd.scrollDisplayRight(); // This command shifts all content to the right by one position
delay(200);
}
lcd.clear();
}

lcd.scrollDisplayLeft()

Shifts all displayed content one position to the left.

Returns value: None

lcd.scrollDisplayRight()

Shifts all displayed content one position to the right.

Returns value: None


Cursor control

This section demonstrates how to control the cursor on the LCD display, including enabling and disabling the cursor, making it blink, and hiding it completely. These functions allow for enhanced text display and interaction on the screen.

void loop()
{
// Cursor
lcd.setCursor(0, 0);
lcd.cursor();
lcd.print(F("Cursor"));
delay(3000);
lcd.clear();

// Cursor blink
lcd.setCursor(0, 0);
lcd.blink(); // This command enables cursor blink
lcd.print(F("Cursor blink"));
delay(3000);
lcd.clear();

// Blink without cursor
lcd.setCursor(0, 0);
lcd.noCursor(); // Disable the cursor so that it is no longer shown on the screen
lcd.print(F("Just blink"));
delay(3000);
lcd.noBlink(); // Stop cursor blinking
lcd.clear();
}

lcd.cursor()

Displays the cursor as a solid underscore at the current position.

Returns value: None

lcd.noCursor()

Hides the cursor from the display.

Returns value: None

lcd.blink()

Makes the cursor blink on and off at its current position.

Returns value: None

lcd.noBlink()

Stops the cursor from blinking.

Returns value: None


Full example

This code demonstrates each of the functions mentioned above, providing a complete showcase of LCD display control in action.

// Include the library
#include "16x2-LCD-SOLDERED.h"

LCD lcd; // LCD object


void setup()
{
lcd.begin(); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
}

void loop()
{
// Autoscroll
lcd.setCursor(5, 0); // Set cursor to the 6th position of the first row (index starts at 0)
lcd.print(F("Autoscrolling")); // Print text to LCD
lcd.setCursor(10, 1); // Set cursor
lcd.autoscroll(); // Enable autoscroll

for (int i = 0; i < 10; i++) // Print first 10 integers
{
lcd.print(i);
delay(200);
}

lcd.noAutoscroll(); // Disable autoscroll
lcd.clear(); // Clear content from LCD

// Scroll left and right
lcd.setCursor(10, 0);
lcd.print(F("To the left!"));
for (int i = 0; i < 10; i++)
{
lcd.scrollDisplayLeft(); // This command shifts all content to the left by one position
delay(200);
}
lcd.clear(); // Clear content from LCD
lcd.print(F("To the right!"));
for (int i = 0; i < 10; i++)
{
lcd.scrollDisplayRight(); // This command shifts all content to the right by one position
delay(200);
}
lcd.clear();

// Cursor
lcd.setCursor(0, 0);
lcd.cursor();
lcd.print(F("Cursor"));
delay(3000);
lcd.clear();

// Cursor blink
lcd.setCursor(0, 0);
lcd.blink(); // This command enables cursor blink
lcd.print(F("Cursor blink"));
delay(3000);
lcd.clear();

// Blink without cursor
lcd.setCursor(0, 0);
lcd.noCursor(); // Disable the cursor so that it is no longer shown on the screen
lcd.print(F("Just blink"));
delay(3000);
lcd.noBlink(); // Stop cursor blinking
lcd.clear();
}

Functions.ino

Example file for using some functions with the LCD I2C display