Help with LiquidCrystal_I2C_Spark library with SparkFun 16x2 LCD and serial backpack

Hi,

I am trying to figure out how to use the LiquidCrystal_I2C_Spark library with my SparkFun 16x2 serial LCD (https://www.sparkfun.com/products/10097) and the serial backpack (https://www.sparkfun.com/products/258). I am able to print and adjust the brightness of the screen and print “Hello world!” without any library using the following code:

void setup() {
    Serial1.begin(9600);
    
    delay(100);
    Serial1.write(0x7C); // Command
    Serial1.write(158); // Brightness to 100%
    
    delay(100);
    Serial1.print("Hello world!"); // Clear screen
}

void loop() {

}

However, I would much rather use the library to do more complex actions. I’ve tried the sample program included in the library but nothing happens when I flash it to my Photon.

Thanks!

Here are the helper functions (not a library really) that I use with that product:

void displayHome()
{
    Serial1.write(254); // move cursor to beginning of first line
    Serial1.write(128);
}

void displayMoveTo(int pos)
{
    char posChar;
    if (pos>=1 && pos<=16) {
        posChar = char(128+pos-1);
    } else if (pos>=17 && pos<=32) {
        posChar = char(192+pos-17);
    } else {
        posChar = 128;  // on error, go home
    }
    Serial1.write(254);
    Serial1.write(posChar);
}

void displayClear()
{
    displayHome();
    // clear display fast 32 spaces
    Serial1.write("                "); 
    Serial1.write("                ");
    displayHome();
}

void displayBacklight(int brightpercent)
{
    int mappedBright = map(brightpercent, 0,100,128,157);
    // Make sure range is OK
    if (mappedBright >= 128 && mappedBright <= 157) {
        Serial1.write(124);
        Serial1.write(char(mappedBright));
    }
}

Thanks! This works great. Occasionally the output will get corrupted and I have to unplug/reset the Photon and LCD screen to get it back to normal. I saw another post where this was a problem and they resolved it, I’ll have to look into that. I expanded on your helper functions and added a web based function to print anything to the screen, its pretty cool! Thanks for your help.

int timeDelay = 10;

void setup() {
    Particle.function("print",printToScreen);
    
    Serial1.begin(9600);
    
    displayClear();
    displayBacklight(100);

    printToScreen("    Welcome!");
    
    underlineCursor(false);
    blinkingBox(false);
}

void loop() {
    
}





//================================================
// HELPER FUNCTIONS
//================================================

void underlineCursor(bool val) {
    delay(timeDelay);
    if (val) {
        Serial1.write(0xFE); // Command
        Serial1.write(0x0E);
    } else {
        Serial1.write(0xFE); // Command
        Serial1.write(0x0C);
    }
}

void blinkingBox(bool val) {
    delay(timeDelay);
    if (val) {
        Serial1.write(0xFE); // Command
        Serial1.write(0x0D);
    } else {
        Serial1.write(0xFE); // Command
        Serial1.write(0x0C);
    }
}

void displayHome() {
    delay(timeDelay);
    Serial1.write(0xFE); // Command
    Serial1.write(128);  // Move cursor to beginning of first line
}

void displayMoveTo(int pos) {
    delay(timeDelay);
    char posChar;
    if (pos>=1 && pos<=16) {
        posChar = char(128+pos-1);
    } else if (pos>=17 && pos<=32) {
        posChar = char(192+pos-17);
    } else {
        posChar = 128;  // on error, go home
    }
    Serial1.write(0xFE); // Command
    Serial1.write(posChar);
}

void displayClear() {
    delay(timeDelay);
    Serial1.write(0xFE); // Command
    Serial1.write(0x01); // Clear screen command
}

void displayBacklight(int brightpercent) {
    delay(timeDelay);
    int mappedBright = map(brightpercent, 0,100,128,157);
    // Make sure range is OK
    if (mappedBright >= 128 && mappedBright <= 157) {
        Serial1.write(0x7C); // Command
        Serial1.write(char(mappedBright));
    }
}

int printToScreen(String phrase) {
    displayClear();
    Serial1.print(phrase);
}

Glad you like it! Just so you know, on my display writing 32 spaces was a faster way to clear the display. Your mileage may vary etc.

Oh ok, thanks for the heads up. By the way, what’s the difference between Serial1 and Serial?

Hi @navy2x

Serial is the USB serial port, while Serial1 is the the TX/RX pins which is where you would normally hook-up the display. There is also a Serial2 available on two other pins if you include a header for it.