Lcd 1602 prints array but not strings

Hello all,
Just another arduino beginner but experienced php / mysql programmer.
arduino uno with lcd 1602 using I2C DSA & SCl prints array but not strings

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

char array3[] = "Oops!           ";
char array4[] = "1 Bit at a time!";

void setup() {
	Wire.begin();
	lcd.begin(16,2);              //  
	lcd.backlight();              //  Turn on backlight
	lcd.clear();                  //  Clears the display
	delay(2000);
}

void loop() {
	lcd.clear(); // Clears the display

	//	if I do this it does not display
    lcd.setCursor(0, 0);  // set the cursor to column 0, line 0
    lcd.print("1602 LCD Display");

	//	this it does display
	lcd.setCursor(0, 0);
	for ( int positionCounter1 = 0; positionCounter1  < 17; positionCounter1++)
	{
	lcd.print(array3[positionCounter1]);  //  prints a 17 character array without scrolling
	delay(250);
	}

	{
	lcd.setCursor(0, 1);
	for (int positionCounter2 = 0; positionCounter2  < 17; positionCounter2++)
	{
	  lcd.print(array4[positionCounter2]);  //  prints a 17 character or array without scrolling
	}
	delay(3000);
	lcd.clear();
	}

}

The array3 displays a byte at a time but array4 displays all at once.

Does anyone know why lcd.print won't display the string?

You need a delay after the print or it will get wiped out.

1 Like

There needs to be a delay inside the for loop, otherwise it prints each character so fast it appears to be all at once.

Possibly not the reason of your problem but your array only contains 16 characters (and a terminating '\0'; you're printing the '\0'.

The better way is to use strlen

for ( int positionCounter1 = 0; positionCounter1  < strlen(array3); positionCounter1++)
1 Like

Hello guys,
sonofcy, david_2018 Yes, I forgot to include the delay but added it now.

sterretje section #1 works but section #2 does not.

Here is the updated code:

/*
  LiquidCrystal Display Library - Hello World using I2C

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
  The circuit is not using I2C:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 This example code is in the public domain.
 https://docs.arduino.cc/learn/electronics/lcd-displays
*/
// include the library code:
#include <LiquidCrystal_I2C.h>  // include the library code
#include <Wire.h>               // wire library

//initialize the liquid crystal library when using I2C_Method
//the first parameter is  the I2C address
//the second parameter is how many rows are on your screen
//the  third parameter is how many columns are on your screen
LiquidCrystal_I2C lcd(0x27, 16, 2);

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to when using Conventional_Method
// const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
// LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

char array1[] = "Hello!          ";       //an array to print on line 0
char array2[] = "1 Bit at a time!";      //an array to print on line 1
char array3[] = "Hello, From";
char array4[] = "Arduino_uno_guy";

void setup() {
  Wire.begin();
  lcd.begin(16,2);              //  init columns and rows
  lcd.home();                   //  set cursor at 0,0
  lcd.backlight();              //  Turn on backlight
  delay(2000);
}

void loop() {
  
  lcd.clear();                  // Clears the display  
  delay(1000);              // wait for a second

  // section #1
  lcd.setCursor(0, 0);
  for ( int positionCounter1 = 0; positionCounter1  < strlen(array1); positionCounter1++) {
    lcd.print(array1[positionCounter1]);  //  prints a 17 character array without scrolling
    delay(250);
  }
  {
    lcd.setCursor(0, 1);
    for ( int positionCounter2 = 0; positionCounter2  < strlen(array2); positionCounter2++) {
      lcd.print(array2[positionCounter2]);  //  prints a 17 character or array without scrolling
    }
    delay(3000);
    lcd.clear();
  }

  lcd.setCursor(0, 0);
  for ( int positionCounter1 = 0; positionCounter1  < strlen(array3); positionCounter1++) {
    lcd.print(array3[positionCounter1]);  //  prints a 17 character array without scrolling
    delay(250);
  }
  {
    lcd.setCursor(0, 1);
    for ( int positionCounter2 = 0; positionCounter2  < strlen(array4); positionCounter2++) {
      lcd.print(array4[positionCounter2]);  //  prints a 17 character or array without scrolling
    }
    delay(3000);
    lcd.clear();
  }

  // section #2
  // this section still does not print, it only outputs a single letter at the 
  // beginning of the first and second row ???
  // I tried using the F accordingly to see if that would change anything - no.
  lcd.setCursor(0,0);           // tell the screen to write on the top row  
  lcd.print("Hello, From");     // tell the screen to write “hello, from” on the top row  
  lcd.setCursor(0,1);           // tell the screen to write on the bottom  row
  lcd.print(F("Arduino_uno_guy"));  
  delay(3000);                  // wait 
}

Any ideas about the second part?
Another question, what would be a slightly better display to use as I am thinking it might be better to have a few more rows?
Thanks

How do you have the display wired to the Arduino? Did you add the I2C adapter to the display yourself, or was it pre-assembled? How are you powering the LCD?

I don't really see anything wrong with the code:

  // section #2
  // this section still does not print, it only outputs a single letter at the
  // beginning of the first and second row ???
  // I tried using the F accordingly to see if that would change anything - no.
  lcd.setCursor(0, 0);          // tell the screen to write on the top row
  lcd.print("Hello, From");     // tell the screen to write “hello, from” on the top row
  lcd.setCursor(0, 1);          // tell the screen to write on the bottom  row
  lcd.print(F("Arduino_uno_guy"));
  delay(3000);                  // wait
}

You can get LCD displays with 4 rows of 20 characters each, if that is not enough then there are OLED displays (generally 128x32 or 128x64 pixel), but those are physically much smaller.

Hello david_2018,

The 1602 lcd included the I2C adapter on the backside.
Yea, I don't understand it either. Unless someone else happens to know why I guess that I will have to use the arrays.

I am guessing that there are a variety of slightly different add-on boards out there and I noticed that some libraries don't work.

Thanks for the comments on the displays as well.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.