LCD (Liquid Crystal Display)

Overview

An LCD (Liquid Crystal Display) is a versatile module capable of displaying characters and shapes. The 16x2 LCD module, as depicted in Figure 5.16, can display up to 32 characters, with each character slot being a 5x7 dot matrix. This module uses a parallel interface for communication, although it also supports I2C interfaces, which are more efficient in terms of wiring. The LCD is commonly used to display data from various sensors, such as compass heading, temperature, and pressure.

Figure 5.16. A 16x2 parallel LCD module Figure 5.16. A 16x2 parallel LCD module

In addition to the 16x2 module, the Nokia 5110 display is another type of LCD used in experiments. It has a resolution of 48x84 pixels and is controlled via an SPI interface using the PCD8544 module in Espruino, allowing for the display of both text and graphics.

Wiring and Setup

Connecting a 16x2 parallel LCD module involves using a significant number of jumper wires. The module typically has 16 pins, with an LED backlight, as shown in Figure 5.13. The wiring diagram in Figure 5.17 provides guidance on how to connect the LCD to a breadboard, ensuring that the leftmost pin of the LCD module is connected to the leftmost row of the breadboard.

Figure 5.13. 16x2 parallel LCD modules are available from many manufacturers. They have 16 pins and an LED backlight. Figure 5.13. 16x2 parallel LCD modules are available from many manufacturers. They have 16 pins and an LED backlight.

Figure 5.17. Wiring diagram of timer including potentiometer and LCD placement Figure 5.17. Wiring diagram of timer including potentiometer and LCD placement

Power connections are crucial for the LCD's operation. As shown in Figure 5.19, LCD pins 1, 5, and 16 should be connected to the ground power rail, while pins 2 and 15 should be connected to the source power rail. These connections power the LCD and its LED backlight.

Figure 5.19. Wiring diagram showing the breadboard power connections to the LCD Figure 5.19. Wiring diagram showing the breadboard power connections to the LCD

Programming the LCD

Basic Display

To test the LCD, you can use the Johnny-Five library to instantiate an LCD object and display text. The following code snippet demonstrates how to print "hello, world" and "hello, again" on a 16x2 LCD:

const five = require('johnny-five');
const board = new five.Board();

board.on('ready', () => {
  const downButton = new five.Button(2);
  const upButton   = new five.Button(3);
  const goButton   = new five.Button(4);
  const lcd = new five.LCD([7, 8, 9, 10, 11, 12]);      // Instantiates a Johnny-Five LCD object
  lcd.cursor(0, 0).print('hello, world');               // Positions the cursor on the 0th line, 0th position (top left) and prints “hello, world”
  lcd.cursor(1, 0).print('hello, again');               // Writes “hello, again” to the LCD’s second line, 0th position
});

Displaying Sensor Data

The LCD can also be used to display data from sensors, such as a compass. The following code snippet shows how to display compass heading data on the LCD, updating every second:

board.on('ready', () => {
  const compass = new five.Compass({ controller: 'HMC5883L' });
  const lcd = new five.LCD({ pins: [7, 8, 9, 5, 6, 12] });

  lcd.useChar('circle'); // Tells the LCD to use the special circle character (for a degree marker)

  function update() {
    var display = formatDisplay({
      heading: correctForDeclination(compass.heading, DECLINATION),
    });
    if (!lastDisplay || (lastDisplay.join('') != display.join(''))) {
      lcd.clear();
      lcd.cursor(0, 0).print(display[0]);
      lcd.cursor(1, 0).print(display[1]);
      lastDisplay = display;
    }
  }

  board.loop(UPDATE_FREQ_MS, update);
});

Timer Application

The LCD can be integrated into a timer application, displaying the remaining time. The following code snippets illustrate the setup and operation of a timer using the LCD:

Setting up the Timer:

var lcd;
var progress = 0; // Current timer progress
var frameDuration = 200; // ms (5fps)
var timerSeconds = 10;
var timerLength = timerSeconds * 1000 / frameDuration;
var timerActive = false;

function onInit() {
  SPI1.setup({ sck: B3, mosi: B5 });
  lcd = require('PCD8544').connect(SPI1, B13, B14, B15);
}

onInit();

Timer Operation:

board.on('ready', () => {
  const downButton = new five.Button(2);
  const upButton = new five.Button(3);
  const goButton = new five.Button(4);
  const lcd = new five.LCD([7, 8, 9, 10, 11, 12]);
  const alertLED = new five.Led(6);
  var remaining, timer, timeString, lastTimeString, timestamp, lastTimestamp;

  function init() {
    remaining = DEFAULT_TIMER;
    lastTimeString = '00:00';
    timeString = '';
    showRemaining();
  }

  function showRemaining() {
    var minutes, seconds, minPad, secPad;
    minutes = Math.floor(remaining / 60000);
    seconds = Math.floor((remaining % 60000) / 1000);
    minPad = (minutes < 10) ? '0' : '';
    secPad = (seconds < 10) ? '0' : '';
    timeString = `${minPad}${minutes}:${secPad}${seconds}`;
    if (timeString != lastTimeString) {
      lcd.cursor(0, 0).print(timeString);
    }
  }

  function start() {
    lcd.clear();
    timestamp = Date.now();
    timer = setInterval(tick, 250);
    tick();
  }

  function pause() {
    timer = clearInterval(timer);
    lcd.cursor(0, 9).print('PAUSED');
  }

  function tick() {
    lastTimestamp = timestamp;
    timestamp = Date.now();
    remaining -= (timestamp - lastTimestamp);
    if (remaining <= 0) {
      timer = clearInterval(timer);
      init();
    }
    showRemaining();
  }

  goButton.on('press', () => {
    if (!timer) {
      start();
    } else {
      pause();
    }
  });

  init();
});

These examples demonstrate the flexibility and utility of LCDs in displaying information and interacting with various hardware components.

FAQ (Frequently asked questions)

How many characters can a 16x2 LCD module display?

What is the character slot configuration of a 16x2 LCD module?

What type of interface does a 16x2 LCD module use for communication?

What does a 16x2 parallel LCD module look like?

How do you print text on a specific line of an LCD using Johnny-Five?

What does the code lcd.cursor(0, 0).print('hello, world'); do?

What does the code lcd.cursor(1, 0).print('hello, again'); do?

What types of interfaces does the LCD support?

Why is I2C more efficient than parallel interfaces for LCDs?

How do you instantiate a parallel LCD in Johnny-Five?

What data is displayed on the LCD from the compass?

How often is the compass data updated on the LCD?

Why are compass readings corrected for magnetic declination?

What is the resolution of the Nokia 5110 LCD used in the experiments?

What interface is used to control the Nokia 5110 LCD?

What module is used to control the Nokia 5110 LCD in Espruino?

How is the timer duration calculated in the code?

How is the SPI interface configured for the LCD?