Problem with I2C LCD display.

Hi, I am building a BMI calculator with an ultrasonic sensor and a weighing scale. I firstly wanted to make the LCD display show the measurements it gets from the ultrasonic sensor. The code works fine, after I did some research and changed the LCD display address to my own. The LCD's lights work fine but there is no text being displayed. I am using an I2C backpack to control the LCD and I am using an Arduino UNO. I am using someone else's code and I will leave a link for the video and an example of the code. The SCL of the I2C is connected to the A5 analog pin and the SDA is connected to the A4 analog pin. The ultrasonic sensor echo is connected to pin 13 and the trigg is connected to pin 12. The LCD and the ultrasonic sensor are connected to the GND on the arduino and they are powered by 5v on a breadboard. Help would be appreciated, Thank you.

#include<UltraDistSensor.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,16,2);

UltraDistSensor mysensor;
float reading;

void setup() {
    lcd.init();                      // initialize the lcd 
    lcd.backlight();
    Serial.begin(9600);
    mysensor.attach(12,13);//Trigger pin , Echo pin
}

void loop() {
    reading=mysensor.distanceInCm();
     lcd.setCursor(0,0);
     lcd.print("Distance :- ");
      lcd.print(reading);

    delay(1000);
}

Video I am working from: Ultrasonic Sensor HC-SR04 Tutorial with LCD I2C - YouTube

danielkepaniel:
The code works fine, after I did some research and changed the LCD display address to my own.

What exactly do you mean by that?

First essential steps:

Have you made the LCD to work with a "Hello World" test example?

What does the LCD actually show?

Have you adjusted the contrast?

I have tried to make it work with the Hello World example but the same thing happens. The LCD's is shining a light but there is no text. I have tried to adjust the contrast but nothing changes. The code shows no errors and the I ran some code to detect the address of the LCD. This helped me clear out the problem that there is something faulty with the code. In the beginning the LCD didn't even turn on till I changed the address.
P.S. I tried to add an attachment with a photo of the circuitry but it wouldn't post it because the image's size was above 2mb any idea how I can post the attachment?

Use IrfanView (Windoze system) to shrink the photo a little. :grinning:

Please show what appears on the display.

Many (most?) backpacks are incompatible with that antique LiquidCrystal_I2C library. It is recommended you install Bill Perry's HD44780 library using the Library Manager and start with the examples included there.

I don't know what is happening know. When I plugged in the Arduino the LCD turned on and then off again. The LCD is now repeating this cycle of turning on and off. Attached is an image of the LCD when it is switched on. Thank you for all your help and patience.

I tried to tweak the brightness again, but it either looks like the photo I sent or the photo I have attached to this post. The first photo is how it looks when the LCD is on and the other one is when the LCD is going off.

Arduino LCD 1.jpg

Here is the second image.

First photo:
Arduino LCD.jpg

Second photo:
Arduino LCD 1.jpg

Now third photo:
Arduino LCD 2.jpg

What are you using as a regulated 5 V supply for the UNO and the display? OK, the UNO appears to be powered by USB.

Second and third photo show LCD contrast set correctly (more or less) but not being initialised by code. First photo - if contrast setting the same - suggest it is initialised but nothing further.

Soldering to pins 10 and 11 of the display looks bad.

I'll be sure to check the soldering. I am using 5V from the arduino which is plugged into my computer.

I used the hello world example from the HD44780 library, only major change is that the LCD is not blinking anymore and now it look like the second photo.

What do you see when you run the I2CexpDiag diagonistic sketch in the library examples for the I2Cexp io class?

hd44780\examples\ioClass\hd44780_I2Cexp\I2CexpDiag

Ok, so when I ran the diagnostic test it was the first time the LCD displayed something useful. The results seemed to show that nothing is wrong but it still wont work with the code I put in the beginning. These are the results from the diagnostic test.
Serial Initialized

I2CexpDiag - i2c LCD i/o expander backpack diagnostic tool

hd44780 lib version: 1.1.1

Reported Arduino Revision: 1.8.12
CPU ARCH: AVR - F_CPU: 16000000

SDA digital pin: 18 A4
SCL digital pin: 19 A5

Checking for required external I2C pull-up on SDA - YES
Checking for required external I2C pull-up on SCL - YES
Checking for I2C pins shorted together - Not Shorted

Scanning i2c bus for devices..
i2c device found at address 0x20
Total I2C devices found: 1

Scanning i2c bus for all lcd displays
LCD at address: 0x20 | config: P65401237L | R/W control: Yes
Total LCD devices found: 1

LCD Display Memory Test
Display: 0
Walking 1s data test: PASSED
Address line test: PASSED

Each working display should have its backlight on
and be displaying its #, address, and config information
If all pixels are on, or no pixels are showing, but backlight is on, try adjusting contrast pot
If backlight is off, wait for next test

Blinking backlight test: to verify BL level autodetection
If backlight is mostly off but
you briefly see "BL Off" on display with backlight on,
then the library autodetected incorrect BL level
and the library cannot autoconfigure the device

Displaying 'uptime' on all displays

I ran the code above that I am currently using to display the metrics from an ultrasonic sensor on the LCD. Something interesting happened, the display I got from the diagnostic test stayed there but the LCD blinked on and off.

OK, well here's the hint:

The diagnostic code from the HD44780 library actually works and it actually displays useful data.

The code you used originally does not display data at all and indeed, it does not even initialise the display and clear the other data from it.

The immediate and striking conclusion is that the library and LCD code in your original sketch is completely useless and must be abandoned. :astonished:

