Pi4J is an open source Java library that provides an abstraction layer for hardware I/O interactions with the Raspberry Pi. It allows controlling inputs and outputs through object-oriented APIs and listeners. The document discusses Pi4J's GPIO, trigger, and component APIs and provides examples of using them to control an LED based on input from a button. More advanced components include sensors, displays, and relays.
2. What is Raspberry Pi?
• Single Board Computer
• Linux Operating system
• ARM Architecture
• Low Cost
• Low Power
3. 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
4. Acknowledgment to Pi4J Creator
Robert Savage
Software Architect
savage.home.automation
Examples for this
session has been taken
from demos created by
Robert Savage for
JavaOne 2013
Conference.
https://github.com/sav
agehomeautomation/p
i4j-javaone-demos
5. “We perform I/O
Programming on
Raspberry Pi to
realize IoT”
Input / Output
“We are working
on hardware I/O”
We withdraw
Physical Output
Or
Provide Physical
Input
6. GPIO – Main Programmable I/O
Interface
It acts as
General Purpose I/O – GPIO
Apart from that it also
supports via Configuration
UART - Serial / RS232
SPI - Serial Peripheral
Interface
I2C - Inter-Integrated Circuit
PWM - Pulse-Width-
Modulation
Pi4J provides Java Classes
supporting all of these I/O
Operations
7. Pull up and Pull Down
In these circuits
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.
8. 3 Main Designs Classes
• Listeners
• Triggers
• Component
Other Components
• Pull up and Pull down Resistors
• Toggle output and handy methods to access
pins and change their states
9. Pi4J : GPIO Demo
• Basic program using a simple
momentary button (input)
and LED (output).
• GOAL:
Illuminate LED when the button
is pressed. Deactivate LED when
button is released.
14. Pi4J : GPIO Trigger Demo
• Pi4J provides simple
automation triggers for common
GPIO event interactions.
( == less code)
• GOAL:
Toggle LED state when the button
is pressed.
19. Pi4J : Component API
• The component APIs provides an abstraction
layer from the hardware I/O layer.
• This allows hardware design/circuitry to change
with *less* impact to your implementation code.
• For example, a RELAY could be controlled from
GPIO, RS232, SPI, or I2C. You program defines the
RELAY impl up front based on the hardware
interface, but the rest of your program logic
works against the RELAY component interface
and not the direct hardware /communication IO
interfaces.
20. Pi4J : Component API
• Keypad
• Light / LED
• Dimmable Light
• LCD
• Power Controller
• Relay
• Momentary Switch
• Toggle Switch
• Analog Sensor
• Distance Sensor
• Motion Sensor
• Temperature Sensor
23. ComponentDemo:SampleCode
// create GPIO controller
final GpioController gpio = GpioFactory.getInstance();
// momentary push-button switch; activates when button is pushed
final MomentarySwitch momentarySwitch = new GpioMomentarySwitchComponent(
gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP),
PinState.HIGH, // "OFF" PIN STATE
PinState.LOW); // "ON" PIN STATE
// led; illuminates when momentary switch is pushed
final LED led = new GpioLEDComponent(
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW));
// create momentary switch event listener
momentarySwitch.addListener(new SwitchListener() {
@Override
public void onStateChange(SwitchStateChangeEvent event) {
if(event.getNewState() == SwitchState.ON){
led.on(); // turn ON LED
}
else{
led.off(); // turn OFF LED
}
}
});
28. References
• Pi4J - http://pi4j.com/
• Fritgzing - http://fritzing.org/home/
• Wiring Pi - http://wiringpi.com/
• More Examples -
https://github.com/Pi4J/pi4j/tree/master/pi4j
-example
• Watch Interview of Robert Savage -
https://www.youtube.com/watch?v=Z_eI7DfD
MjI