dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Java and I2C Display

Thu Apr 07, 2016 12:39 pm

Hello!

I recently bought a LCD display (HD44780 - 1602 - 16x2) and a I2C module that should allow me to save some precious pins on the Pi, it runs with a PCF8574T chip, this one looks like this :

Image

I found a lot of Python scripts that worked almost perfectly (http://www.raspberrypi-spy.co.uk/2015/0 ... pberry-pi/ for example), but not any Java one. At least not for this chip :(

Does anyone know how to achieve it? or have some tips? I would really appreciate it.
Thanks in advance!

dbrazzor

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Fri Apr 08, 2016 10:58 am

Depends which Java library you were to use. You could of course use the diozero library which adds an abstractoin layer across the various options (Pi4j, pigpio, wiringPi, JDK Device I/O) :-).
I've just pushed up a snapshot build (0.7-SNAPSHOT) with a port of the Python code to Java as an example. I don't have one of those devices hence haven't been able to test this.

Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Fri Apr 08, 2016 7:59 pm

Thanks Matt!

I've never used the diozero library before, I was happy with PI4J :)
I installed it but I got the following error :
"Error: no device provider factory service found, please configure META-INF/services/com.diozero.internal.spi.NativeDeviceFactoryInterface or set -Dcom.diozero.devicefactory".

Also, in the I2CLcd class you've made, the method

Code: Select all

writeByte(byte)
does not exist, I replaced it with

Code: Select all

writeByte(DEVICE_ADDRESS, byte)
I don't know if I was expected to do it this way, I am completely new with I2C, sorry about that :?

I hope I can get some help, thanks again!

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Fri Apr 08, 2016 8:46 pm

Apologies for the confusion - I pushed a new snapshot build to sonatype but not google drive. I've just uploaded 0.7-SNAPSHOT Zip file to google drive. If you extract that zip file you should be able to run the following:

Code: Select all

sudo java -cp tinylog-1.0.3.jar:diozero-core-0.7-SNAPSHOT.jar:diozero-provider-pi4j-0.7-SNAPSHOT.jar:pi4j-core-1.1-SNAPSHOT.jar com.diozero.sandpit.I2CLcdSampleApp
Let me know if this works, I'm keen to get feedback on diozero, the aim of it is to make life much easier to develop Java apps on the Pi.

If you're familiar with Pi4j it also has support via its com.pi4j.component.lcd.impl.I2CLcdDisplay class in the pi4j-device JAR.

Regards,
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Fri Apr 08, 2016 9:22 pm

Thanks again, but I can't find the link to your Drive :? (Or am I just exhausted?)

I already saw the PI4J I2CLcdDisplay class but I can't figure out some values, like rs, rw, d4 ➔ d7.

Really appreciate the help :)

dbrazzor

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Fri Apr 08, 2016 9:37 pm

Does this link work? https://drive.google.com/folderview?id= ... kd4OXhYd3c.
There's also a link from the docs at http://rtd.diozero.com/.
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Sat Apr 09, 2016 3:33 pm

I tried the command but got the following error : http://pastebin.com/fpZvjRmu

The log file : http://pastebin.com/czjTyqWM

Thanks for the help!

dbrazzor

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Sat Apr 09, 2016 3:50 pm

Strange - that's an error loading the Pi4j shared library. What Pi are you running this on? I've just tried running this on a Pi 2 and a Pi Zero and got the same error as you on the Pi Zero but only when using the Pi4j provider.
Can you try with the pigpio provider please using this command:

Code: Select all

sudo java -cp tinylog-1.0.3.jar:diozero-core-0.7-SNAPSHOT.jar:diozero-provider-pigpio-0.7-SNAPSHOT.jar:pigpioj-java-1.0.0.jar com.diozero.sandpit.I2CLcdSampleApp
I've noticed that this occasionally fails (with a signal 11) on the Pi Zero but then works if I try again, I need to debug to get to the bottom of this.

Regards,
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Sat Apr 09, 2016 9:34 pm

I am running it with a Pi model 1 - B+.

I tried the command, it gives me some infos and turn of the display's backlight off but nothing else :? (http://pastebin.com/ZJDjtJ74).

Regards,
dbrazzor

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Sat Apr 09, 2016 10:12 pm

Getting closer :-). Does the Python code linked to at the start work? I tried to replicate it exactly in Java...
I might look in to buying one of these, in the meantime you could try pull the diozero code from GitHub and try get it working - I can commit any changes you make.
Happy to answer any questions regarding setting up a dev environment.
Actually I did fix a stupid bug in the lcd driver class (when calling the string pad method!) that meant it would always display spaces, I may not have updated the Google drive zip file afterwards. Will update tomorrow.

Matt
http://www.diozero.com/

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Sun Apr 10, 2016 9:09 am

