CIPAD 47: Comment afficher au centre, à droite ou à gauche un texte sur afficheur lcd.

3 min read 5 hours ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial guides you through the process of displaying text on an LCD screen using Arduino. Specifically, you'll learn how to center, right-align, and left-align text using three custom functions: lcdPrintCenter, lcdPrintRight, and lcdPrintLeft. Understanding these functions will enhance your ability to present information clearly on your display.

Step 1: Set Up Your Arduino Environment

Before you start coding, ensure you have the following:

  • Arduino IDE installed on your computer.
  • An Arduino board connected to your computer.
  • An LCD display (typically 16x2) connected to the board.

Practical Tips:

  • Verify your wiring connections between the Arduino and the LCD to avoid any display issues.
  • Use jumper wires to connect the LCD pins to the appropriate Arduino pins.

Step 2: Define the LCD and Include Libraries

At the beginning of your Arduino sketch, you need to include the necessary libraries and define your LCD.

#include <LiquidCrystal.h>

// Initialize the LCD with the number of columns and rows
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Practical Tips:

  • Adjust the pin numbers in the LiquidCrystal constructor to match your actual connections.

Step 3: Create the Custom Functions

Now, let's create the functions that will allow you to display text in the desired alignment.

Center Align Function

void lcdPrintCenter(String text, LiquidCrystal &lcd, int row) {
    int len = text.length();
    int pos = (16 - len) / 2; // Calculate starting position for centering
    lcd.setCursor(pos, row);
    lcd.print(text);
}

Right Align Function

void lcdPrintRight(String text, LiquidCrystal &lcd, int row) {
    int len = text.length();
    int pos = 16 - len; // Calculate starting position for right-aligning
    lcd.setCursor(pos, row);
    lcd.print(text);
}

Left Align Function

void lcdPrintLeft(String text, LiquidCrystal &lcd, int row) {
    lcd.setCursor(0, row); // Start at the leftmost position
    lcd.print(text);
}

Practical Tips:

  • Ensure that the string length does not exceed the display's column limit, or it will not display correctly.

Step 4: Implement the Functions in Your Loop

In the loop() function, you can call these alignment functions to display text based on your requirements.

void loop() {
    lcd.clear(); // Clear the screen before displaying new text
    lcdPrintCenter("Centered Text", lcd, 0);
    lcdPrintRight("Right Aligned", lcd, 1);
    lcdPrintLeft("Left Aligned", lcd, 0);
    delay(2000); // Wait for 2 seconds before repeating
}

Practical Tips:

  • Use lcd.clear() to ensure that previously displayed text does not interfere with new messages.

Step 5: Connect and Test Your Project

  1. Upload the code to your Arduino.
  2. Power your setup and observe how the text aligns on the LCD display.

Common Pitfalls to Avoid:

  • Ensure the LCD is powered correctly.
  • Double-check the pin connections if the display is not working as expected.

Conclusion

In this tutorial, you learned how to center, right-align, and left-align text on an LCD display using Arduino. By implementing the custom functions lcdPrintCenter, lcdPrintRight, and lcdPrintLeft, you can enhance the way you present information on your display. For further exploration, consider integrating a joystick to control text display dynamically. Happy coding!