Pi4J is an open source Java library that provides an abstraction layer for interacting with Raspberry Pi's GPIO pins and other hardware interfaces like I2C and SPI. It aims to simplify GPIO programming for Java developers by presenting an object-oriented API. Pi4J has evolved over several major versions, with version 2.0 introducing a plugin architecture and migrating to a more standard Broadcom pin numbering scheme. The library provides classes for configuring pins as inputs or outputs, reading from and writing to pins, attaching listeners to respond to pin state changes, and interacting with common hardware components.
4. 4
Confidential
What is Raspberry Pi
• Single Board Computer
• Linux Operating system -
Raspbian
• ARM Architecture
• Low Cost
• Low Power
• Has the ability to interact with
the outside world GPIO (used
in a wide array of digital maker
projects, from music machines
and parent detectors to
weather stations and tweeting
birdhouses with infra-red
cameras, etc.)
6. 6
Confidential
Finding Compatible Hardware
• Zillions of devices available
• Search Amazon, Adafruit for
Raspberry Pi devices
• 3.3V or 5V, I2C, GPIO, Serial
• “Hats” can be used to expand
capability by stacking boards
7. 7
Confidential
Ground Pin, Power Pin, GPIO what is it?
The Raspberry Pi 4 board has a GPIO header with 40 pins (amount could be different from
model to model).
Ground Pins
The ground is very useful for making a common reference between all components in your
circuit. Always remember to connect all components to the ground.
Power Pins
Those pins can be used to power components such as sensors or small actuators.
GPIO
GPIO means General Purpose Input/Output. Basically that’s one pin you can use to write data to
external components (output), or read data from external components (input).
10. 10
Confidential
What is Pi4J?
• Open Source Project
• Raspberry Pi Platform (Linux/ARM)
• Abstraction over Low Level I/O
• Supports I/O Programming
• Object-oriented API
• Event Based
• Java / C (JNI + Native)
• Provides Listeners , Triggers & Component API
• Provides Java Classes supporting I/O Operations
11. 11
Confidential
Brief history of Pi4J
The Pi4J Project was started in 2012, the same year the Raspberry Pi was introduced as a tool to
provide Java developers a simple and familiar object-oriented interface library to access the low-level
I/O capabilities of the Raspberry Pi including GPIO, I2C, SPI, PWM and Serial communications.
12. 12
Confidential
Version 1.4 of Pi4J
The original library which started in 2012 and got a last release in 2021. Up till version 1.3 the
library targets Java 8, while version 1.4 relies on Java 11.
Main features:
• Export & unexport GPIO pins
• Configure GPIO pin direction
• Configure GPIO pin edge detection
• Control/write GPIO pin states
• Pulse GPIO pin state
• Read GPIO pin states
• Listen for GPIO pin state changes (interrupt-based;
not polling)
• Automatically set GPIO states on program
termination (GPIO shutdown)
• Triggers for automation based on pin state changes
• Send & receive data via RS232 serial communication
• I2C Communication
• SPI Communication
• Extensible GPIO Provider interface to add GPIO
capacity via expansion boards
• Access system information and network information
from the Raspberry Pi
• Wrapper classes for direct access to WiringPi Library
from Java
13. 13
Confidential
Version 2 of Pi4J
As of Version 2.0, Pi4J will no longer include support for peripheral devices and add-on
chipsets/boards as part of the core project. A new plugin model has been introduced in version
2.0 that should help enable third-party development and support third-party add-ons which may
be developed and maintained independently of the core Pi4J project.
Main features:
• Fluent APIs/Interfaces
• Immutable Runtime Context
• Extensible Provider/Platform/Plug-in
Architecture
• Builder-patterns for creating new I/O
instances
• Dependency Injection via Pi4J Annotations
• Well-documented source code
• Hardware PWM Support
• Remote I/O Support (via TCP/IP)
• Java 11
• In addition to the features listed above, Pi4J
version 2.0 also abandons the old WiringPi
pin numbering scheme in favor of the more
traditional and commonly used Broadcom
pin numbering scheme.
14. 14
Confidential
Version 2 of Pi4J
As of Version 2.0, Pi4J will no longer include support for peripheral devices and add-on
chipsets/boards as part of the core project. A new plugin model has been introduced in version
2.0 that should help enable third-party development and support third-party add-ons which may
be developed and maintained independently of the core Pi4J project.
Main features:
• Fluent APIs/Interfaces
• Immutable Runtime Context
• Extensible Provider/Platform/Plug-in
Architecture
• Builder-patterns for creating new I/O
instances
• Dependency Injection via Pi4J Annotations
• Well-documented source code
• Hardware PWM Support
• Remote I/O Support (via TCP/IP)
• Java 11
• In addition to the features listed above, Pi4J
version 2.0 also abandons the old WiringPi
pin numbering scheme in favor of the more
traditional and commonly used Broadcom
pin numbering scheme.
15. 15
Confidential
GPIO Programming with Pi4J
• Hardware Pins to Motorola Broadcom chipset numbering
• Physical pin locations can change between board revisions
• Pi4J can use Broadcom or WiringPi numbering schemes
• Input or Output Pins
• High (3.3 VDC) or Low (0 VDC) States
• Outputs used to control things like relays, lights, motors, bells, etc.
• Pullup and Pulldown resistors
16. 16
Confidential
Main design classes (Pi4J 1.4)
• Listeners. Provides the ability to react on
events from triggers
• Triggers. Create an event on state
change
• Components (Deprecated in Pi4J 2.0)
Other Components
• Pull up and Pull down Resistors
• Toggle output and handy methods to access
pins and change their states
17. 17
Confidential
Pull up and Pull Down resistors
The pull-down resistor pulls the voltage
down to zero. If the pull-up switch is pressed,
it pulls the voltage up to whatever the +
supply is.
The pull-up resistor pulls the voltage up to
whatever the + supply is. If the pull-down
switch is pressed, it pulls the voltage down to
zero.
18. 18
Confidential
GPIO Output Circuit Example
GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalOutput output =
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_12,
PinState.LOW);
output.high(); // set to high state
output.low(); // set to low state
output.toggle(); // switch to other state
output.pulse(1000); // set state for a duration
20. 20
Confidential
Resources
• Pi4J - https://pi4j.com/1.2/index.html
• GitHub Pi4J - https://github.com/Pi4J/pi4j
• Pi4J 2.0 - https://pi4j.com/
• GPIO - https://www.raspberrypi.org/documentation/usage/gpio/README.md
• WiringPi - http://wiringpi.com/
• Extensive support for many devices for many languages -
https://github.com/ControlEverythingCom
• Hardware Vendors (SeeedStudio Grove, Adafruit – Provides Python code for
most of their hardware, Texas Instruments (sold by Mouser, DigiKey, etc.)
• Pi Starter and Learning Kit Vendors (CanaKit, Adeept, etc.)