Pi4J Java – PIR Sensor interfaced to Raspberry Pi

Pi4J Java – PIR Sensor interfaced to Raspberry Pi

I decided to play around again. Java was the first programming language that caught my interest, and when it came to embedded programming. Python provided lesser lines of codes but I still feel that my bae Java will one day come knocking.

 

See Also:

Yea, I stumbled on Pi4J Library although I have been seeing it around before. I think Python will have to hold on for some time. In this tutorial, I will try to detect motion with PIR sensor connected to Raspberry Pi and an LED will light up when motion is detected. I have made some posts on PIR earlier, you can check them out also. The materials for this post are:

Setup

  • PIR VCC  –> RPi 5volts
  • PIR GND –> RPi GND
  • PIR out –> RPi Pin GPIO 29 (using Pi4j Pin Numbering)
  • LED –> RPi Pin GPIO 4 (using Pi4j Pin Numbering)

 

 

Code

This factory class provides a static method to create new ‘GpioController’ instances.

Provisioning configures the pin based on how you intend to use it. Provisioning can automatically export the pin, set its direction, and setup any edge detection for interrupt based events.

Pi4J provides an implementation for monitoring GPIO pin interrupts. This is a far more efficient method for actively monitoring pin states versus constantly polling and interrogating the pin state.

You must create a class that implements the GpioListener interface. This interface simply contain a single method pinStateChanged(GpioPinStateChangeEvent event) that is used to notify the listener class when a pin change event has occurred.

 

The full code is available on GITHUB .

 

Feel free to drop your comments about this post and follow our social media channels for more tutorials like these.

Leave a Reply

Your email address will not be published. Required fields are marked *