javaraspberry-piraspberry-pi4pi4j

Raspberry Pi4 with Pi4j Java


I am trying to run a simple test on my Raspberry Pi4 using the pi4j java library v1.2. Somehow it's not doing anything. I also don't see any errors on console. It just finishes after 2 seconds. I'm expecting the LED light to turn on and turn off after 2 seconds.

I also updated my gpio binary to v2.52 as per this https://stackoverflow.com/a/63433316/607637 but it's still the same. Also, the command gpio write 19 1 is not doing anything.

GPIO

pi@gtpi:~ $ gpio -v
gpio version: 2.52
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 4B, Revision: 02, Memory: 4096MB, Maker: Sony 
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.
pi@gtpi:~ $ gpio write 19 1  ///---> this also doesn't do anything


Java App: uses com.pi4j:pi4j-core:1.2

import com.pi4j.io.gpio.*;
public class Pi4 {
  public static void main(String[] a) throws Exception {
    GpioPinDigitalOutput op = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_19);

    op.high();
    Thread.sleep(2000);
    op.low();
 }
}

However, a python script that I wrote to do the same is working:

from gpiozero import LED
from time import sleep

red = LED(19)
red.on()
sleep(2)
red.off()

Question: what else do I need to do to make the Java app working(turning on the LED)? Is it not compatible with Pi4 at all?


Solution

  • It appears its answered here:

    It looks Pi4j is using a different pin numbering scheme. "Under the hood", WiringPi is used to control the GPIOs.

    So, I needed to tell it to use Broadcom pin numbering scheme using:

    GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

    Now its working:

    import com.pi4j.io.gpio.*;
    public class Pi4 {
      public static void main(String[] a) throws Exception {
        GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));
    
        GpioPinDigitalOutput op = GpioFactory.getInstance().provisionDigitalOutputPin(RaspiPin.GPIO_19);
    
        op.high();
        Thread.sleep(2000);
        op.low();
     }
    }