上周重新拿出吃灰的树莓派,装了系统,接下来就是写传感器的demo,demo最主要就是找到依赖库,接线和代码并不难。
好了,进入正题,本文主要写用树莓派点亮LCD1602,使用nodejs 10。
先来一个成品图,和下面接线图有些许差别,加了背光
环境准备
- Raspberry 3b+, ubuntu20.04
- LCD 1602
- 3脚电位器、若干杜邦线
- NodeJs 10.19.0
LCD1602的说明见Arduino连接LCD1602显示屏,树莓派环境的准备见树莓派基础配置。
接线如下图
代码
const Lcd = require('lcd');
const lcd = new Lcd({ rs: 20, e: 21, data: [19, 13, 6, 5], cols: 16, rows: 2 });
// 打印函数
const printLcd = (msg) => {
console.log(`LCD print msg: {msg}`)
lcd.setCursor(0, 0);
lcd.print(msg, err => {
if (err) {
throw err;
}
});
}
lcd.on('ready', () => {
printLcd('hello world')
//设置定时器,使程序不退出控制台
setInterval(() => {
console.log('keep alive')
}, 1000);
});
// 当按ctrl+c,释放资源并退出
process.on('SIGINT', _ => {
console.log("exit")
lcd.close()
process.exit()
});