Control i2c lcd screens with a Raspberry Pi using the i2c-bus module
npm install raspberrypi-liquid-crystal
printLine ( line : int, text : string )
`
Each method has a synchronous version, an asynchronous version (Promise) and an error-first callback pattern version. (Except getChar() which returns directly a previously created custom character)
Example with the print() method (applies to all methods):
`
print ( text : string ) - Promise
printSync ( text : string ) - Synchronous
printAsync ( text : string, callback : function ) - Error-first callback (classic node)
`
Installation
`
npm install raspberrypi-liquid-crystal
`
Usage
First, set up I2C on your Raspberry Pi. More information about this can be found here:
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
Now, check for the address of the LCD on the I2C bus:
For a rev. 1 board (old), use the following:
`
sudo i2cdetect -y 0
`
For a rev. 2+ board (new), use the following:
`
sudo i2cdetect -y 1
`
This will print out the devices on the I2C bus, such as:
`
root@raspberrypi:/home/pi/raspberrypi-liquid-crystal# sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
`
Here, we see the device on address 0x27.
To use raspberrypi-liquid-crystal, add the following code to your node.js application to import and instantiate the LCD object:
`
const LCD = require('raspberrypi-liquid-crystal');
const lcd = new LCD( 1, 0x27, 16, 2 );
lcd.beginSync();
`
Note that this will set up an I2C LCD on I2C bus 1, address 0x27, with 16 columns and 2 rows.
You can set the cursor position then print text on the screen with the following code:
`
lcd.clearSync();
lcd.printSync( 'Hello' );
lcd.setCursorSync(0, 1);
lcd.printSync( 'World' );
`
To print out a string to the LCD panel using specified line numbers, see the following example code:
`
lcd.clearSync();
lcd.printLineSync(0, 'This is line 1');
lcd.printLineSync(1, 'This is line 2');
`
To create custom characters:
`
lcd.createCharSync( 0,[ 0x1B,0x15,0x0E,0x1B,0x15,0x1B,0x15,0x0E] ).createCharSync( 1,[ 0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00] );
`
More information about creating such custom characters can be found here:
http://www.quinapalus.com/hd44780udg.html
Error Checking
- Promise methods
`
lcd.begin()
.then(() => {})
.catch((e) => console.log(e))
`
- Synchronous methods
`
try {
lcd.beginSync();
} catch (e) {
console.log(e);
}
`
- Error-first callback methods
`
lcd.beginAsync((err) => {
if (err) {
console.log(err);
}
});
`
Examples
See a lot of examples on the examples folder
Basic synchronous example:
`
// Import the module
const LCD = require('raspberrypi-liquid-crystal');
// Instantiate the LCD object on bus 1 address 3f with 16 chars width and 2 lines
const lcd = new LCD(1, 0x3f, 16, 2);
// Init the lcd (must be done before calling any other methods)
lcd.beginSync();
// Clear any previously displayed content
lcd.clearSync();
// Display text multiline
lcd.printLineSync(0, 'hello');
lcd.printLineSync(1, 'world!');
``