Hardware Setup Support: Raspberry Pi via GPIO
The Raspberry Pi is a family of inexpensive credit card size single board computers that have many applications in embedded electronics. This page describes the use of the Raspberry Pi's GPIO pins under JMRI. Since the Raspberry Pi runs Linux natively, other connections are also possible using either a USB connection or via the Raspberry Pi's built in serial ports.
Underlying support for the Raspberry Pi's GPIO connections is provided using the Pi4J library for Java.
Version 1.0 of the Pi4J Library, used with JMRI version
4.5.4 and earlier, require that applications using GPIO pins
be run as root (or using
Starting with version 4.5.5, JMRI uses version 1.1 of the Pi4J Library. Version 1.1 of the Pi4J library, when used in conjunction with Raspbian Jessie (and later releases) allow the user to access the GPIO pins without running as root. For compatability reasons, and to allow possible use of PWM outputs, JMRI has choosen not to enable this mode by default.
In order to enable use of the GPIO pins by a non-root
user, the user must set the
environment variable as follows:
This environment varialbe must be set prior to executing JMRI.
For more information, please see the relavent portions of the pi4j web page.
Pi4j 1.1 and Raspbian Stretch
It has been brought to our attention that the linux kernel used in Raspbian Stretch and Pi4j version 1.1 have a slight incompatability. This incompatability is caused by Pi4J's interface with the hardware is doesn't recognize the CPU reported by the kernel. This section describes how to determine if you are experiencing this issue and how to correct the problem on your Raspberry Pi.
Determining if the incompatability is a problem
If the incompatability is an issue, JMRI will crash when attempting to create a Raspberry Pi connection and the following error message will appear in the console log:
Unable to determine hardware version. I see: Hardware : BCM2835
- expecting BCM2708 or BCM2709.
If this is a genuine Raspberry Pi then please report this
to firstname.lastname@example.org. If this is not a Raspberry Pi then you
are on your own as wiringPi is designed to support the
Raspberry Pi ONLY.
How to correct for this issue
To correct for this issue, there are two steps you need to complete. First, you need to install a recent version of the wiringPi library, whic Pi4J uses to talk to the Raspberry Pi's hardware. You can do this by running the following command in a terminal:
sudo apt-get install wiringpi
Second, we need to configure Pi4j to use the library you just installed instead of the one that is included with Pi4j. To tell JMRI to use Pi4j in this mode, we are going need to run the following command in a terminal as the user who starts the JMRI application(s):
echo "default_options=\"-Dpi4j.linking=dynamic\"" > ~/.jmri/jmri.conf
After completing these steps, JMRI will be able to configure and use Raspberry Pi hardware normally.
JMRI currently supports inputs (Sensors) and outputs (Turnouts) via the Raspberry Pi's GPIO pins.
Input/Output pin addressing
JMRI currently uses the default Pi4J pin numbering. The Pin numbers may differ on each model Pi. Generally speaking, Broadcom GPIO pins 0 to 7 may be referenced by those pin numbers in JMRI. This is not guaranteed to be the case. Higher pin numbers are available, but may not correspond to the Broadcom pin numberings. Please refer to the Pi4J website for model specific pin numbering information.
- Make sure you have the latest version of JMRI installed, then go to the Preferences panel of a JMRI-based program. This opens automatically the first time a program is run, or you can select it from the "Edit" menu
- Select "Raspberry Pi Foundation" from the System Manufacturer selection box.
- Select "Raspberry Pi GPIO" from the System Connection selection box.
- Click "Save". You'll be asked if it's OK for the program to quit, click "Yes".
- Restart the program. You should be up and running.
Third Party info
The Pi4J website provides model specific Java I/O information.