GPIO Triggers Example using Pi4J.

The following example demonstrates how to setup GPIO triggers. GPIO Triggers listen for pin state changes and then perform some action on on alternate GPIO pin or perform a callback to a user specified method. Pi4J includes several built in pre-defined trigger implementation classes or you can create your own trigger by implementing the GpioTrigger interface. Triggers are based on the GPIO listener implementation and rely on GPIO hardware interrupts not state polling.

Source Code

The source code for this example is included in the github repository:

 * #%L
 * **********************************************************************
 * PROJECT       :  Pi4J :: Java Examples
 * FILENAME      :  
 * This file is part of the Pi4J project. More information about 
 * this project can be found here:
 * **********************************************************************
 * %%
 * Copyright (C) 2012 - 2013 Pi4J
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%

import java.util.concurrent.Callable;


 * This example code demonstrates how to setup simple triggers for GPIO pins on the Raspberry Pi.
 * @author Robert Savage
public class TriggerGpioExample {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("<--Pi4J--> GPIO Trigger Example ... started.");

        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, 
        System.out.println(" ... complete the GPIO #02 circuit and see the triggers take effect.");
        // setup gpio pins #04, #05, #06 as an output pins and make sure they are all LOW at startup
        GpioPinDigitalOutput myLed[] = { 
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "LED #1", PinState.LOW),
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "LED #2", PinState.LOW),
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06, "LED #3", PinState.LOW) 
        // create a gpio control trigger on the input pin ; when the input goes HIGH, also set gpio pin #04 to HIGH
        myButton.addTrigger(new GpioSetStateTrigger(PinState.HIGH, myLed[0], PinState.HIGH));

        // create a gpio control trigger on the input pin ; when the input goes LOW, also set gpio pin #04 to LOW
        myButton.addTrigger(new GpioSetStateTrigger(PinState.LOW, myLed[0], PinState.LOW));

        // create a gpio synchronization trigger on the input pin; when the input changes, also set gpio pin #05 to same state
        myButton.addTrigger(new GpioSyncStateTrigger(myLed[1]));

        // create a gpio pulse trigger on the input pin; when the input goes HIGH, also pulse gpio pin #06 to the HIGH state for 1 second
        myButton.addTrigger(new GpioPulseStateTrigger(PinState.HIGH, myLed[2], 1000));

        // create a gpio callback trigger on gpio pin#4; when #4 changes state, perform a callback
        // invocation on the user defined 'Callable' class instance
        myButton.addTrigger(new GpioCallbackTrigger(new Callable<Void>() {
            public Void call() throws Exception {
                System.out.println(" --> GPIO TRIGGER CALLBACK RECEIVED ");
                return null;

        // keep program running until user aborts (CTRL-C)
        for (;;) {

        // stop all GPIO activity/threads by shutting down the GPIO controller
        // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
        // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO controller        

Wiring Diagram

The following circuit can be used in conjunction with this sample code.

(click here for hi-resolution image)


If you have not already downloaded and installed the Pi4J library on the RaspberryPi, then view this page for instructions on where to download and how to install Pi4J:
Download & Install Pi4J

First, locate the source file in the samples folder of the Pi4J installation on the RaspberryPi.
You can use the following command on the Pi's console or SSH terminal to navigate to this path:
cd /opt/pi4j/examples


Next, use the following command to compile this example program:

javac -classpath .:classes:/opt/pi4j/lib/'*' -d .


The following command will run this example program:

sudo java -classpath .:classes:/opt/pi4j/lib/'*' TriggerGpioExample


The LED attached to GPIO pin 21 should turn ON when the momentary button is pressed and OFF when the button is released.

The LED attached to GPIO pin 22 should turn ON when the momentary button is pressed and OFF when the button is released.

The LED attached to GPIO pin 23 should turn ON for 1 second when the momentary button is pressed. The button release has no effect on this LED.

The following message should be displayed on the console each time the momentary button is pressed and again when released.