Pi4J v2 and I2C

Skip to first unread message

Thomas Manz

Aug 26, 2020, 2:50:36 PM8/26/20
to Pi4J

today I wanted to do some first tests with version 2, so I downloaded the jars from https://oss.sonatype.org/content/repositories/snapshots/com/pi4j/ .
As test object I´m using the brightness sensor BH1750, it´s already connected with my Raspberry Pi 3 and works with using i2cset/i2cget. This is also what I´m currently using in my Java project (Runtime.getRuntime().exec("i2cget ...")) because I´m on Java 14 and Pi4J v1.x does not work here with I2C.

Unfortunately it does not work with Pi4J v2, I only receive 0´s on every call (debugging showed me that I end up in this file for every call: RpiI2C.java)

Am I missing something in the configuration (because the code in PiGpioI2C.java would look fine on a rough look...) or is it just not implemented yet?

That´s my code so far:
Context pi4j;
I2CConfig pi4j_i2c_BH1750_config;
I2C i2c_BH1750;

pi4j = Pi4J.newAutoContext();
pi4j_i2c_BH1750_config = I2C.newConfigBuilder(pi4j)
.name("My I2C Bus")

i2c_BH1750 = pi4j.i2c().create(pi4j_i2c_BH1750_config);
i2c_BH1750.write(0x01);  //PowerOn
i2c_BH1750.write(0x10);  //Set Continuous Mode

Recurrent event:
byte[] value = new byte[2];
i2c_retval = i2c_BH1750.read(value, 0, 2);

Thanks and best regards,

Thomas Manz

Aug 26, 2020, 6:12:38 PM8/26/20
to Pi4J
Ok, meanwhile I´ve solved it, in case anyone faces the same problem, the correct code should look like this:

pi4j_i2c_BH1750_config = I2C.newConfigBuilder(pi4j)
.name("My I2C Bus")
.provider("pigpio-i2c")     // define what provider to choose
i2c_BH1750 = pi4j.create(pi4j_i2c_BH1750_config);     // and not pi4j.i2c().create(...)

Good night!


Aug 27, 2020, 6:52:02 AM8/27/20
to Pi4J
Hello Thomas, thanks for sharing your problem and solution! We are still working on the V2 and examples so sorry if it was not clear enough from the start...


Thomas Manz

Aug 28, 2020, 7:56:02 AM8/28/20
to frank.d...@gmail.com, Pi4J
Hello Frank,

thanks for your response and letting me know about the examples (will for sure be useful when I'm going to deal with my actual sensor of interest)!
I realized my main failure was that I copied code not from the examples but from the test modules:

I think the reason is (and maybe this is something you could improve) that on the web page https://v2.pi4j.com/ there is no link to the examples repository, it's not even mentioned that it exists and furthermore the only link there points to the source code which is one level below and I still could not see the examples. 
I'm not very experienced with github, so I don't know if it is obvious for anyone else where to find that, for me it was not. 

For now I'm happy that I2C is already working in this early phase and I'm planning to continue using it. Of course I'll let you know when I face further problems. 


You received this message because you are subscribed to the Google Groups "Pi4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pi4j+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi4j/e5acecbb-5cd2-4580-a39f-4556f4b7330fn%40googlegroups.com.

Robert Savage

Aug 28, 2020, 10:24:28 AM8/28/20
to Pi4J
As far as the explicit assignment to the I2C provider .... a similar issue was reported here:

I suspect it is a bug and the default I2C provider is not getting assigned during the framework startup.  
Definitely something we need to look deeper into in V2 codebase.  I'm not available to dig into it until late Sept, early Oct -- but hopefully its something simple to fix. 

Thanks, Robert
Reply all
Reply to author
0 new messages