I2C LCD only prints first character of the string

Hi, I just got my Arduino kit from Sunfounder. So far nothing works out of the box. I've fixed the first few problems but this one has me stumped.

When I print multiple characters only the first character prints unless I print one character at a time.

i.e. lcd.print("cm"); will show 'c' on the LCD, but lcd.print("c"); lcd.print("m"); will display 'cm'

Sunfounder knows about this problem because they worked around it in their Hello World sketch by putting the string in an array and then printing one character at a time in a loop. Here is their code:

#include <LiquidCrystal.h>

// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//
char array1[]=" Sunfounder "; //the string to print on the LCD
char array2[]="hello, world! "; //the string to print on the LCD
int tim = 250; //the value of delay time, 500 for 250 ms or 2 for each 1 ms
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/
/
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
}
/
/
void loop()
{
lcd.setCursor(15,0); // set the cursor to column 15, line 0
for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array1[positionCounter1]); // Print a message to the LCD.
delay(tim); //wait for 250 microseconds
}
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
lcd.setCursor(15,1); // set the cursor to column 15, line 1
for (int positionCounter = 0; positionCounter < 26; positionCounter++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array2[positionCounter]); // Print a message to the LCD.
delay(tim); //wait for tim which is set above
}
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
}
/
****/

But they don't do this in their other sketches. The one I'm working on now is Ultrasonic. And it only prints the first character. Here is the code:

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

// include the library code
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>

LiquidCrystal_I2C lcd(0x27,16,2);

#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
lcd.init();
lcd.backlight();
}

void loop() {
delay(100); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print(uS / US_ROUNDTRIP_CM);
lcd.setCursor(12, 1);
lcd.print("cm");
}

Please help me.

My first question is "How do I fix this?"

Other questions:
How do I find out which library a function is in? For example the Serial.print("Ping: "); Where do I find the code? I want to see it as an example.
How does the lcd.print() function work? Again, where is the code? I can't find it in the LiquidCrystal_I2C library, even though print is in the keywords?
Thanks in advance for the help.

There is a problem with your i2c lcd library and the recent version(s) of the IDE. See this thread 5V IIC/I2C/TWI LCD Module Adapter For Arduino from eBay - Displays - Arduino Forum

My first question is "How do I fix this?"

My recommendation would be to convert to the F. Malpartida i2c lcd library which does not have the problem. Otherwise, change the return value for the write function in the library you are using.

1 Like

And while you are at it, go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so anyone can review it conveniently and accurately.

I have the same problem. I'm using Arduino IDE 1.8.0 and coudn't find any updated LiquidCrystal I2C library.
I think that the only problem I suffer from. If you think there are other ways to solve this problem, tell me whet to do now.

The code that I've tried to use is:

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

LiquidCrystal_I2C lcd(0x27 , 16 , 2);

void setup() {
lcd.begin(16,2); }

void loop() {
lcd.home();
lcd.print("HI BAYRAM");
delay(2500);
lcd.clear();
lcd.cursor();
lcd.blink();
delay(5000);
lcd.noBlink();
lcd.noCursor();
delay(3000);
delay(6000);
}

According to that code, my 16x2 LCD only show the letter ""H" as a starter of "HI BAYRAM"

I'm using Arduino IDE 1.8.0 and coudn't find any updated LiquidCrystal I2C library.

You can use the library manager to install v1.1.2 of LiquidCrystal_I2C.

Alternatively, read the referenced thread 5V IIC/I2C/TWI LCD Module Adapter For Arduino from eBay - Displays - Arduino Forum completely and use one of the other two libraries mentioned.

The hd44780 library is available through the library manager.

I have the same problem.

And you also didn't read reply #2.

Don

In their defense, there is SO much misinformation out there on this topic that it is VERY hard to find or figure out a solution.
This is made even more of an issue since the Arduino forum admins removed the forum search capability and instead replaced it with a generic google search of the forum site.
THAT was a very dumb thing to do and dramatically limits the type of searching you can do.
I participate in over 100 different forums and every other forum other than this one, has a real search capability.
Even when I know what to look for, it can be quite difficult on this site.

