Posts: 18
Joined: Tue Mar 30, 2021 10:33 pm

Help with PIR motion sensor listener and pi4j

Thu May 13, 2021 11:08 pm

HI there, I'm still pretty new to all of this, but it seems like this code should work. Here is what I'm trying to do: I have a PIR Motion sensor connected with the VCC to the 5 volt Pin of a RaspBerry PI4. I have the ground, of course, going to ground, and the data line in the middle going to GPIO17. I have a line running from GPIO17 to an LED with a resistor. I've tested the light and it works when connected to power. I've checked this sensor and it works, too. It must be something with my code. The motion detector is supposed to set off the LED, and also activate the raspberry pi cam, using a class from a library I found on these forums, which uses the raspistill command. I will include the code below (I'm using Java and the pi4j libraries). I've instantiated two GpioController objects, which I used to provision the pins: GPIO_27 (as an input) , and GPIO_17 (as an output). Then added a listener to the sensor GpioPinDigitalInput (GPIO_17), since that is the one with the data. I'll include a picure of my wiring, in case I got that wrong too somehow. Any suggestions are greatly appreciated. The code compiles, and I can execute it using Netbeans Remote Platform option--it runs, but no response on the hardware side of things...the led doesn't light, and the motion detector isn't activated. I'm sure someone must know what I've failed to get right here. Thank you so much for any time you can give.

Here are my exact connections:
PIR MotionSensor

GND to GND line of breadboard, which runs to cathod (short leg...?) of LED
Led anode to 10Kom resistor, back to GND

I am using a raspberrypi, and the RaspiStill class in the code below.

package aircam;

Code: Select all

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.alatheia.kshetra.RPiCamera;
import com.alatheia.kshetra.exceptions.FailedToRunRaspistillException;
import com.alatheia.kshetra.enums.AWB;
import com.alatheia.kshetra.enums.DRC;
import com.alatheia.kshetra.enums.Encoding;
import com.alatheia.kshetra.enums.Exposure;
import com.alatheia.kshetra.enums.ImageEffect;
import com.alatheia.kshetra.enums.MeteringMode;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

 * @author glass
public class AirCam {

     * @param args the command line arguments
    public static void main(String[] args) throws FailedToRunRaspistillException, IOException, InterruptedException {
        // TODO code application logic here
        System.out.println("<--PI4J Attempt #1 -->");

     * @param args the command line arguments

System.out.println("Starting Pi4J Motion Sensor Example");					
// create gpio controller			
final GpioController gpioSensor = GpioFactory.getInstance(); 
final GpioPinDigitalInput sensor = gpioSensor.provisionDigitalInputPin(RaspiPin.GPIO_27, PinPullResistance.PULL_DOWN);			
// create another gpio controller							
final GpioController gpioLED = GpioFactory.getInstance();			
final GpioPinDigitalOutput led = gpioLED.provisionDigitalOutputPin(RaspiPin.GPIO_17, "LED", PinState.LOW);			
// create and register gpio pin listener			
sensor.addListener(new GpioPinListenerDigital() {			
    public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {		
            System.out.println("Motion DetectOR WORKING.....!"); 
            try {
                System.out.println("in RaspiCam try block");
                //Take the  picture
                RPiCamera piCamera = new RPiCamera().setEncoding(Encoding.PNG);
                piCamera.takeStill("AStillImage.png", 500, 500);
            } catch (FailedToRunRaspistillException ex) {
                Logger.getLogger(AirCam.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(AirCam.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(AirCam.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("All is quiet...");
try {			
    // keep program running until user aborts		
    for (;;) {	
catch (final Exception e) {			

Return to “Java”