patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Help with 16x2 LCD

Mon Apr 14, 2014 9:33 pm

Hello,

I was put on a project for work that I cant seem to get working. We are building a localhost device that needs to display raspbery pi server IP address on the 16x2 LCD screen.

I have my device running Pidora.

I have a "Blue 16x2 LCD Display with I2C Backpack and cables Arduino Raspberry Pi 1602".
(purchased here http://www.ebay.com/itm/181357834990?ss ... 1497.l2649)

The LCD is wired through the i2c backpack to the following pins:
http://www.gallot.be/wp-content/uploads ... ection.png

I'm looking for an extremely simple hello world C program that I can modify to show IP address. I do not program in python and installing the libraries can potentially be time consuming for this project.

I have multiple libraries such as WiringPi and bcm2835.

Any help would be greatly appreciated.

Thank you.

User avatar
JRV
Posts: 270
Joined: Mon Apr 02, 2012 1:39 pm
Location: Minneapolis, MN

Re: Help with 16x2 LCD

Tue Apr 15, 2014 12:46 pm

Info for using wiringPi is here: http://wiringpi.com/examples/adafruit-r ... -wiringpi/

The example program is: /home/USER/wiringPi/examples/lcd-adafruit.c

patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Re: Help with 16x2 LCD

Tue Apr 15, 2014 5:55 pm

I understand that, I've been to those links.

I'm currently using the wiringpi library.

My problem is getting text to the LCD to display text.

I believe the device I have is different than the one you posted:

http://i.ebayimg.com/00/s/MTA2NFgxNjAw/ ... r/$_57.JPG

patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Re: Help with 16x2 LCD

Tue Apr 29, 2014 11:46 pm

Any help please?

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Wed Apr 30, 2014 12:09 am

From the product description you linked to:"Only requires 4 wires (only 2 I/O pins needed)
GND, VCC (+5V), SDA, and SCL"
So start by connecting Vcc to the Pi's 5V rail.The Adafruit code should work with this, but make sure the I2C address is correct for this module. You can check it with i2cdetect -y 1. and adjust the code if needed.
And if the code does work, consider Adafruit for your next purchase. It may save you time and aggravation, and supports their free code libraries.

[EDIT: Nope, I'm totally wrong. Looks like that module uses the PCF8574 I2C expander, not the MCP23017 that the Adafruit (and Gordon's wiringpi) code is written for. This will take some more Googling, to find code for this chip. I'm aware of a Java class, if that helps. Can you contact the seller?]

[EDIT: Corrected part number ]
Last edited by Douglas6 on Wed Apr 30, 2014 11:34 pm, edited 1 time in total.

patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Re: Help with 16x2 LCD

Wed Apr 30, 2014 5:38 am

Hello,

Thank you so much for your reply. I have contacted the seller and was told to google... :oops:

I've found some good stuff, but I really dont think this particular LCD screen wants to work.

If anyone knows a direct link to an LCD screen that requires minimal soldering AND is very cheap AND comes with a simple hello world program, please let me know.

Until then, I will be working on making this current product I have work.

riklaunim
Posts: 265
Joined: Tue Apr 22, 2014 7:34 pm

Re: Help with 16x2 LCD

Wed Apr 30, 2014 6:14 am

Those I2C backpacks are mostly for LiquidCrystal library (Arduino and alike). I didn't saw any nice library for such displays in Python. Adafruit LCD boards as far as I know share some if not all with those I2C Arduino LCDs so the code may work out of the box.

The most easy would be a LCD via UART/serial which I got working without any monstrous library like LiquidCrystal :)

User avatar
DougieLawson
Posts: 38780
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Help with 16x2 LCD

Wed Apr 30, 2014 6:23 am

Are you sure that the I2C chip is a PCF8474? Only there is no evidence of any datasheet for that chip when I plug it's number into Google.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Help with 16x2 LCD

Wed Apr 30, 2014 7:56 am