Anyway, as pointed out, the issue is a library issue and it was fixed like a year and half ago.
My suggestions for any newbies is to get libraries from the library manager whenever possible.
Another issue in this particular case, is that there are too many libraries named "LiquidCrystal_I2C" and they all don't work the same.
While there is only 1 called "LiquidCrystal_I2C" in the library manager, it doesn't work with all LCD backpacks so there is no guarantee that it works with your backpack.
However, if you see only a single character, it will work with the particular backpack you have, but it might not work with the one you buy in the future.

Because of all this, I created the hd44780 library.
I should work with any LCD backpack and will automatically configure most backpacks, by locating the i2c address and figuring out the pin mappings.
It can be installed directly from the GUI using the IDE library manager:

You can read more about it here: GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library
The i/o class you will use is hd44780_I2Cexp
I would recommend that you run the included diagnostic I2CexpDiag which will test i2c connections and the LCD internal RAM.

--- bill

I have tried all the libs including the pololu one described here... they seem to have the same problem or are set up for 16x2 screens not a 20x4. Does anyone have a definitive lib for the 20x4 LCD screen such that it prints more than just the first char of a string? I am sorry but I am a beginner and not yet fully up to speed with all the libs for this (there are many!!) and which might work, etc... thank you!

I am sorry but I am a beginner and not yet fully up to speed with all the libs for this (there are many!!) and which might work, etc

I recommend that you install the hd44780 library available through the library manager.

This sketch should get you going

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

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip

void setup()
{
  // initialize LCD with number of columns and rows:
  lcd.begin(20, 4);

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

void loop()
{
  lcd.setCursor(0, 1);
  lcd.print(millis() / 1000);
  delay(1000);
}

Read this from the library's creator and display guru Bill Perry from reply #6

Because of all this, I created the hd44780 library.
I should work with any LCD backpack and will automatically configure most backpacks, by locating the i2c address and figuring out the pin mappings.
It can be installed directly from the GUI using the IDE library manager:
https://www.arduino.cc/en/Guide/Libraries
You can read more about it here: GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library
The i/o class you will use is hd44780_I2Cexp
I would recommend that you run the included diagnostic I2CexpDiag which will test i2c connections and the LCD internal RAM.

1 Like

BINGO! Thank you! I am off and running :slight_smile:

Hey Sam.

Perhaps you should mention in your other thread that you solved that problem by zombification of this thread.
Do so by putting [solved] in front the title of the first post, and by appending a reply.
Saves people some valuable time.

MAS3: How can I change the title of the post from "20x4 I2C only displays 1st char " to "SOLVED: 20x4 I2C only displays 1st char " I don't see any way to edit the title. I am the creator of the post but the title can't be edited (??).

You can change the title if you edit your original post.

Don

Hello I know it's an older Topic, but maybe I can give a solution
I my case the wiring of the PCF8574 was wrong and I had the issue with just one char I still wonder why it was working, anyway
the wires should be
P0-RS
P1-RW
P2-E
P3-LED
P4-D4
P5-D5
P6-D6
P7-D7
I'm using the Lib "LiquidCrystal_PCF8574"
and altered to
#define PCF_RS 0b00000001
#define PCF_RW 0b00000010
#define PCF_EN 0b00000100
#define PCF_BACKLIGHT 0b00001000
in LiquidCrystal_PCF8574.cpp

I also changed

void LiquidCrystal_PCF8574::setBacklight(void) {
_backlight = 0;
// send no data but set the background-pin right;
_write2Wire(0x00, RSMODE_DATA, false);
} // setBacklight

void LiquidCrystal_PCF8574::clearBacklight(void) {
_backlight = 1;
// send no data but set the background-pin right;
_write2Wire(0x00, RSMODE_DATA, false);
} // clearBacklight

In my case it is working

I just do a random float test for the last 30 min and it is stable

Have a good one