Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

lcd

fivdi155MIT3.1.1

Hitachi HD44780 LCD driver

lcd, hd44780, gpio, beaglebone, bbb, bb, raspberry, raspi, rpi, pi, linux

readme

Build Status npm Version Downloads Per Month

lcd

A Node.js Hitachi HD44780 LCD driver for Linux boards like the Raspberry Pi or BeagleBone. Heavily inspired by the Arduino LiquidCrystal library.

Most LCDs compatible with the HD44780 have a sixteen pin interface. This Node.js module uses six of these interface pins for controlling such displays. Register select (RS), enable (E), and four data bus pins (D4-D7). The read/write (R/W) pin is assumed to be tied low to permanently select write mode.

lcd supports Node.js versions 10, 12, 14, 15 and 16.

Installation

npm install lcd

Usage

The following program can be used to make a UTC digital clock.

const Lcd = require('lcd');
const lcd = new Lcd({rs: 45, e: 44, data: [66, 67, 68, 69], cols: 8, rows: 1});

lcd.on('ready', _ => {
  setInterval(_ => {
    lcd.setCursor(0, 0);
    lcd.print(new Date().toISOString().substring(11, 19), err => {
      if (err) {
        throw err;
      }
    });
  }, 1000);
});

// If ctrl+c is hit, free resources and exit.
process.on('SIGINT', _ => {
  lcd.close();
  process.exit();
});

Here it is up and running on a BeagleBone Black wired up to an 8x1 display:

After requiring the lcd module, the above program creates an Lcd object. The constructor function is passed all the necessary information.

The six LCD interface pins used to control the display need to be wired up to six GPIOs on the BeagleBone Black. GPIOs on Linux are identified by unsigned integers. The relevant information for all six GPIOs used here is shown in the following table:

BBB Expansion Header GPIO No. LCD Function LCD Pin No.
P8_7 66 Data Bus Bit 4 11
P8_8 67 Data Bus Bit 5 12
P8_10 68 Data Bus Bit 6 13
P8_9 69 Data Bus Bit 7 14
P8_11 45 Register Select 4
P8_12 44 Enable 6

The constructor function is also told how many columns and rows the display has, eight and one respectively in this case.

It takes several milliseconds to initialize an LCD. The constructor starts the initialization process, but it doesn't wait for it to complete. Instead, a 'ready' event is emitted after the LCD has been completely initialized and is ready for usage.

The 'ready' handler leverages setInterval to execute a function that updates the time displayed on the LCD once a second.

API

Lcd(config)

Returns a new Lcd object which inherits from EventEmitter. A 'ready' event will be emitted when the display is ready for usage.

The config object has these possibilities:

  • cols LCD column count. Defaults to 16.
  • rows LCD row count. Defaults to 1.
  • largeFont Use 5x10 dot font. Defaults to false for 5x8 dot font.
  • rs Register select GPIO number.
  • e Enable GPIO number.
  • data Array of four GPIO numbers for data bus bits D4 through D7.

print(val, [callback])

Converts val to string and writes it to the display asynchronously.

If the optional completion callback is omitted, a 'printed' event is emitted after the operation has completed. The string representation of val is passed to the 'printed' event handler as the first argument. If an error occurs, an 'error' event will be emitted and an error object will be passed to the 'error' event handler as the first argument.

If the optional completion callback is specified, it gets two arguments (err, str), where err is reserved for an error object and str is the string representation of val. If the optional completion callback is specified, no 'printed' or 'error' event will be emitted.

The example print-twice-20x4.js demonstrates how to print two strings in succession.

clear([callback])

Clears display and returns cursor to the home position asynchronously.

If the optional completion callback is omitted, a 'clear' event is emitted after the operation has completed. If an error occurs, an 'error' event will be emitted and an error object will be passed to the 'error' event handler as the first argument.

If the optional completion callback is specified, it gets one argument (err), where err is reserved for an error object. If the optional completion callback is specified, no 'clear' or 'error' event will be emitted.

home([callback])

Returns cursor to home position asynchronously. Also returns display being shifted to the original position.

If the optional completion callback is omitted, a 'home' event is emitted after the operation has completed. If an error occurs, an 'error' event will be emitted and an error object will be passed to the 'error' event handler as the first argument.

If the optional completion callback is specified, it gets one argument (err), where err is reserved for an error object. If the optional completion callback is specified, no 'home' or 'error' event will be emitted.

setCursor(col, row) Moves the cursor to the specified col and row. Numbering for col and row starts at zero.

cursor() Turn cursor on.

noCursor() Turn cursor off.

blink() Turn cursor blink on.

noBlink() Turn cursor blink off.

scrollDisplayLeft() Shift display to the left. Cursor follows the display shift.

scrollDisplayRight() Shift display to the right. Cursor follows the display shift.

leftToRight() Sets cursor move direction to left to right.

rightToLeft() Sets cursor move direction to right to left.

autoscroll() Automatically shift display when data is written to display.