It says PCF8574 in eBay description.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Wed Apr 30, 2014 11:29 pm

DougieLawson wrote:Are you sure that the I2C chip is a PCF8474? Only there is no evidence of any datasheet for that chip when I plug it's number into Google.
Sorry, fat fingers on a tablet. PCF8574. Datasheet: http://www.ti.com/lit/ds/symlink/pcf8574.pdf

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Thu May 01, 2014 1:16 am

patthehuman wrote:If anyone knows a direct link to an LCD screen that requires minimal soldering AND is very cheap AND comes with a simple hello world program, please let me know.
I think you can get your current module working, with just a bit of coding. It will be a good exercise in I2C programming.

As an alternative to others, who don't mind a bit of soldering, there's another option, kinda cute. With an inexpensive FF32 chip from FlyFish Techonologies, and a cheap 'bare' HD44780 module, about $6 on DealExtreme, and a few capacitors etc., you can cheaply build a USB character LCD device. Plug it in, plug it out. Nice for occasional diagnostics or statistics gathering from a headless box, for example. There's Python code on the FlyFish Technologies website, Linux only right now, but it could potentially be adapted for Windows or Mac.

patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Re: Help with 16x2 LCD

Mon May 05, 2014 5:47 am

Thanks for the reply guys.

I understand that I may be able to get this thing working, the problem is how?

I have minimal knowledge of GPIO programming.

I'm really looking for help with a simple hello world program and, and how to "identify" the pins to send to the WiringPi library.

I understand I can use gpio commands to view the device location which is 0x27. This is about all i've gotten to work so far.

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Mon May 05, 2014 3:53 pm

You can start with this thread: http://www.raspberrypi.org/forums/viewt ... 7&p=395136. At the end is a link to this github, which has some code that might help. If that doesn't work, you'll have to do more googling on 'PCF8574 HD44780'. Or (heaven forfend) start tackling the datasheets. I don't have that chip, so can't be of much more help. You may also need to figure out just how the PCF8574 is wired up to the HD44780 (8 data lines? 4 data lines? RS, CE are the important ones), and adjust the code accordingly. Also look into examples like Adafruit's and Gordon's wiringpi, that use the MCP23017 expander. Things won't be much different for the PCF8574, maybe just some bytes to initialize the ports of the PCF8574.

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Mon May 05, 2014 4:16 pm

On the other hand, if your time and aggravation are worth more than $20, you can just buy one of these, which comes with code and step-by-step tutorials for getting it up and running on a Pi.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5578
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Help with 16x2 LCD

Mon May 05, 2014 7:44 pm

[quote="patthehuman"]Hello,
I was put on a project for work that I cant seem to get working. We are building a localhost device that needs to display raspbery pi server IP address on the 16x2 LCD screen.
I have my device running Pidora.
I have a "Blue 16x2 LCD Display with I2C Backpack and cables Arduino Raspberry Pi 1602".
(purchased here http://www.ebay.com/itm/181357834990?ss ... 1497.l2649
...
/quote]
FWIW, the backpack is one I've recently used**, and written some 'C' demo software**** for (using wiringPi etc.) :
http://www.cpmspectrepi.webspace.virgin ... D_Backpack
http://www.cpmspectrepi.webspace.virgin ... tware.html
If the software is of interest, PM me with contact details & I can let you have a "tarball".
Trev.
** Also equivalent to this one:
http://www.cpmspectrepi.webspace.virgin ... D_Backpack
**** lcd_fi2c_demo.c - originally written for the "funduino" version of the backpack (linked to above).
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12983
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Help with 16x2 LCD

Mon May 05, 2014 9:00 pm

the problem with many 5V LCD's is that they are not completely 3V3 I/O compatible, most often their minimal "logic high" voltages are 70% of VCC,, and at 5v that is 3.5V, which is higher than 3V3 I/O levels. So you need levels shifters, or lower VCC to get the I/O to work reliably. without this you will get no characters, or scrambled ones.

