Home Code Java GPIO push button example

Java GPIO push button example

by shedboy71

Connect a push button to a GPIO pin and check for a button press

Schematic

pi push button

pi push button

Code

[codesyntax lang=”java”]

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;


public class pushbutton 
{
    
    public static void main(String args[]) throws InterruptedException 
	{
        System.out.println("Push button example");
        
        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // gpio pin #02 as an input pin with its internal pull down resistor enabled
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

        // create and register gpio pin listener
        myButton.addListener(new GpioPinListenerDigital() 
		{
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) 
			{
                // display pin state on console
                System.out.println(" Switch change detected: " + event.getPin() + " = " + event.getState());
            }
            
        });
        
        
        // keep program running until user aborts (CTRL-C)
        for (;;) {
            Thread.sleep(1500);
        }
             
    }
}

[/codesyntax]

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More