LCD DISPLAY USING WITH I2C FOR ARDUINO

I2C  LCD DISPLAY 

A liquid-crystal display is a flat-panel display or other electronically modulated optical device that uses the light-modulating properties of liquid crystals combined with polarizers. Liquid crystals do not emit light directly but instead use a backlight or reflector to produce images in color or monochrome.

Using an I2C interface to connect an LCD to an Arduino is a more efficient way of wiring, as it reduces the number of pins required. Below are the steps and code needed to set up and use an I2C LCD with an Arduino.

Components Needed:

1. Arduino Board (e.g., Arduino Uno)



2. 16x2 I2C LCD Display (or a standard 16x2 LCD with an I2C backpack)



3. Breadboard



4. Jumper Wires




 Steps to Connect the I2C LCD Display:


1. Wiring:

   - GND → Arduino Ground (GND)

   - VCC → Arduino 5V

   - SDA → Arduino A4 (for Arduino Uno)

   - SCL → Arduino A5 (for Arduino Uno)





   Note: On different Arduino boards, SDA and SCL might be on different pins, so check your board's pinout.

Installing the Necessary Library:

1. Install the LiquidCrystal_I2C Library:

   - Open the Arduino IDE.

   - Go to Sketch → Include Library → Manage Libraries.

   - In the Library Manager, search for `LiquidCrystal I2C`.

   - Install the `LiquidCrystal_I2C` library by Frank de Brabander.


Arduino Code Example:

  1. cpp
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>

  4. // Set the LCD address to 0x27 for a 16 chars and 2 line display
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);

  6. void setup() {
  7.   // Initialize the LCD
  8.   lcd.init();
  9.   // Turn on the backlight
  10.   lcd.backlight();
  11.   // Print a message to the LCD.
  12.   lcd.print("Hello, I2C!");
  13. }

  14. void loop() {
  15.   // Move the cursor to the second line, first position
  16.   lcd.setCursor(0, 1);
  17.   // Print the number of seconds since reset:
  18.   lcd.print(millis() / 1000);
  19. }


 Explanation:

- Wire Library: The `Wire` library is used to communicate with I2C devices.

- LiquidCrystal_I2C lcd(0x27, 16, 2): This initializes the LCD at address `0x27` (which is common, but it could be different for your module, so it might need checking) with 16 columns and 2 rows.

- lcd.init(): Initializes the LCD in I2C mode.

- lcd.backlight(): Turns on the backlight of the LCD.

- lcd.print("Hello, I2C!"): Prints the text "Hello, I2C!" on the LCD display.


Finding the I2C Address:

If you're unsure of your LCD's I2C address, you can use the following code to scan for the correct I2C address:


  1. cpp
  2. #include <Wire.h>

  3. void setup() {
  4.   Wire.begin();
  5.   Serial.begin(9600);
  6.   while (!Serial);
  7.   Serial.println("\nI2C Scanner");
  8.   byte error, address;
  9.   int nDevices = 0;

  10.   for (address = 1; address < 127; address++ )
  11.  {
  12.     Wire.beginTransmission(address);
  13.     error = Wire.endTransmission();

  14.     if (error == 0) {
  15.       Serial.print("I2C device found at address 0x");
  16.       if (address < 16)
  17.         Serial.print("0");
  18.       Serial.print(address, HEX);
  19.       Serial.println(" !");
  20.       nDevices++;
  21.     }
  22.     else if (error == 4) {
  23.       Serial.print("Unknown error at address 0x");
  24.       if (address < 16)
  25.         Serial.print("0");
  26.       Serial.println(address, HEX);
  27.     }
  28.   }
  29.   if (nDevices == 0)
  30.     Serial.println("No I2C devices found\n");
  31.   else
  32.     Serial.println("done\n");
  33. }

  34. void loop() {
  35. }

Library files:


 Tips:

- Ensure that the library you install matches the one being called in your code (`LiquidCrystal_I2C.h`).

- If the LCD doesn't display anything, check the wiring, adjust the contrast (if available), or double-check the I2C address using the I2C scanner.


Using I2C simplifies wiring and is very useful, especially when you need to save Arduino pins for other components in your project.

Comments

Popular Posts