SlideShare a Scribd company logo
1 of 28
Pi4J and Pi
Narendran Solai Sridharan
What is Raspberry Pi?
• Single Board Computer
• Linux Operating system
• ARM Architecture
• Low Cost
• Low Power
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
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
“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
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
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.
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
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.
GPIODemo:WiringDiagram
Gnd
GPIO1
GPIO26
Gnd
GPIODemo:ProgramLogic
// create GPIO controller
final GpioController gpio = GpioFactory.getInstance();
// momentary push-button switch; input pin
final GpioPinDigitalInput buttonPin =
gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_UP);
// led; output pin
final GpioPinDigitalOutput ledPin =
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_26, PinState.LOW);
// create event listener for button input pin
buttonPin.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
if(event.getState().isHigh()){
// turn off LED pin
ledPin.setState(PinState.LOW);
}
else{
// turn on LED pin
ledPin.setState(PinState.HIGH);
}}
});
GPIODemo:SampleCode
GPIODemo:InAction
Pi4J : GPIO Demo
Demo Time!!!
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.
GPIOTriggerDemo:WiringDiagram
Gnd
GPIO18
GPIO12
Gnd
GPIOTriggerDemo:ProgramLogic
// create GPIO controller
final GpioController gpio = GpioFactory.getInstance();
// momentary push-button switch; activates when button is pushed
final GpioPinDigitalInput buttonPin =
gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_UP);
// led; illuminates when GPIO is HI
final GpioPinDigitalOutput ledPin =
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_26, PinState.LOW);
// create button event listener
buttonPin.addTrigger(new GpioToggleStateTrigger(PinState.LOW, ledPin));
GPIOTriggerDemo:SampleCode
GPIOTriggerDemo:InAction
Pi4J : GPIO Trigger Demo
Demo Time!!!
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.
Pi4J : Component API
• Keypad
• Light / LED
• Dimmable Light
• LCD
• Power Controller
• Relay
• Momentary Switch
• Toggle Switch
• Analog Sensor
• Distance Sensor
• Motion Sensor
• Temperature Sensor
ComponentDemo:WiringDiagram
Gnd
GPIO1
GPIO26
Gnd
ComponentDemo:ProgramLogic
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
}
}
});
ComponentDemo:InAction
Pi4J : Component Demo
Demo Time!
More Components
Pi4J : I/O Expansion
• GPIO General Purpose I/O
• PWM Pulse-Width Modulation
• ADC Analog-to-Digital Converter
• DAC Digital-to-Analog Converter
https://github.com/Pi4J/pi4j/tree/master/pi4j-gpio-extension
Pi4J : Components & Devices
Abstraction
Components
• LCD
• Buzzer
• Light
• Motor
• Potentiometer
• Motion Sensor
& Other
Sensors
• Power & Relay
https://github.com/Pi4J/pi4j/tree/master/pi4j-device/
Devices
• Fireplace
• Garage
• Gate
• Pibrella
• Sprinklers
• Access
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

More Related Content

Similar to Control GPIO Pins and Components with Pi4J

Java Webinar #9: “Raspberry Pi Platform for Java Programmers”
Java Webinar #9: “Raspberry Pi Platform for Java Programmers”Java Webinar #9: “Raspberry Pi Platform for Java Programmers”
Java Webinar #9: “Raspberry Pi Platform for Java Programmers”GlobalLogic Ukraine
 
IoT from java perspective
IoT from java perspectiveIoT from java perspective
IoT from java perspectiveDmytro Panin
 
Internet of Things With PHP
Internet of Things With PHPInternet of Things With PHP
Internet of Things With PHPAdam Englander
 
Raspberry-Pi GPIO
Raspberry-Pi GPIORaspberry-Pi GPIO
Raspberry-Pi GPIOSajib Sen
 
Raspberry Pi Introductory Lecture
Raspberry Pi Introductory LectureRaspberry Pi Introductory Lecture
Raspberry Pi Introductory LectureSyed Umaid Ahmed
 
Ins and Outs of GPIO Programming
Ins and Outs of GPIO ProgrammingIns and Outs of GPIO Programming
Ins and Outs of GPIO ProgrammingICS
 
