Forum rules


Any links to third party websites located on this Website are provided for your convenience only. We have not reviewed each third party website and have no responsibility for such third party websites or their content. We do not endorse the third party websites or make representations about them or any material contained in them. If you choose to access a third party website linked to from this Website, it is at your own risk.

You may not post external links for the purpose of promoting products, services, or websites to which such links are deemed to provide a financial benefit to the poster.

Links to posts within this forum must be deemed to be relevant to the products and services of Hobby Components Ltd and is community.

Reply to topic  [ 1 post ] 
Better LiquidCrystal_I2C library serialDisplay example. 
Author Message

Joined: Thu Sep 04, 2014 2:58 pm
Posts: 2
Post Better LiquidCrystal_I2C library serialDisplay example.
This sketch takes the "serialDisplay" example included with the LiquidCrystal_I2C library, and adds a crude text wrapping function to it.

It's compatable with both the 1602 and 2004 displays. Just change the 2 variables 'Rows' and 'Chars' at the top to match the display you have.

PIN 2: +5V
PIN 3: SDA - Connect to Arduino analogue PIN 4 (or the dedicated SDA pin on the arduino uno R3)
PIN 4: SCL - Connect to Arduino analogue PIN 5 (or the dedicated SCL pin on the arduino uno R3)

const int Rows = 4; // How many lines does your LCD have?
const int Chars = 20; // How may characters can be displayed per line?

//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 20 character, 4 line display (as set by the Rows and Chars variables above) */
LiquidCrystal_I2C lcd(0x27, Chars, Rows); // Create the LCD object

char inData[(Rows*Chars)]; // Allocate some space for the incoming serial string, capped at Rows x Chars (i.e. 20x4, or 80 chars max) to prevent overflow
char inChar; // Where to store each character we read over serial before adding it to 'inData'
byte index = 0; // The current index in the 'inData' array where the character in 'inChar' will be added

void setup()
   Serial.begin(9600); // Open a serial connection
   delay(100); // Wait 100 milli seconds
   Serial.println("Serial started"); // Print "serial started" to the console then move to a new line
   /* Initialise the LCD */
   Serial.println("Attempting to Initalise LCD..."); // Print whats happening to the serial monitor
   lcd.init(); // Call the special I2C LCD initalisation function
   Serial.println("LCD Initalised!"); // Update the serial monitor
   lcd.backlight(); // Turn on the LCD backlight

   lcd.setCursor(0, 1);
   lcd.print("Send something over");
   lcd.setCursor(0, 2);
   lcd.print("serial to see it");
   lcd.setCursor(0, 3);
   Serial.println("Ready to accept serial data");

/* Main program loop */
void loop()
   if (Serial.available()) // when characters arrive over the serial port...
      delay(100); // wait a bit for the entire message to arrive
      while (Serial.available() > 0) // read all the available characters
         if (index < 79) // If the value of 'index' is still one less than the size of the 'inData' array
            inChar =; // Read a character
            inData[index] = inChar; // Store it in 'inChar'
            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); // call the 'stringToLCD function' and pass it 'inData' to format and 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.print("Printing: '"); // Echo back to serial what's going to be shown on the LCD
   Serial.println("' to display");

   while (stillProcessing) // While there are still characters to print
      if (++lineCount > Chars) {    // have we printed 20 characters yet?
         lineNumber++; // If so, move the cursor to the next line
         lcd.setCursor(0, lineNumber);   // move cursor down
         lineCount = 1;

      lcd.print(stringIn[charCount - 1]); // Print the current character at the index of 'charCount' from the 'stringIn' array (which contains the data from 'inData')

      if (!stringIn[charCount])  // no more chars to process?
         stillProcessing = 0; // Set 'stillProcessing' to 0 to exit the while loop
      charCount++; // Add 1 to the charCount to process the next character in the array

Tue Sep 09, 2014 1:56 pm
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Thank you to &
Design created by Florea Cosmin Ionut.
© 2011 All contents of this site are © Hobby Components LTD