Using Pi4j (V1) on the Raspberry Pi 4

A topic which comes up from time to time in questions related to Java and the Raspberry Pi, is the support of the 4th version of this board in combination with Pi4J, “the friendly object-oriented I/O API and implementation library for Java programmers to access the full I/O capabilities of the Raspberry Pi “.

Under the hoods, Pi4J is using WiringPi to control the GPIOs. Because the internal wiring of the processor on the Raspberry Pi 4 is different from the previous boards, an update is available of WiringPi.

Be sure to use version 2.52 of the gpio utility. You can check your version by using the gpio -v command via the terminal and, if needed, install the new version using the following commands:

$ 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

UPDATE 20210112

Even with version of 2.52 there are still pull up/down errors with some of the GPIOs on the Raspberry Pi 4 as you can read in this StackOverflow question: “Setting Pull Up/Pull Down mode doesn’t work (mostly)”.

An unofficial version of WiringPi can be found on GitHub in WiringPi > WiringPi.

Robert Savage created a solution which will be part of V1.3 of Pi4J. After you update to Pi4J 1.3 you can run the following command to perform this use the latest version of this “unofficial WiringPi”:

$ sudo pi4j --wiringpi

This command will remove the existing WiringPi installation, clone the GitHub project and build it like this:

echo "--------------------------------------------"
echo "Uninstalling any existing WiringPi versions "
echo "--------------------------------------------"
sudo apt-get remove wiringpi -y
sudo apt-get --force-yes --yes install git-core gcc make
echo "--------------------------------------------"
echo "Cloning latest WiringPi sources from repo   "
echo "--------------------------------------------"
cd /opt/pi4j
git clone https://github.com/WiringPi/WiringPi --branch master --single-branch wiringpi
cd /opt/pi4j/wiringpi
echo "--------------------------------------------"
echo "Building and installing WiringPi library"
echo "--------------------------------------------"
sudo ./build uninstall
sudo ./build clean
sudo ./build