With I2C I/O expanders the problem can also occur, but you could try adding 10K pullups to 5V, (so the I/O expanders see 5V logic highs) as the PI's I2C GPIO's are somewhat more tolerant to 5V (via a 10K pull-up) than the other GPIO's.

Alternatively lower the 5V of the LCD or I/O expander to 4.5V using a diode.

lxh
Posts: 47
Joined: Fri Feb 07, 2014 10:32 am

Re: Help with 16x2 LCD

Tue May 06, 2014 5:46 am

Besides the voltage level problems mentioned, there is some logic involved. I'll share some of my findings.

Most of the LCD character modules are compatible (both pin-layout and command set). They can be driven in 4-bit and 8-bit mode. This is the common pin layout:

Code: Select all

Pin No. Pin Name    I/O     Descriptions
1       VSS         Power   Power supply, Ground (0V)
2       VDD         Power   Positive power supply
3       V0          Power   LCD contrast reference supply
4       RS                  Input Register Select
                            RS=HIGH: transferring display data
                            RS=LOW: transferring instruction data
5       R/W         Input   Read / Write Control bus:
                            R/W=HIGH: Read mode selected
                            R/W=LOW: Write mode selected
6       E           Input   Data Enable
7       DB0
:       :
14      DB7         I/O     Bi-directional tri-state Data bus
                            In 8 bit mode, DB0 ~ DB7 are in use
                            In 4 bit mode, DB4 ~ DB7 are in use, DB0~DB3 leave open
15      BLA         Power   Backlight positive supply
16      BLK         Power   Backlight negative supply 
You'll need to trace the lines on the PCB to the pins of the I2C I/O expander, and consult the datasheet to discover which pins drives what lines.

Adafruit's LCD module uses the LCD module in 4-bit mode (that is default when they power up), and as a bonus can drive 3 different backlight leds (for the RGB-version)

So based on a software for Adafruit's module, you can modify it. Adafruit uses the MCP23017, which is a 16 bit IO expander (IO ports A and B). The connections there are like this:

Code: Select all

AdaFruit's setup of the connection between MCP23017 and the LCD panel:
B.7 = RS
B.6 = R/W
B.5 = E
B.4 = D4
B.3 = D5
B.2 = D6
B.1 = D7
B.0 = Blue LED -> output
A.7 = Green LED -> output
A.6 = Red LED -> output
A.5 = button ->input
A.4 = left button ->input
A.3 = up button ->input
A.2 = down button ->input
A.1 = right button ->input
A.0 = select button ->input
As you can see, the entire register A is optional for simple LCD boards (no buttons and a single back light led)

Overview of what happens in the code: Sending data is broken down to "nibbles" (half bytes). Because of the timing requirements of the module, the data needs to be present a little while before the "strobe" is pulsed. So if you want to send 1 byte to the module, what needs to be done is
  • present the databits + data line if it is data or command
  • pulse write strobe ("E")
So sending 1 byte results in 4 port changes on the I/O expander:
status bits + nibble 1 + E=0
status bits + nibble 1 + E=1
status bits + nibble 2 + E=0
status bits + nibble 2 + E=1

Status bits include R/W and RS (character or command data), but perhaps also backlight on / off. You'll have to wait until the previous command or data is processed. I2C is usually slow enough, but some commands (e.g. clear display) take longer, so you'll need to wait until the module is ready.

As the data pins of the LCD module may not be connected to your I/O expander with the same "bit numbers", so you may also need to shuffle the bits in your nibble before sending them (a 16 byte lookup table will to fine).

When you've got the basic communication settled, then you'll need to
  • Initialise and configure your I/O expander
  • drive your LCD module logically. This is not trivial, and requires understanding of the command set. If you use an existing working library, that work is already done for you.
But when you've got everything in place, even those simple LCD modules are wonderful in many projects