02 Raspberry Pi GPIO Interface on Node-RED (Some correction)
02 Raspberry Pi GPIO Interface on Node-RED (Some correction)02 Raspberry Pi GPIO Interface on Node-RED (Some correction)
02 Raspberry Pi GPIO Interface on Node-RED (Some correction)Mr.Nukoon Phimsen
 
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...Anne Nicolas
 
Android Based Robots
Android Based RobotsAndroid Based Robots
Android Based Robotsrobotics25
 
Introduction To Raspberry Pi with Simple GPIO pin Control
Introduction To Raspberry Pi with Simple GPIO pin ControlIntroduction To Raspberry Pi with Simple GPIO pin Control
Introduction To Raspberry Pi with Simple GPIO pin ControlPradip Bhandari
 
Android Things - The IoT platform from Google
Android Things - The IoT platform from GoogleAndroid Things - The IoT platform from Google
Android Things - The IoT platform from GoogleEmmanuel Obot
 
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game ConsoleRaspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game ConsoleRICELEEIO
 
Part-1 : Mastering microcontroller with embedded driver development
Part-1 : Mastering microcontroller with embedded driver development Part-1 : Mastering microcontroller with embedded driver development
Part-1 : Mastering microcontroller with embedded driver development FastBit Embedded Brain Academy
 
Hands-on Labs: Raspberry Pi 2 + Windows 10 IoT Core
Hands-on Labs: Raspberry Pi 2 + Windows 10 IoT CoreHands-on Labs: Raspberry Pi 2 + Windows 10 IoT Core
Hands-on Labs: Raspberry Pi 2 + Windows 10 IoT CoreAndri Yadi
 
Democratizing the Internet of Things
Democratizing the Internet of ThingsDemocratizing the Internet of Things
Democratizing the Internet of ThingsAdam Englander
 
Got Python I/O: IoT Develoment in Python via GPIO
Got Python I/O: IoT Develoment in Python via GPIOGot Python I/O: IoT Develoment in Python via GPIO
Got Python I/O: IoT Develoment in Python via GPIOAdam Englander
 
Lab 2_ Programming an Arduino.pdf
Lab 2_ Programming an Arduino.pdfLab 2_ Programming an Arduino.pdf
Lab 2_ Programming an Arduino.pdfssuser0e9cc4
 

Similar to Control GPIO Pins and Components with Pi4J (20)

Java Webinar #9: “Raspberry Pi Platform for Java Programmers”
Java Webinar #9: “Raspberry Pi Platform for Java Programmers”Java Webinar #9: “Raspberry Pi Platform for Java Programmers”
Java Webinar #9: “Raspberry Pi Platform for Java Programmers”
 
IoT from java perspective
IoT from java perspectiveIoT from java perspective
IoT from java perspective
 
Internet of Things With PHP
Internet of Things With PHPInternet of Things With PHP
Internet of Things With PHP
 
4. GPIO Access
4. GPIO Access4. GPIO Access
4. GPIO Access
 
Raspberry-Pi GPIO
Raspberry-Pi GPIORaspberry-Pi GPIO
Raspberry-Pi GPIO
 
Raspberry Pi Introductory Lecture
Raspberry Pi Introductory LectureRaspberry Pi Introductory Lecture
Raspberry Pi Introductory Lecture
 
Ins and Outs of GPIO Programming
Ins and Outs of GPIO ProgrammingIns and Outs of GPIO Programming
Ins and Outs of GPIO Programming
 
02 Raspberry Pi GPIO Interface on Node-RED (Some correction)
02 Raspberry Pi GPIO Interface on Node-RED (Some correction)02 Raspberry Pi GPIO Interface on Node-RED (Some correction)
02 Raspberry Pi GPIO Interface on Node-RED (Some correction)
 
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
 
Android Based Robots
Android Based RobotsAndroid Based Robots
Android Based Robots
 
Introduction To Raspberry Pi with Simple GPIO pin Control
Introduction To Raspberry Pi with Simple GPIO pin ControlIntroduction To Raspberry Pi with Simple GPIO pin Control
Introduction To Raspberry Pi with Simple GPIO pin Control
 