Use the code provided with the HD44780 library, starting with a basic "hello world" as the basis for your project. Start with a simple sketch and add the parts of your original design that you need to display, one at a time, testing as you go.

That is how you build a project. :roll_eyes:

Thank you for all your help this really helped me a lot and I learned some new things too and I am grateful for that. I only have one last question. How can I find the address for for my LCD, because I tried using the address I used for my previous code but that didn't work. I am just wondering if there is something on the backpack I should look out for?

// LiquidCrystal compability:
// Since hd44780 is LiquidCrystal API compatible, most existing LiquidCrystal
// sketches should work with hd44780 hd44780_I2Clcd i/o class once the
// includes are changed to use hd44780 and the lcd object constructor is
// changed to use the hd44780_I2Clcd i/o class.

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Clcd.h> // i2c LCD i/o class header

// Note, i2c address can be specified or automatically located
// If you wish to use a specific address comment out this constructor
// and use the constructor below that specifies the address

// declare the lcd object for auto i2c address location
hd44780_I2Clcd lcd;

//
// enter address of LCD.
// Addresses seen so far include:
// - 0x3a, 0x3b (PCF2119x)
// - 0x3c (unknwon chip)
// - 0x3d (unknwon chip)
// - 0x3e (unknwon chip)
// - 0x3f (unknwon chip)

// declare i2c address and constructor for specified i2c address
//const int i2c_addr = 0x3e;
//hd44780_I2Clcd lcd(i2c_addr); // use device at this address


// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup()
{
int status;

	// initialize LCD with number of columns and rows: 
	// hd44780 returns a status from begin() that can be used
	// to determine if initalization failed.
	// the actual status codes are defined in <hd44780.h>
	// See the values RV_XXXX
	//
	// looking at the return status from begin() is optional
	// it is being done here to provide feedback should there be an issue
	//
	status = lcd.begin(LCD_COLS, LCD_ROWS);
	if(status) // non zero status means it was unsuccesful
	{
		// begin() failed so blink error code using the onboard LED if possible
		hd44780::fatalError(status); // does not return
	}

	// Print a message to the LCD
	lcd.print("Hello, World!");
}

void loop() {}
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Clcd.h> // i2c LCD i/o class header
hd44780_I2Clcd lcd;

I think, you are using the wrong ioClass. You want I2Cexp for the "expander" backpacks. The header files should look like this. The library will auto configure for the connection arrangement and i2c address. It will be plug and play for all the different back pack expanders.

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header

hd44780_I2Cexp lcd;

The I2Cexp ioclass was used by the diagnostic sketch.

danielkepaniel:
Thank you for all your help this really helped me a lot and I learned some new things too and I am grateful for that. I only have one last question. How can I find the address for for my LCD, because I tried using the address I used for my previous code but that didn't work. I am just wondering if there is something on the backpack I should look out for?

// LiquidCrystal compability:

// Since hd44780 is LiquidCrystal API compatible, most existing LiquidCrystal
// sketches should work with hd44780 hd44780_I2Clcd i/o class once the
// includes are changed to use hd44780 and the lcd object constructor is
// changed to use the hd44780_I2Clcd i/o class.

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Clcd.h> // i2c LCD i/o class header

// Note, i2c address can be specified or automatically located
// If you wish to use a specific address comment out this constructor
// and use the constructor below that specifies the address

// declare the lcd object for auto i2c address location
hd44780_I2Clcd lcd;

//
// enter address of LCD.
// Addresses seen so far include:
// - 0x3a, 0x3b (PCF2119x)
// - 0x3c (unknwon chip)
// - 0x3d (unknwon chip)
// - 0x3e (unknwon chip)
// - 0x3f (unknwon chip)

// declare i2c address and constructor for specified i2c address
//const int i2c_addr = 0x3e;
//hd44780_I2Clcd lcd(i2c_addr); // use device at this address

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup()
{
int status;

// initialize LCD with number of columns and rows:
// hd44780 returns a status from begin() that can be used
// to determine if initalization failed.
// the actual status codes are defined in <hd44780.h>
// See the values RV_XXXX
//
// looking at the return status from begin() is optional
// it is being done here to provide feedback should there be an issue
//
status = lcd.begin(LCD_COLS, LCD_ROWS);
if(status) // non zero status means it was unsuccesful
{
// begin() failed so blink error code using the onboard LED if possible
hd44780::fatalError(status); // does not return
}

// Print a message to the LCD
lcd.print("Hello, World!");
}

void loop() {}

You are using the wrong hd44780 i/o class.
In that sketch you are trying to use, just above the code you posted, was this comment:

// This sketch is for LCD modules that have a native I2C interface such as
// PCF2119x, PCF2116, or certain RayStar LCDs rather than those LCD modules that
// use an i/o expander chip based based backpack.

This should have been a clue that this sketch is not for the type of LCD device you are using.

I would recommend taking some time to read hd44780 included documentation.
There is quite a bit of documentation that comes with the library that can be accessed directly from the
IDE under the Documentation example sketch.
Bring up that example, and you bring up the documentation and can click on many other documentation links.
It explains the structure of the library and its examples and has lots of documentation on the type of LCD and backpack that you have.

--- bill

Ok so I finally figured the LCD thing out and it displays the information it has to display. I'd like to thank everyone that contributed to this forum and everyone that helped me along the way. I appreciate all the help you guys have given me.

danielkepaniel:
Ok so I finally figured the LCD thing out and it displays the information it has to display.

For all those following along and for those that see this thread in the future,
What was the issue, and how did you solve it?

--- bill