As promised uploaded a new distribution ZIP file to Google drive.

Hope this works, I've ordered one of these so will be able to test and improve when it arrives.

Regards,
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Mon Apr 11, 2016 1:50 pm

Hello, thanks a lot but I don't see any new distribution on your Drive :c
Have you received the display? Hope you'll enjoy it :)

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Mon Apr 11, 2016 1:57 pm

I updated the existing 0.7-SNAPSHOT ZIP file.

Display arrives tomorrow.

Regards,
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Mon Apr 11, 2016 1:59 pm

Haw okay, I tried this distribution again but got the same results :?

dbrazzor

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Mon Apr 18, 2016 11:26 am

Apologies for the delay, busy with work and a birthday weekend :-)
Just had a quick test and it is working with my setup (lost a lot of time thinking it wasn't working until I tweaked the display contrast!). I've pushed a new snapshot build to Google drive plus commited modified code to GitHub. Still needs tidying up plus can be made a bit more generic to work with other setups (not all of these I2C connected LCDs are wired the same apparently).

Let me know if this works for you, fingers crossed.

Regards,
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Mon Apr 18, 2016 6:00 pm

Hello! No worries, Happy birthday! :D

I have to say, you made my day! Your code works with my setup, I've been searching for something like this for weeks!

Here is a video of the code running on my display :
https://drive.google.com/open?id=0BxgwY ... kdnSFUzekE

Can you explain me how should I implement this in my project?

Thanks a lot for what you've done :)

(I thought I had received a bad unit because of the contrast too ^^')

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Mon Apr 18, 2016 7:18 pm

When I use the following code (found in I2CLcdSampleApp) :

Code: Select all

I2CLcd lcd = new I2CLcd(16, 2);
        lcd.setText(0, "Welcome!");
I got the following error :
http://pastebin.com/yTx6zm9y :?

(Sorry for the multiple posts and the inconvenience)

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Mon Apr 18, 2016 9:10 pm

Excellent news, glad you've got it working. I'm using the 1.1-SNAPSHOT version of Pi4j, I'm not at my computer now but it looks like you may be using an older version of Pi4j that doesn't have the method that I'm using. For info I do most testing using my pigpio provider.
Nice looking project by the way!
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Wed Apr 20, 2016 4:47 pm

Hello Matt, thanks for your help!

I just tried with PI4J 1.1 (Sorry, I was busy with school projects :?), It works pretty well, but sometimes I got some weirdness going on :

Image
Image

Also, the text disapear for no reason (at 24s) :

https://www.youtube.com/watch?v=WUMHevmHsFQ

Oh and the audio is affected by it, at random moments the sounds just stop. Even when it's played outside the program (??).

Regards,
dbrazzor

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Wed Apr 20, 2016 6:57 pm

Is your code on GitHub? Happy to take a quick look...

Regards,
Matt
http://www.diozero.com/

dbrazzor
Posts: 15
Joined: Thu Apr 07, 2016 12:20 pm

Re: Java and I2C Display

Fri Apr 22, 2016 1:58 pm

Hello!

I am currently recoding it, I will put it on Github :)

Regards,
dbrazzor

kendler
Posts: 3
Joined: Tue Sep 02, 2014 12:39 pm

Re: Java and I2C Display

Sat Jun 11, 2016 9:24 pm

can you please share what values should be in d4->7 bits?

Nevermind, found it
new I2CLcdDisplay(4,20,1,0x27,3,0,1,2,7,6,5,4); is the way.

Pawel_G
Posts: 1
Joined: Wed Dec 28, 2016 4:59 pm

Re: Java and I2C Display

Thu Dec 29, 2016 3:36 pm

Hello,
First of all Matt, I just want to say thank you for shering your library, it looks really promissing, great job!

Guys, I'm new with raspberry pi and I2C stuff, and I've got a problem with I2C addressing, it looks that my I2C converter actually got 3f address, but I2CLcd class constructor takes only int as address parameter, do you know what should I type there to run I2CLcdSampleApp20x4?

User avatar
DougieLawson
Posts: 30431
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Java and I2C Display

Thu Dec 29, 2016 5:32 pm

Pawel_G wrote:it looks that my I2C converter actually got 3f [sic] address,
0x3f hex == 63 decimal substitute that for 0x27 hex (or 39 decimal) wherever you see it in the sample code you're working with.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

mattlewis
Posts: 107
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java and I2C Display

Mon Jan 02, 2017 1:00 pm

The latest version of the I2CLcdSampleApp20x4 allows you to specify the I2C address as well as the I2C bus number. The I2C address parameter is parsed using Integer.decode so can accept decimal, hexadecimal, and octal numbers.

Matt
http://www.diozero.com/

Return to “Java”

Who is online

Users browsing this forum: No registered users and 4 guests