1

I am basically trying to turn on a led by using pi4j library. My code is pretty simple:

public static void main(String args[]) throws InterruptedException {
    GpioController gpioController = GpioFactory.getInstance();
    GpioPinDigitalOutput pinOut = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_17);
    pinOut.high();
    Thread.sleep(5000);
    pinOut.low();
}

I am exporting the jar file from my computer, copying to my Raspberry Pi 4 than running in there.

To check if my led is not broken or GPIO is set, I've executed following Python scripts and I see my led works perfectly:

python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17,True)

What am I missing?

5
  • 1
    Do you get any errors? My concern is that Pi 4 is not supported going by the list at pi4j.com/1.2 Also check that the pin numbering pi4j.com/1.2/pin-numbering-scheme.html matches the BCM numbering.
    – user115418
    Oct 11, 2020 at 16:32
  • 1
    @Andyroo I don't get any errors, I can even see the console writes on my pi. I also couldn't find anything regarding to pi4 on pi4j documentation but I didn't worry a lot since it didn't give me any errors...
    – osumatu
    Oct 11, 2020 at 16:37
  • 1
    As @andyroo has said. 1) the Pi4 is not supported. 2) you are using the wrong GPIO numbering scheme.
    – joan
    Oct 11, 2020 at 16:51
  • 1
    The Pi4 has GPIO located at different addresses and pull-ups are different, so the library will need to be modified.
    – Milliways
    Oct 11, 2020 at 21:45
  • 1
    Or you can try BlueJ, or what Pi Mag suggests. More details in the following Q&A (Appendix B of my answer) : raspberrypi.stackexchange.com/questions/111679/…. Cheers.
    – tlfong01
    Oct 12, 2020 at 0:26

2 Answers 2

1

Indeed as mentioned in the comments, Pi4J is using a different pin numbering scheme. "Under the hood", WiringPi is used to control the GPIOs. This library uses a different numbering scheme. This means BCM pin 17 you are using in Python is pin 0 in the WiringPi scheme.

See below a full table with BCM, WiringPi, and physical pin numbering (see link 2 below).

Raspberry Pi numbering scheme with BCM and WiringPi numbering

Next to that, the internal wiring of the Raspberry Pi 4 is different compared to the previous ones, so you'll need to upgrade your WiringPi to the latest version 2.52 with:

$ gpio -v
gpio version: 2.50

$ cd /tmp
$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
$ sudo dpkg -i wiringpi-latest.deb
$ gpio -v
gpio version: 2.52

As a side note: we are working on a new version of Pi4J which will make things easiers, but work-in-progress... ;-)

More info in my blog posts on:

  1. Using Pi4j (V1) on the Raspberry Pi 4
  2. Raspberry Pi history, versions, pins and headers as a Java Maven library
  3. Oracle Java Magazine: Getting started with JavaFX on Raspberry Pi
3
  • I see there is a difference in pin numbering but when I change my code for GPIO_00 (Which is the WiringPI pin for GPIO_17) I get runtime error saying: Unable to open GPIO direction interface for pin[0]: No such file or directory.
    – osumatu
    Oct 15, 2020 at 10:18
  • Just an idea, can you try another pin? E.g. pin 40 = BCM 21 = WiringPi 29 as used in the example at blogs.oracle.com/javamagazine/…
    – Frank
    Oct 15, 2020 at 17:40
  • Yes indeed, for some reason some pins are working and some are not. I am basically just brute-forcing to see which ones I can use. Thanks for the reply.
    – osumatu
    Nov 1, 2020 at 11:29
0

Since I don't have enough reputation to comment...

In the PI4J library, you use constants like "GPIO_16"... However, these are not the GPIO pins that you find in the RPI documentation. They are the WiringPI pins.

A mapping is here: https://pinout.xyz/pinout/wiringpi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.