Introduction: Tutorial 7: the LCD Screen (Liquid Crystal Display)

In this 7th Tutorial, we finally use a device that uses the IC2 port. Such devices typically are a little trickier to set up, but then have a whole range of functions/methods, making them overall more powerful devices to use.

Supplies

Step 1: Build

This is a very easy build. Just connect the LCD Screen to the Grove Shield with a Grove cable.

Step 2: Code (Project 1: Hello World)

For the first test of the screen, this is the code you need:


//Add Libraries
#include <Wire.h>
#include "rgb_lcd.h"

//Global Variables

//Create Objects
rgb_lcd screen;

//Setup
void setup() {
 screen.begin(16, 2);
 screen.print("Hello World");
}

//Main Loop
void loop() {
  
}


Let's go through it.


#include <Wire.h>
#include "rgb_lcd.h"

This device uses 2 libraries. So we just include them both.


rgb_lcd screen;

The class is 'rgb_lcd'. Note the name of my object is just 'screen'. Unlike the other devices we have used so far, we don't include brackets and a number with the name.


 screen.begin(16, 2);
 screen.print("Hello World");

While we didn't attach it to a single pin when we created the object... we do need to start the I2C line that allows the Arduino and the LCD Screen (which has its own build-in processor) to communicate with each other. That's why we do screen.begin(16,2). When we do serial.begin(9600), we start communication between the Arduino and the computer (via the serial port). Here, the (16,2) is just saying our screen size (16 columns, 2 rows).


You may note, that the main loop is empty here. We put our actual code for what we want the screen to display in the setup. This is because the setup is a ONCE ONLY action, while the LOOP will keep going. As I only want to display "Hello World" and just leave it there on the screen, it seems fine to leave it in setup.


screen.print("Hello World");

This works the same as printing to the Serial. But start with the object's name (here I called it 'screen') followed by the print method/function, and inside the brackets, in quotation marks, state what you want to print.

Be aware, if you have a variable, you can also type a variable name inside the brackets and it will print the VALUE of that variable. NOTE: You don't use quotation marks with variables, only with a string you want to print.

Step 3: Upload and Check Out

Upload the program to the Arduino and check out the screen displaying 'Hello World'.

If you forgot how to upload to the Arduino Board, then make sure the Arduino is connected to the laptop via USB and then follow these instructions in the PDF below.

Step 4: Code (Project 2 - Rocket Countdown)

ok, try adding this code instead and uploading, and noting what it does.

(Note, the code doesn't use a FOR LOOP to count down (as I want to keep changing the screen colour each time). If you don't care about the colour of the screen, try and make it count down from 10 using a FOR LOOP.


//Add Libraries
#include <Wire.h>
#include "rgb_lcd.h"

//Global Variables


//Create Objects
rgb_lcd screen;

//Setup
void setup() {
 screen.begin(16, 2);
  
 screen.print("10");
 delay(1000);
  
 screen.clear();
 screen.setCursor(0,0);
 screen.print("9");
 screen.setRGB(255,0,0);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("8");
 screen.setRGB(0,255,0);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("7");
 screen.setRGB(0,255,255);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("6");
 screen.setRGB(255,155,0);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("5");
 screen.setRGB(155,0,255);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("4");
 screen.setRGB(0,0,255);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("3");
 screen.setRGB(255,0,0);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("2");
 screen.setRGB(0,255,0);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("1");
 screen.setRGB(255,255,255);
 delay(1000);

 screen.clear();
 screen.setCursor(0,0);
 screen.print("Blast Off");
 screen.setRGB(255,0,0);
 delay(1000);
  
}

//Main Loop
void loop() {
  
}

Step 5: Play Around

Try your own little bits of code to test out some of the functions/methods for the LCD screen, as per the Wiki.