Table of Contents

B1: Basic operations on the 4x20 LCD screen

Whatever you do, you expect to have some output of the system. Sometimes there is a blinking LED, sometimes information about connected/disconnected network and some other time simply trace algorithm progress. In laboratories where you have physically connected MCU to your programming device (i.e. computer), you usually would choose Serial port to report about what is going on. However here all you have is access via the video stream. Perhaps those are reasons you will use this LCD display in your every lab work.

Note, the video stream from the camera is limited in bandwidth and presents some 5-10fps maximum (usually around 1 fps) so you shouldn't change display content nor LED state faster than once every 2 seconds, to let you notice any change!

Target group

This hands-on lab guide is intended for the Beginners but other target groups may benefit from it, treating it as a tool for advanced projects.

Prerequisites

There are no other prerequisites than LCD I2C library. Mind, LCD is controlled via the I2C bus. LCD Display is 4×20 characters. LCD is controlled via I2C extender: LCM1602. The I2C extender address is 0x3F or 0x27 (depends on the laboratory node, please refer to the documentation!) and the I2C bus is connected to the pins D1 and D2 (D1 is SCL and D2 is SDA).

There are two types of I2C extenders differing their I2C address:
  • Nodes 1 through 5, 8 and 9 use 0x3F
  • Nodes 10 and 11 use 0x27

Scenario

Initialize LCD screen, clear it then write some fancy text on it, i.e. “Hello IOT!” in the first line then your first name in the second line and the name of the city you're in, in the third. In the fourth line, print right-aligned number of loop iterations (delay it for 1 second between updates - to observe video stream lag and delays). Note, delays are provided in ms, not in s.

Result

You should see the texts and ticking counter in the video stream.

Start

There are no special steps to be performed.

Steps

Step 1

Include LCD driver library:

#include <LiquidCrystal_I2C.h>
Step 2

Instantiate software controler component for the LCD display:

LiquidCrystal_I2C lcd(0x3F,20,4);  // set the LCD address to 0x3F for nodes 1 through 5, 8 and 9
//LiquidCrystal_I2C lcd(0x27,20,4); //for nodes 10 and 11 only!
                                   // for a 20 chars and 4 line display
Step 3

Declare some variables: counter i, its length n and buffer for the into to string conversion:

int i = 0;
char buffer [50];
int n;

We will use them in further part of the code.

Step 3

Initialize display - we suggest to do it in setup() function:

...
  lcd.init(D2,D1);                 // initialize the lcd I2C
  lcd.backlight();                 // switch on the backlight
...
Step 4

Clear the contents, set cursor and draw static text - still in setup() function:

...
  lcd.home();
  lcd.print("Hello IOT!");
  lcd.setCursor(0, 1);
  lcd.print("James Bond here");
  lcd.setCursor(0,2);
  lcd.print("London");
...
Step 5

Implement loop() to draw number of loop executions:

...
  i++;
  n=sprintf(buffer,"%d",i);
  lcd.setCursor(20-n,3);
  lcd.print(i);
  delay(1000);
...
sprintf uses number of wildcards that are rendered with data. Refer to the c/c++ documentation on sprintf. Here %d means: having integer number render it to string and as we do not specify number of digits, it is up to the engine to convert it properly.\\delay(time) is measured in milliseconds.

Result validation

Observe text, its position and counter ticking in lower, right corner.