noAutoscroll() Turn automatic shifting off.

close() Frees (unexports) all GPIOs used by the Lcd.

Example "Hello, World!" on an 8x1 display

"Hello, World!" is five characters too long for an 8x1 display, but by moving the cursor to the ninth column, turning autoscroll on, and displaying a new character every 300 milliseconds the text can be scrolled onto the display character by character. Note that an 8x1 display actually has eighty columns but only eight of them are visible.

const Lcd = require('../lcd');
const lcd = new Lcd({rs: 45, e: 44, data: [66, 67, 68, 69], cols: 8, rows: 1});

const print = (str, pos) => {
  pos = pos || 0;

  if (pos === str.length) {
    pos = 0;
  }

  lcd.print(str[pos], err => {
    if (err) {
      throw err;
    }

    setTimeout(_ => {
      print(str, pos + 1);
    }, 300);
  });
};

lcd.on('ready', _ => {
  lcd.setCursor(8, 0);
  lcd.autoscroll();
  print('Hello, World! ** ');
});

// If ctrl+c is hit, free resources and exit.
process.on('SIGINT', _ => {
  lcd.close();
  process.exit();
});

Tested with the following displays

NHD-0108FZ-FL-YBW-33V3

NHD-0420DZ-FL-YBW-33V3

changelog

3.1.1 - Apr 29 2021

  • drop support for node.js 8 and 13, add support for node.js 15 and 16
  • update dependencies

3.1.0 - Apr 25 2020

  • update dependencies (onoff v6.0.0, async v3.2.0, jshint v2.11.0)
  • drop support for node.js 6, add support for node.js 14

3.0.0 - Sep 22 2019

  • update dependencies (onoff v5.0.0)
  • drop support for node.js v4

2.0.6 - Sep 08 2019

  • add node 12 to build
  • remove node 11 from build
  • update dependencies (onoff v4.1.4, async v3.1.0)

2.0.5 - Mar 17 2019

  • update dependencies (onoff v4.1.1, async v2.6.2)
  • document node 12 support
  • lint with jshint
  • add badges
  • add travis build

2.0.4 - Oct 01 2018

  • cleanup
  • remove old test
  • update dependencies (onoff v3.2.2)

2.0.3 - Apr 07 2018

  • update dependencies (onoff v3.0.2)

2.0.2 - Mar 01 2018

  • restore support for node 4

2.0.1 - Mar 01 2018

  • take microsecond execution times into account
  • use native promises
  • implement Lcd as class

2.0.0 - Feb 27 2018

  • document node 9 support
  • update dependencies (onoff v2.0.0, async v2.6.0)
  • drop support for node.js v0.10, v0.12, v5 and v7

1.1.5 - Oct 15 2017

  • node v0.10 or higher required
  • update dependencies (onoff v1.1.8, q v1.5.0)
  • document supported node versions

1.1.4 - Jun 05 2016

  • updated wiring for beaglebone examples
  • updated dependency: onoff 1.0.4 -> 1.1.1

1.1.3 - Mar 20 2016

  • queue async operations and execute them sequentially

1.1.2 - Feb 07 2016

  • Improved documentation and examples
  • Updated dependencies

1.1.1 - Mar 04 2015

  • Got rid of the magic numbers and replaced them with a command map (by nodebotanist)
  • Added test harness and tests (by nodebotanist)

1.1.0 - Jan 10 2015

  • Async methods now support callbacks

1.0.0 - Jan 10 2015

  • Updated dependency: onoff 0.3.2 -> 1.0.0 (GPIO access without superuser privileges on Raspbian)
  • Updated dependency: q 1.0.1 -> 1.1.2

0.2.4 - May 08 2014

  • Delay 1ms more than required #8

0.2.3 - May 01 2014

  • Fallback to nextTick if setImmediate not available #7

0.2.2 - Apr 18 2014

  • Fallback to setTimeout if setImmediate not available #7
  • Documented BeagleBone Ångström prerequisites #8
  • Updated dependency: onoff 0.3.1 -> 0.3.2

0.2.1 - Mar 28 2014

  • v0.11.x process.nextTick compatibility #5
  • Example print-twice-20x4.js added
  • Updated dependency: onoff 0.3.0 -> 0.3.1
  • Updated dependency: q 0.9.7 -> 1.0.1

0.2.0 - Nov 23 2013

  • Asynchronous print #2
  • Print optimization #1
  • Removed write8Bits method

0.1.0 - Nov 18 2013

  • Updated dependencies: onoff 0.2.3 -> 0.3.0

0.0.4 - Nov 08 2013

  • Use || rather than | where appropriate

0.0.3 - Nov 08 2013

  • Example "Hello, World!" on an 8x1 display
  • Lcd constructor is now new-agnostic
  • API documentation

0.0.2 - Nov 07 2013

  • Improved documentation
  • Improved performance-check-20x4

0.0.1 - Nov 07 2013

  • Initial release