I recently bought a PIR Motion Sensor and wrote a java program which prints in the console whenever movement is detected. I connected the pins properly, and the signal to GPIO_17 according to the BCM numbering scheme. Here's my code:
import com.pi4j.io.gpio.*; import com.pi4j.io.gpio.event.GpioPinListenerDigital; import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; public class Main { public static void main(String[] args) { GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING)); GpioController gpio = GpioFactory.getInstance(); GpioPinDigitalInput motionDetector = gpio.provisionDigitalInputPin(RaspiBcmPin.GPIO_17, "motionDetector", PinPullResistance.PULL_DOWN); System.out.println("Motiondetector started."); motionDetector.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { System.out.println("State changed"); } }); while(true) { try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } } } } This code should print the text "State changed" in the console whenever I move my hand in front of it, but for some reason nothing happens. But when I add this code to the while loop,
System.out.println(motionDetector.getState()); It does print high every 50 milliseconds whenever I move my hand and low when nothing moves. So for some reason the event doesn't trigger. Can anybody tell me why this happens, and how I can solve it?