help with I2C LCD text wrapping function

Hay guys!

So i’ve been trying to find some code that will read in data from a serial connection and split that data into 4, 20 character “blocks” which will then print those “blocks” to a 20x4 I2C controlled LCD panel.

Essentially, i need a function to properly wrap the text on an lcd

so far, i have the last part of the code down and i know there is probably a much better way to do this, but im new to arduino, so i have no idea where to go from this.

char* Data[] = { "This is block 1", "This is block 2", "This is block 3", "This is block 4" };
for (int i = 0; i < 4; i++)
    lcd.setCursor(0, i);

The reason i need a function for this is that the screens buffer is set up in that it will write line 1, then line 3, then line 2, then line 4 when you send more than 20 characters

i found this thread which comes pretty close to what i need, but the function takes no inputs and causes compile errors


I managed to butcher together some code from various other forum posts and got it sort of working. heres the program as it stands:


PIN 2: +5V
PIN 3: SDA - Connect to Arduino analogue PIN 4
PIN 4: SCL - Connect to Arduino analogue PIN 5

//Include the SPI/IIC Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

/* Initialise the LiquidCrystal library. The default address is 0x27 and this is a 20x4 line display */
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int LEDPin = 13;
char* Data[] = { "This is string 01", "This is string 02", "This is string 03", "This is string 04" };
char inData[80]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void setup()
	Serial.println("Serial started");
	pinMode(LEDPin, OUTPUT);
	/* Initialise the LCD */
	Serial.println("Attempting to Initalise LCD (1/2)");
	Serial.println("Attempting to Initalise LCD (2/2)");
	Serial.println("LCD Initalised");
	Serial.println("Enabled backlight");

	for (int i = 0; i < 4; i++)
		lcd.setCursor(0, i);

/* Main program loop */
void loop()
	// when characters arrive over the serial port...
	if (Serial.available()) {
		// wait a bit for the entire message to arrive
		// read all the available characters
		while (Serial.available() > 0) {
			if (index < 79) // One less than the size of the array
				inChar =; // Read a character
				inData[index] = inChar; // Store it
				index++; // Increment where to write next
				inData[index] = '\0'; // Add a Null character to terminate the string
		Serial.flush(); // Flush the serial buffer
		stringToLCD(inData); // Send the data we recieved to the LCD
		index = 0; // Set the index back to 0 ready for the next stream of data

void stringToLCD(char stringIn[]) {
	int lineCount = 0;
	int lineNumber = 0;
	byte stillProcessing = 1;
	byte charCount = 1;
	lcd.setCursor(0, 0);
	Serial.println("echoing string:");

	while (stillProcessing) {
		if (++lineCount > 20) {    // have we printed 20 characters yet (+1 for the logic)
			lcd.setCursor(0, lineNumber);   // move cursor down
			lineCount = 1;
		lcd.print(stringIn[charCount - 1]);

		if (!stringIn[charCount]) {   // no more chars to process?
			stillProcessing = 0;

Why don't you take a look at the LiquidCrystal1.0 library? This library was actually written to deal with 40x4 displays but it works with smaller displays as well. This library also compensates for the inherent LCD line wrap behavior so that it works as you would expect, 1--2--3--4.

To get a copy start here:--> and follow the [u]Downloads[/u] link to get to the latest version.


Thanks for replying!

i would use the library you suggested, but the LCD im using likely wont work because its controlled via I2C and comes with its own custom library (which ive attached should you wish to see it)
i got the module from Here

i will have a go at seeing if i cant somehow combine the 2 librarys together or extract the parts i need from the one you suggested, but i doubt it will work. (21.8 KB)

... but the LCD im using likely wont work because its controlled via I2C ...

In general you will get appropriate responses more quickly if you put tidbits of information like that in the topic title.


Point taken. changes made to OP

I'm using the library HD44780-master in which a method called lineWrap is solving this kind of issue p.e. : lcd.lineWrap();


Answering a 4 year old post ??