Nazanin De's Blog

Projects, Ideas and thoughts

How to set up LCD on Arduino

LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

A register select (RS) pin that controls where in the LCD's memory you're writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD's controller looks for instructions on what to do next._From Arduino

A Read/Write (R/W) pin that selects reading mode or writing mode

An Enable pin that enables writing to the registers

8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you're writing to a register when you write, or the values you're reading when you read.

There's also a display contrast pin (Vo), power supply pins (+5V and Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

In this post I like to show you how to code Arduino with LCD using Javascript JohnnyFive library.

For setting up the circuit you can follow the JohnnyFive documentation here

When you have set your circuit now it is time for some basic coding. Basically we ned three main component in our code:

  • Board
  • LCD
  • JohnnyFive library

So let's bring them in:

var five = require('johnny-five'),  
    board = new five.Board(),
    lcd;

When the board is ready it will trigger an event which we need to implement handler for to manage out main logic.

 board.on('ready', function() {
    //all your logic here
 });

In our handler first we need to instantiate an LCD object with the properties and pins that we desire. The pins we define in object constructor should be the same as the pins we are using for the LCD on the board. Other properties include LCD backlight, number of rows, number of columns , bitMode, dots and lines we would like to define for our LCD. This is definitely depends on your LCD size.

 board.on('ready', function() {
    lcd = new five.LCD({
        pins: [4, 7, 10, 11, 12, 13],
        backlight: 6,
        rows: 2,
        cols: 20
    });
 });

Now the LCD is ready to read your text. There are couple of methods we can leverage from JohnnyFive to work with the LCD:

  • print
  • useChar
  • clear
  • cursor
 board.on('ready', function() {
    var that =  this;
    lcd = new five.LCD({
        pins: [4, 7, 10, 11, 12, 13],
        backlight: 6,
        rows: 2,
        cols: 20
    });
    lcd.useChar("check");
    lcd.useChar("heart");
    lcd.useChar("duck");

    setInterval(function() {
        lcd.clear().print('Daily Note: ');
        lcd.cursor(1, 0).print("Stay Sharp!");

        that.wait(4000, function() {
            lcd.clear().cursor(0, 0).print("Study!!!");
        });
    }, 6000);

    this.repl.inject({
        lcd: lcd
    });
 });

You can use all these methods and generate meaningfull and cool texts on your LCD. LCD can be useful to display information from any other inputs of your Arduino. It can be used to display the temperature of the room or the distance of your bot from the wall. Try it out!