How to Connect Spark to a serial enabled 16x2 LCD [SOLVED]

Hi, I’m trying to connect my spark to a serial enabled 16x2 LCD that I bought at Sparkfun.

My question: how do I output serial commands through the UART serial port?

Any ideas appreciated. Thanks in advance!

Here’s my code so far:

// This #include statement was automatically added by the Spark IDE.
#include "lib2.h"

// This #include statement was automatically added by the Spark IDE.
#include "lib1.h" 
#include "Serial.h"

// Program to blink an LED connected to pin D7
// of the Spark Core. 

// We name pin D7 as led
int led = D7; 


Serial.begin(9600);
Serial.print('hello');

// This routine runs only once upon reset
void setup() 
{
  // Initialize D7 pin as output
  pinMode(led, OUTPUT);
}

// This routine loops forever 
void loop() 
{
  digitalWrite(led, HIGH);   // Turn ON the LED
  delay(1000);               // Wait for 1000mS = 1 second
  digitalWrite(led, LOW);    // Turn OFF the LED
  delay(1000);               // Wait for 1 second
}

And here’s the error message I’m getting:

In file included from ../inc/spark_wiring.h:30:0,
from ../inc/application.h:31,
from /blink.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
/blink.cpp:6:20: fatal error: Serial.h: No such file or directory
compilation terminated.
make: *** [/blink.o] Error 1

Hi @dcdenison

I have the Sparkfun serial 16x2 LCD display in white and I really like it.

First off, Spark has two serial ports, Serial which is the USB host connection serial port and Serial1 which is connected to the core TX and RX pins. You will want Serial1 for the display. (By the way, Serial2 is coming soon if you need more ports.) The display only has a receive pin and that connects to the core’s TX pin.

The Serial1 object is already available for you, so you don’t need any include files, you just need to call Serial1.begin(9600); in the setup() function. Similarly with Serial1.print('hello'); which you have to call inside either the setup() or loop() functions. Also in the C/C++ language used with Spark, a single character can go inside single-quotes but a string of characters requires double-quotes, so Serial1.print("hello"); inside of setup should work fine.

Here is a display test program that I wrote when I first got that display. It prints some hex numbers in the setup() function and the prints “Hello Sparky!” every five seconds, clearing the display first. I packaged up the move and clear commands into functions that I use a lot, so I can post those if you don’t want to write your own.

/* Test of Serial Display */ 


void setup() {
    Serial1.begin(9600);
    delay(500);
    Serial1.write("                "); // clear display
    Serial1.write("                ");
    unsigned long us = micros();  //check micros() function
    Serial1.print(us,HEX);
    delay(100);
    us = micros();
    Serial1.print(us,HEX);
    delay(5000);

}

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

    Serial1.write("                "); // clear display
    Serial1.write("                ");

    Serial1.write(254); // move cursor to beginning of first line
    Serial1.write(128);
    delay(500);
 
    Serial1.write("Hello Sparky!");

    delay(5000);
}
1 Like

Thanks, this sounds really promising. I’ll let you know how it goes. Psyched!

1 Like

So should the code look like this? I haven’t been able to get it to work. But I’m still trying!

  // This #include statement was automatically added by the Spark IDE.
    // #include "lib2.h"
    
    // This #include statement was automatically added by the Spark IDE.
    // #include "lib1.h"
    
    // Define the pins we're going to call pinMode on
    int led = D0;  // You'll need to wire an LED to this one to see it blink.
    int led2 = D7; // This one is the built-in tiny one to the right of the USB jack
    
    // This routine runs only once upon reset
    void setup() {
      // Initialize D0 + D7 pin as output
      // It's important you do this here, inside the setup() function rather than outside it or in the loop function.
      pinMode(led, OUTPUT);
      pinMode(led2, OUTPUT);
    
        Serial1.begin(9600);
        delay(500);
        Serial1.write("                "); // clear display
        Serial1.write("                ");
        unsigned long us = micros();  //check micros() function
        Serial1.print(us,HEX);
        delay(100);
        us = micros();
        Serial1.print(us,HEX);
        delay(5000);
    
    }
    
    
    // This routine gets called repeatedly, like once every 5-15 milliseconds.
    // Spark firmware interleaves background CPU activity associated with WiFi + Cloud activity with your code. 
    // Make sure none of your code delays or blocks for too long (like more than 5 seconds), or weird things can happen.
    void loop() {
     /* digitalWrite(led, HIGH);   // Turn ON the LED pins
      digitalWrite(led2, HIGH);
      delay(5000);               // Wait for 1000mS = 1 second
      digitalWrite(led, LOW);    // Turn OFF the LED pins
      digitalWrite(led2, LOW); 
      delay(9000);           */    // Wait for 1 second in off mode
    Serial1.write(254); // move cursor to beginning of first line
        Serial1.write(128);
    
        Serial1.write("                "); // clear display
        Serial1.write("                ");
    
        Serial1.write(254); // move cursor to beginning of first line
        Serial1.write(128);
        delay(500);
    
        Serial1.write("Hello Sparky!");
    
        delay(5000);
    }

Yes, that looks like it is going to work.

How do you have it hooked up? I have the screw terminals on the display which are labelled 5V (even though mine is 3.3V) GND and RX connected like this:

Core Pin        LCD screw terminal
 3.3v                5v
 GND               GND
 TX                  RX

That last one is important–the core transmits, the LCD receives!

1 Like

Hey, got it going! I think the problem was connectivity at my local makerspace.

Tried it again at home – success!

thanks again.

2 Likes