I bought one of these
http://www.ebay.com/itm/New-Arduino-IIC-I2C-TWI-Serial-LCD-2004-20-4-Module-Shield-Display-Blue-White-/320949546464?pt=LH_DefaultDomain_0&hash=item4aba156de0
a 20 char 4 line LCD
I bought one of these
http://www.ebay.com/itm/New-Arduino-IIC-I2C-TWI-Serial-LCD-2004-20-4-Module-Shield-Display-Blue-White-/320949546464?pt=LH_DefaultDomain_0&hash=item4aba156de0
a 20 char 4 line LCD
Hi Jim,
Before you start you might want to take a look at "tech2077.blogspot.com/2012/06/running-hc44870-lcd-over-i2c-on.htm". It works, but I want to modify it to work with Adafruit’s "Adafruit_Python.py. Not successful yet, but you might have better luck or come up with something else that does. GOOD LUCK!
Jim
I have had a problem with this type LCD, as it is a 5V and not 3.3.
I can read the LCD text at 2 line display; at 4 line display however need a torch to be shined on the LCD to see that the text appeared
to make sure it is detected, execute
i2cdetect -y -r 0 (assuming connected to pin 19+20 from memory)
my LCD had address 0x20; so commends followed are as such
i2cset -y 1 0x20 0x00 0x00 → set all pins to output
i2cset -y 1 0x20 0x09 0xff ->should see LCD backlight go on
i2cset -y 1 0x20 0x09 0x00 ->should see LCD backlight go off
However the code, which I have got it work is; eclipse cross compiled
#include
#include
#include
#include
#include
#include
#include
#include
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
using namespace std;
#define output “out”
#define input “in”
//i2c
#define usei2cLCD 1
#define data 0
#define text 1
#define raw 1
int i;
int r;
int fd;
unsigned char i2ccommand[2];
useconds_t delay = 2000;
char *dev = “/dev/i2c-1”;
int addr = 0x20;
//i2c LCD
#define LCD_LINE_1 0x80
#define LCD_LINE_2 0xC0
#define LCD_LINE_3 0x94
#define LCD_LINE_4 0xD4
#define LCD_CLEAR 0x01
#define backlight 0x02
#define DB7 0x04
#define DB6 0x08
#define DB5 0x10
#define DB4 0x20
#define db7 DB7
#define db6 DB6
#define db5 DB5
#define db4 DB4
#define E 0x40
#define RS 0x80
//ADC coms
#define ADCstring “BB-ADC”
char command[] = “echo BB-ADC > /sys/devices/bone_capemgr.”*"/slots";
#define useADC 1
#define ADC “AIN”
unsigned char j,k = 0;
fstream workfile,workfile1;
string line,line1;
int analog[7]={0,}; //ADC actual values
int analogavg = 500; //ADC actual values
void init();
void adcmeasure();
int i2csend4bit(int location, int value,int textdisplay);
int i2csend(int location, int value,int instruction);
int i2csend(int location, int value,int instruction,unsigned char direct);
struct HexCharStruct
{
unsigned char c;
HexCharStruct(unsigned char _c) : c(_c) { }
};
inline std::ostream& operator<<(std::ostream& o, const HexCharStruct& hs)
{
return (o << std::hex << (int)hs.c);
}
inline HexCharStruct hex(unsigned char _c)
{
return HexCharStruct(_c);
}
int main ()
{
init();
cout << “Hello World!” << endl;
while(1)
{
adcmeasure();
}
return 0;
}
void init()
{
if(usei2cLCD)
{
sleep(1);
i2csend(0x00,0x00,data,raw); //turn on direction to all outputs on MCP23008
i2csend(0x09,0x00,data,raw); //turn off all outputs on MCP23008
//4 bit mode select
i2csend(0x09,(backlight+DB5+DB4),data); //4 bit mode execute ->0011
i2csend(0x09,(backlight+DB5+DB4),data); //4 bit mode execute ->0011
i2csend(0x09,(backlight+DB5+DB4),data); //4 bit mode execute ->0011
i2csend(0x09,(backlight+DB5),data); //4 bit mode execute ->0010
//4 bit mode + 2 line LCD
i2csend(0x09,(backlight+DB5),data); //4 bit mode execute ->0010
i2csend(0x09,(backlight),data); //4 bit mode execute ->0100 ?? 2 line mode
// i2csend(0x09,(backlight+DB7),data); //4 bit mode execute ->0100 ?? 4 line mode
//curser+display shift
i2csend(0x09,(backlight),data); //4 bit mode execute ->0000
i2csend(0x09,(backlight+DB7+DB6),data); //4 bit mode execute ->1100
i2csend(0x09,(backlight),data); //4 bit mode execute ->0000
i2csend(0x09,(backlight+DB6+DB5),data); //4 bit mode execute ->0110
//clear screen and return to zero
i2csend(0x09,(backlight),data); //4 bit mode execute ->0000
i2csend(0x09,(backlight+DB4),data); //4 bit mode execute ->0001
sleep(1);
// i2csend4bit(0x09,LCD_LINE_1,data);
// i2csend4bit(0x09,‘t’,text);
}
if(useADC)
{
system(“find /sys/devices/ -name bone_capemgr.* > /tmp/adctmp”);
workfile.open ("/tmp/adctmp");
if ( ! workfile.is_open() )
{
cout <<" Failed to open adcstatus at init" << endl;
}
else
{
while (getline(workfile,line))
{
line.append("/slots");
workfile1.open(line.c_str());
if ( ! workfile1.is_open() )
{
cout <<" Failed to open workfile1" << endl;
}
else
{
workfile1 << ADCstring;
}
workfile1.close();
}
}
workfile.close();
}//ADC unable
}
void adcmeasure()
{
if(useADC)
{
system(“find /sys -name *in_voltage?_raw* > /tmp/adctmp”);
workfile.open ("/tmp/adctmp");
if ( ! workfile.is_open() )
{
cout <<" Failed to open adcstatus" << endl;
}
else
{
i=0;
while (getline(workfile,line))
{
workfile1.open(line.c_str(), std::ifstream::in);
if ( ! workfile1.is_open() )
{
cout <<" Failed to open workfile1" << endl;
}
else
{
getline (workfile1,line1);
stringstream(line1) >> analog[i];
}
i++;
workfile1.close();
}
}
cout <<“analog0 " << analog[0] << endl <<” analog1 (GND)" << analog[1] << endl <<" analog2 " << analog[2] << endl<<" analog3 " << analog[3] << endl<<" analog4 (1.8V)" << analog[4] << endl <<" analog5 (SENSOR) " << analog[5] << endl<<" analog6 " << analog[6] << endl<<" analog7 " << analog[7] << endl;
workfile.close();
analogavg = ((analogavg+analog[5])/2);
cout <<“Average ADC result is " << analogavg <<” ADC results updated" << endl;
i2csend4bit(0x09,LCD_LINE_1,data);
i2csend4bit(0x09,‘t’,text);
i2csend4bit(0x09,‘h’,text);
i2csend4bit(0x09,‘e’,text);
i2csend4bit(0x09,’ ‘,text);
i2csend4bit(0x09,‘s’,text);
i2csend4bit(0x09,‘e’,text);
i2csend4bit(0x09,‘n’,text);
i2csend4bit(0x09,‘s’,text);
i2csend4bit(0x09,‘o’,text);
i2csend4bit(0x09,‘r’,text);
i2csend4bit(0x09,’ ‘,text);
i2csend4bit(0x09,‘i’,text);
i2csend4bit(0x09,‘s’,text);
i2csend4bit(0x09,’:’,text);
i2csend4bit(0x09,’ ',text);
i2csend4bit(0x09,(analog[5]/1000)%10+48,text);
i2csend4bit(0x09,(analog[5]/100)%10+48,text);
i2csend4bit(0x09,(analog[5]/10)%10+48,text);
i2csend4bit(0x09,(analog[5]/1)%10+48,text);
}
}
int i2csend(int location, int value,int instruction)
{
if(instruction)
value |= RS; //set RS to 1
else
value &= ~RS; //set RS to 0
fd = open(dev, O_RDWR );
if(fd < 0)
{
perror(“Opening i2c device node\n”);
return 1;
}
usleep(30);
r = ioctl(fd, I2C_SLAVE, addr);
if(r < 0)
{
perror(“Selecting i2c device\n”);
return 1;
}
usleep(30);
//E->0, E->1, E->0
i2ccommand[0] = location;
i2ccommand[1] = value & ~E; //set E to zero
// cout << (bitset<8>)i2ccommand[1] << endl;
r = write(fd, &i2ccommand, 2);
usleep(500);
i2ccommand[0] = location;
i2ccommand[1] = value | E; //set E to 1
// cout << (bitset<8>)i2ccommand[1] << endl;
r = write(fd, &i2ccommand, 2);
usleep(600);
i2ccommand[0] = location;
i2ccommand[1] = value & ~E; //set E to zero
// cout << (bitset<8>)i2ccommand[1] << endl << endl;
r = write(fd, &i2ccommand, 2);
usleep(6000);
close(fd);
return r;
}
int i2csend(int location, int value,int instruction,unsigned char direct)
{
fd = open(dev, O_RDWR );
if(fd < 0)
{
perror(“Opening i2c device node\n”);
return 1;
}
usleep(30);
r = ioctl(fd, I2C_SLAVE, addr);
if(r < 0)
{
perror(“Selecting i2c device\n”);
return 1;
}
usleep(300);
i2ccommand[0] = location;
i2ccommand[1] = value;
// cout << (bitset<8>)i2ccommand[1] << endl << endl;
r = write(fd, &i2ccommand, 2);
usleep(6000);
close(fd);
return r;
}
int i2csend4bit(int location, int value,int textdisplay)
{
//1st nibble + execute
i2ccommand[0] = location;
i2ccommand[1] = 0;
if((value >> 7) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB7 ; //DB7 high
if((value >> 6) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB6 ; //DB6 high
if((value >> 5) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB5 ; //DB5 high
if((value >> 4) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB4 ; //DB4 high
if(textdisplay)
i2ccommand[1] = i2ccommand[1] + RS ; //If not instruction, set to text mode
i2ccommand[1] = i2ccommand[1] + backlight ; //backlight on
// cout << (bitset<8>)i2ccommand[1] << endl;
i2csend(i2ccommand[0],i2ccommand[1],textdisplay);
//2nd nibble+ execute
i2ccommand[1] = 0;
if((value >> 3) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB7 ; //DB7 high
if((value >> 2) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB6 ; //DB6 high
if((value >> 1) & 0x01)
i2ccommand[1] = i2ccommand[1] + DB5 ; //DB5 high
if(value & 0x01)
i2ccommand[1] = i2ccommand[1] + DB4 ; //DB4 high
if(textdisplay)
i2ccommand[1] = i2ccommand[1] + RS ; //If not instruction, set to text mode
i2ccommand[1] = i2ccommand[1] + backlight ; //backlight on
// cout << (bitset<8>)i2ccommand[1] << endl;
i2csend(i2ccommand[0],i2ccommand[1],textdisplay);
// cout<<"value is "<< value << " i2ccommand[1] " << i2ccommand[1]+0 << " back " << backlight + E + DB4<< endl;
}
Hi Jim
So I looked at a variety of other i2c based LCD codes (there seem to be lots for the Raspberry Pi), but I didn’t like them.
So you can try mine.
You can do the following on the BBB:
git clone https://github.com/dhylands/python_lcd.git
cd python_lcd
sudo pip install -e .
sudo python lcd/i2c_lcd.py
Running i2c_lcd.py runs some test code which puts some stuff up on the LCD, and after 3 seoncds shows the current data and time and then cycles through a variety of backlight/display on/off combinations.
I used a 3.3v to 5v level shifter.
I wound up using smbus directly rather than the Adafruit_I2C (but installing Adafruit_I2C will install smbus)
I threw some photos together here:
https://picasaweb.google.com/115853040635737241756/PythonI2CLCD?authkey=Gv1sRgCLyZoJ3_uPjiXA#
Coming from the BeagleBone Black the wire colors are:
Black: P9-1 GND
Red: P9-3 3.3v
Orange: P9-5 5v
Yellow: P9-19 SCL
White: P9-20 SDA
And the colors going to the LCD are:
Black: GND
Red: 5v
White: SDA
Yellow: SCL
Hi Dave, once again THANKS for helping. I am basically a newbie, my only experience is playing with basic on my imsai, 30 years ago. Boy have things changed! I had wanted to use Adafruit’s P)ython_IO for the other functions it had. However I wanted to try your code, it may be all I need. I am getting an error on the “git clone” that sez “fatal: HTTP request failed”.
What is Shuswap near? You must be even colder than Buena Vista, Colorado.
Hi Jim,
Jim
Hi Jim,