I cannot display an Int on an LCD via I2C

I am trying to send an Int value to an LCD via I2C communication and I am only getting a single digit showing on the LCD that does not seem to be related to the actual value.

The values are just analogRead from a joystick.

Any idea why it won’t display the whole number and only one digit?

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

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display

void setup()
{
lcd.init();
lcd.backlight();
}

void loop()
{
int x = analogRead(A0);
int y = analogRead(A1);

lcd.clear();
lcd.setCursor(5,0);
lcd.print(x,10);
lcd.setCursor(5,1);
lcd.print(y);
delay(50);
}

Did you try printing x and y to the Serial Monitor first? Just to make sure the values are what you think they are.

BTW, if your project every gets anymore complex than this, it would really behoove you to pick meaningful variable names rather than x and y.

Serial monitor displays the true analog values between 0 - 255

The values of x and y are only for demo purposes, they will be sensor values named accordingly in the full code.

"0 - 255"
Not
0-1023 ?

I'm sorry, that is what I meant. I just typed it wrong cause I was thinking of something else.

Analog values from Joystick display from 0 -1023

What happens if you
lcd.setCursor(5,0);
lcd.print(“1023”);
lcd.setCursor(5,1);
lcd.print(“1023”);

I only see "1" displayed on both lines of the LCD

What happened when you ran the examples from the LiquidCrystal_I2C library?

Those work fine, but they don't have any examples where they display Int's that change, so it isn't a very helpful diagnostic tool unfortunately.

Doesn't make a difference as long as the class inherits from Print (which they all do). Post a link to the Github page for the library you're using.

drmesa:
Those work fine, but they don't have any examples where they display Int's that change, so it isn't a very helpful diagnostic tool unfortunately.

Can't image. Try modifying the 'HelloWorld' example to print an int variable rather than one of the string literals.

Tried. It doesn't seem to want to display more than one digit before the Decimal.

I used "float z = 35.787;" and printed it.

"3.79" was displayed, so it killed the 5 and rounded to 0.79

What do you see if you leave out the lcd.clear() and only print every 500 ms.

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

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display

void setup()
{
  lcd.init();
  lcd.backlight();
}

void loop()
{
  int x = analogRead(A0);
  int y = analogRead(A1);

  //lcd.clear();
  lcd.setCursor(5, 0);
  lcd.print(x);
  lcd.setCursor(5, 1);
  lcd.print(y);
  delay(500);
}

Number “3” and “2” on top and bottom lines respectively.

Number "3" and "2" on top and bottom lines respectively.

Connect both A0 and A1 to 5v instead of the joystick. What do you see?

There are MANY different libraries that have a LiquidCrystal_I2C i/o class.
It looks like you have installed a very old version of one of the "LiquidCrystal_I2C" libraries (like several years old) instead of using the one from the IDE library manager.
There was a bug in that library that caused it to only print the first character of a string - it isn't related to numbers.

While it is easy to fix the code or just install the proper latest version from the IDE library manager, I'd recommend that you switch to the hd44780 library and use the hd44780_I2Cexp i/o class instead.
It has additional capabilities and is also quite a bit faster.

So I would suggest that you install the hd44780 library, then run the included diagnostic sketch, I2CexpDIag.
It will test the i2c signals and the internal LCD memory.
If it doesn't work you can post the results here and we can help you sort it out.
After that, then run the included examples, which should work.

You can read more about the hd44780 library here: GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library
There is also lots of documentation included with the hd44780 library that you can find in the included "Documentation" sketch.

--- bill

Issue has been resolved. Thank You all for your help and input.

The issue was with the Library vs IDE version.

I deleted the old library and installed the new IDE, it is now working perfectly with the exact same code.

drmesa:
Issue has been resolved. Thank You all for your help and input.

The issue was with the Library vs IDE version.

I deleted the old library and installed the new IDE, it is now working perfectly with the exact same code.

Yep. Sounds like it was what I thought it was.
The old LiquidCrystal_I2C library, which has the bug in it, "worked" on older IDE versions as the older Print class was not properly checking return status from write(). Once the Print class code was updated in IDE version 1.6.6, if the i/o class returned the incorrect status from its write(), then the Print code would abort sending any further characters.

I'm assuming you also installed the latest LiquidCrystal_I2C library using the IDE library manager, which is what solved the issue.

BTW, I'd still recommend using the hd44780 library, for the added capabilities and and additional speed.

--- bill

What happened when you ran the examples from the LiquidCrystal_I2C library?

Those work fine, but they don't have any examples where they display Int's that change, so it isn't a very helpful diagnostic tool unfortunately.

I understand the issue with older versions of the LiquidCrystal_I2C library, the .write() return value, and the ide change with 1.6.6

There were many threads several years ago with "lcd only prints first letter"

I don't understand how you had a working library example and non working code.