PPT+.pdf
PPT+.pdfPPT+.pdf
PPT+.pdf
 
Raspberry pi led blink
Raspberry pi led blinkRaspberry pi led blink
Raspberry pi led blink
 
Android Things - The IoT platform from Google
Android Things - The IoT platform from GoogleAndroid Things - The IoT platform from Google
Android Things - The IoT platform from Google
 
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game ConsoleRaspberry Pi GPIO Tutorial - Make Your Own Game Console
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
 
Part-1 : Mastering microcontroller with embedded driver development
Part-1 : Mastering microcontroller with embedded driver development Part-1 : Mastering microcontroller with embedded driver development
Part-1 : Mastering microcontroller with embedded driver development
 
Hands-on Labs: Raspberry Pi 2 + Windows 10 IoT Core
Hands-on Labs: Raspberry Pi 2 + Windows 10 IoT CoreHands-on Labs: Raspberry Pi 2 + Windows 10 IoT Core
Hands-on Labs: Raspberry Pi 2 + Windows 10 IoT Core
 
Democratizing the Internet of Things
Democratizing the Internet of ThingsDemocratizing the Internet of Things
Democratizing the Internet of Things
 
Got Python I/O: IoT Develoment in Python via GPIO
Got Python I/O: IoT Develoment in Python via GPIOGot Python I/O: IoT Develoment in Python via GPIO
Got Python I/O: IoT Develoment in Python via GPIO
 
Lab 2_ Programming an Arduino.pdf
Lab 2_ Programming an Arduino.pdfLab 2_ Programming an Arduino.pdf
Lab 2_ Programming an Arduino.pdf
 

More from Narendran Solai Sridharan

More from Narendran Solai Sridharan (8)

Java module configuration
Java module configurationJava module configuration
Java module configuration
 
Test NG Framework Complete Walk Through
Test NG Framework Complete Walk ThroughTest NG Framework Complete Walk Through
Test NG Framework Complete Walk Through
 
Introduction to Web Components
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web Components
 
Thinking tools for value innovation
Thinking tools for value innovationThinking tools for value innovation
Thinking tools for value innovation
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Http 2
Http 2Http 2
Http 2
 
Introduction to value types
Introduction to value typesIntroduction to value types
Introduction to value types
 
Upfront adoption & migration of applications to latest jdk
Upfront adoption & migration of applications to latest jdkUpfront adoption & migration of applications to latest jdk
Upfront adoption & migration of applications to latest jdk
 

Recently uploaded

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

Control GPIO Pins and Components with Pi4J

  • 1. Pi4J and Pi Narendran Solai Sridharan
  • 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.
  • 12. // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; input pin final GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_UP); // led; output pin final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_26, PinState.LOW); // create event listener for button input pin buttonPin.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { if(event.getState().isHigh()){ // turn off LED pin ledPin.setState(PinState.LOW); } else{ // turn on LED pin ledPin.setState(PinState.HIGH); }} }); GPIODemo:SampleCode
  • 13. GPIODemo:InAction Pi4J : GPIO Demo Demo Time!!!
  • 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.
  • 17. // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; activates when button is pushed final GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_UP); // led; illuminates when GPIO is HI final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_26, PinState.LOW); // create button event listener buttonPin.addTrigger(new GpioToggleStateTrigger(PinState.LOW, ledPin)); GPIOTriggerDemo:SampleCode
  • 18. GPIOTriggerDemo:InAction Pi4J : GPIO Trigger Demo Demo Time!!!
  • 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 } } });
  • 26. Pi4J : I/O Expansion • GPIO General Purpose I/O • PWM Pulse-Width Modulation • ADC Analog-to-Digital Converter • DAC Digital-to-Analog Converter https://github.com/Pi4J/pi4j/tree/master/pi4j-gpio-extension
  • 27. Pi4J : Components & Devices Abstraction Components • LCD • Buzzer • Light • Motor • Potentiometer • Motion Sensor & Other Sensors • Power & Relay https://github.com/Pi4J/pi4j/tree/master/pi4j-device/ Devices • Fireplace • Garage • Gate • Pibrella • Sprinklers • Access
  • 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