lxh
Posts: 47
Joined: Fri Feb 07, 2014 10:32 am

Re: Help with 16x2 LCD

Wed May 07, 2014 2:31 pm

I quickly scanned the datasheet for your I2C port expander, and it appears simple: any data you send to it will appear on the I/O pins. No setup needed, so in comparison to the MCP23017 it is easy.

So the I2C layer is fairly simple, send I2C data to a certain address (using e.g. wiringPi). You'll need to figure out the LCD panel commands and data addressing.

Maybe this will help too:

Code: Select all

Display Commands
                                     Code
No. Instructions     RS  R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0  Function
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 1 | Clear          |   |   |   |   |   |   |   |   |   |   | Write “20h” to DDRAM and
   | Display        | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | set DDRAM address (AC) to “00h”
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 2 | Return         |   |   |   |   |   |   |   |   |   |   | Set DDRAM address (AC) to “00h” and
   | Home           | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | x | return cursor to its original position if shifted
   |                |   |   |   |   |   |   |   |   |   |   | (DDRAM contents are not change)
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 3 | Entry Mode     |   |   |   |   |   |   |   |   |   |   | Set cursor moving direction and specify display
   | Set            | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |I/D| S | shift, during data read and write of DDRAM and
   |                |   |   |   |   |   |   |   |   |   |   | CGRAM.
   |                |   |   |   |   |   |   |   |   |   |   | S=1, screen shifting; 
   |                |   |   |   |   |   |   |   |   |   |   | S=0, no screen shifting
   |                |   |   |   |   |   |   |   |   |   |   | I/D=1, AC=AC+1 and if S=1, screen shift left
   |                |   |   |   |   |   |   |   |   |   |   | I/D=0, AC=AC-1 and if S=0, screen shift right
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 4 | Display        |   |   |   |   |   |   |   |   |   |   | D=1, display on; D=0, display off
   | ON/OFF         | 0 | 0 | 0 | 0 | 0 | 0 | 1 | D | C | B | C=1, cursor on; C=0, cursor off
   |                |   |   |   |   |   |   |   |   |   |   | B=1, cursor blinking on; B=0, cursor blinking off
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 5 | Cursor or      |   |   |   |   |   |   |   |   |   |   | Move the cursor or shift the display, where
   | Display Shift  | 0 | 0 | 0 | 0 | 0 | 1 |S/C|R/L| x | x | DDRAM contents.
   |                |   |   |   |   |   |   |   |   |   |   | S/C=1, shift screen; S/C=0, shift cursor
   |                |   |   |   |   |   |   |   |   |   |   | R/L=1, to right-side; R/L=0, to left side
   |                |   |   |   |   |   |   |   |   |   |   | (if S/C=1, AC will not be changed)
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 6 | Function       |   |   |   |   |   |   |   |   |   |   | DL=1, 8-bit interface; DL=0, 4-bit interface
   | Set            | 0 | 0 | 0 | 0 | 1 | DL| N | F | x | x | N=1, multi-line display; N=0, 1-line display
   |                |   |   |   |   |   |   |   |   |   |   | F=1, 5x11 dots font; F=0, 5x8 dots font
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 7 | Set CGRAM      |   |   |   |   |   |   |   |   |   |   | Set CGRAM address in address counter
   | address        | 0 | 0 | 0 | 1 |AC5|AC4|AC3|AC2|AC1|AC0|  
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 8 | Set DDRAM      |   |   |   |   |   |   |   |   |   |   | Set DDRAM address in address counter
   | address        | 0 | 0 | 1 |AC6|AC5|AC4|AC3|AC2|AC1|AC0|
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
 9 | Read Busy flag |   |   |   |   |   |   |   |   |   |   | Check the system status and get the address
   | & address      | 0 | 1 |BF |AC6|AC5|AC4|AC3|AC2|AC1|AC0| counter content (AC6~AC0).
   |                |   |   |   |   |   |   |   |   |   |   | BF=1, busy; BF=0, ready
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
10 | Write data     |   |   |   |   |   |   |   |   |   |   | Write the data into internal RAM, where the
   | to RAM         | 1 | 0 |D7 | D6| D5| D4| D3| D2| D1| D0| address counter pointing at.
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
11 | Read data      |   |   |   |   |   |   |   |   |   |   | Read the data from internal RAM, where the
   | from RAM       | 1 | 1 |D7 | D6| D5| D4| D3| D2| D1| D0| address counter pointing at.
