The normal default address on most of these displays is 0x27 but that can be changed:
I2C Interface
This display incorporates an I2C interface that requires only 2 pins on an MCU to interface with and it has good library support to get up and running fast. The I2C interface is a daughter board attached to the back of the LCD module.
The default I2C address for these displays is 0x27. The address is marked on the bag that the module comes in.
If you need to adjust I2C address to avoid a conflict, this can be done on the I2C adapter board on the back of the module. There are 3 address jumper locations marked A0, A1, A2. Normally these lines are pulled high. If you bridge these pads, it grounds that address line. If you were to bridge all 3 to ground, the address would be 0x20. The range of all possible addresses spans from 0x20 to 0x27
If you are unsure of the I2C address of this or any other device, it is easy to find out what it is. Just refer to this blog post.
Next you can try this basic simple code sample:
The code can be modified as written it uses the 0x27 address, 20 columns, 4 rows. Adjust for what you have be it 16 columns 2 rows or 20 columns 4 rows. On your Mega SDA to SDA and SCL to SCL or on your Mega ADC 4 and ADC 5.
See if that works.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address. Common addresses are 0x27 or 0x3F.
// You might need to run an I2C scanner sketch to find the correct address for your module.
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address, 20 columns, 4 rows
void setup() {
// Initialize the LCD
lcd.init();
// Turn on the backlight
lcd.backlight();
// Set the cursor to the first column (0) and first row (0)
lcd.setCursor(0, 0);
// Print a message to the first row
lcd.print("Hello, world!");
// Set the cursor to the first column (0) and second row (1)
lcd.setCursor(0, 1);
// Print a message to the second row
lcd.print("Arduino I2C LCD");
// Set the cursor to the first column (0) and third row (2)
lcd.setCursor(0, 2);
// Print a message to the third row
lcd.print("20x4 Display");
// Set the cursor to the first column (0) and fourth row (3)
lcd.setCursor(0, 3);
// Print a message to the fourth row
lcd.print("Example Code");
}
void loop() {
// The loop can be used for dynamic content updates, scrolling text, etc.
// For this basic example, it remains empty.
}