Friday, February 24

Sainsmart 1602 i2c


1602 16x2 LCD Hat

One of the interesting things about purchasing items on ebay is having a product arrive which is not exactly how the description said it was. In this case I ordered a 16 character 2 line lcd hat for a raspberry pi. What arrived is definitely "a" display hat, yet there are several differences between what arrived, and the Adafruit 1602 LCD hat I was expecting.

After a lot of effort, web searching, and trial and error. I have found out, what I received is a, or clone of, a Sainsmart 1602 i2c hat. Looking on Amazon, eBay, etc you will find many people ranting about the issues they have using this display hat. Before I spent this time to figure it out, I almost was one of those to prepare a rant.




Also known as the: Sainsmart 1602 I2C (SKU: 20-011-221)


Detecting the LCD Hat

This was as simple as installing i2c-tools. Running the command i2cdetect -y 1. If you are using an older Raspberry Pi 1 series you will probably need to change that command to i2cdetect -y 0. After which it showed the LCD hat was connected at 20h.
apt-get install i2c-tools
i2cdetect -y 1



NodeJS Setup

Since the module we are going to use requires compiling you need to make sure you have "make" and "node-gyp" installed. After installing the tools a simple npm install of Adafruit's "adafruit-i2c-lcd" will get you almost done.
apt-get install -y make node-gyp
npm install adafruit-i2c-lcd --save



lcd.js

/*  don't forget to 

npm install adafruit-i2c-lcd

*/
var LCDPLATE = require('adafruit-i2c-lcd').plate
  , lcd = new LCDPLATE(1, 0x20)
  ;

lcd.sendBytes(0, 0x1F); // Backlight on - Sainsmart 1602

lcd.backlight(lcd.colors.off);
lcd.message('I Work!');

lcd.on('button_change', function(button) {
  lcd.clear();
  lcd.message('Button changed:\n' + lcd.buttonName(button));
  if (button==1) { // select button
    lcd.backlight(lcd.colors.off);
    lcd.sendBytes(0, 0x3F); // Backlight off - Sainsmart 1602
    lcd.clear();

    process.exit();
  }
});

A big thanks goes to 
https://github.com/fehmer/adafruit-i2c-lcd

0 comments :

Post a Comment