---+----------------+---+---+---+---+---+---+---+---+---+---+---------------------------------------------------
Note:
*1. Do not use any other command not listed, or the system malfunction may result.
*2. For the details of the Display Commands, please refer to S6A2067 datasheet. 
Clear Display and Return home are slow commands, you'll need to poll the "Busy" line (e.g. read I2C bytes repeatedly and check a bit) from the LCD device and not send anything until that clears.

Cheers!

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Wed May 07, 2014 2:55 pm

lxh wrote:I quickly scanned the datasheet for your I2C port expander, and it appears simple: any data you send to it will appear on the I/O pins. No setup needed
Ah, thanks for that. I was looking at the code I pointed to earlier, and couldn't see anything relating to setting up the PCF8574. That explains it.
lxh wrote:Clear Display and Return home are slow commands, you'll need to poll the "Busy" line (e.g. read I2C bytes repeatedly and check a bit) from the LCD device and not send anything until that clears.
Alot of code out there simply sleeps for a bit after these commands, if I recall 50 or 75ms should do it, but polling 'busy' is better. There's code in the Adafruit library that does that.

patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Re: Help with 16x2 LCD

Thu May 08, 2014 1:17 am

Hello,

Thank you for your replies. I am currently reading through pin layouts etc... I'm very new to GPIO so this will take a while for me to fully understand. I am emailing back and forth with someone about getting this thing to work.

If anyone has any sample C code that will print out a simple "hello world" to this thing, that would be of great help!

Once again, thank you all so much.

-Pat

lxh
Posts: 47
Joined: Fri Feb 07, 2014 10:32 am

Re: Help with 16x2 LCD

Thu May 08, 2014 7:04 am

if all you need to do is put something on the LCD, you could even connect the thing directly to your GPIO pins and remove the I2C I/O expander alltogether.
From your code, you'll need to set the GPIO lines (R/W, RS and 4 data bits D4-D7), and then toggle the GPIO connected to E as a "write pulse". Then change the I/O direction of D7 (this is the ready signal), and poll for that to clear (or just wait and let the Pi do something else in the mean time). This is just an idea (and should work) - I have not worked that out myself (needed more IO from the Pi itself)

citamretni
Posts: 1
Joined: Thu May 08, 2014 6:50 pm

Re: Help with 16x2 LCD

Thu May 08, 2014 6:54 pm

does the LCD display anything at all?

I know by using these (44870 controller) LCD displays with Arduinos, that they address the individual characters in an odd way.

lxh
Posts: 47
Joined: Fri Feb 07, 2014 10:32 am

Re: Help with 16x2 LCD

Fri May 09, 2014 4:45 am

the character addressing is indeed a little odd, but makes sense if you think about the flexibility of the controller: it can drive e.g. 8x1, 12x1, 16x2, 20x2, 20x4 etc. LCD character displays.

https://www.sparkfun.com/datasheets/LCD/HD44780.pdf explains it (at driver level) pretty well. Even if you're no electrical engineer :)

patthehuman
Posts: 11
Joined: Mon Apr 14, 2014 9:25 pm

Re: Help with 16x2 LCD

Wed May 14, 2014 6:41 pm

Guys,

Thank you so much for the replies. Unfortunately, this is all way above my head.

User avatar
Douglas6
Posts: 4853
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Help with 16x2 LCD

Wed May 14, 2014 7:28 pm


Return to “C/C++”