LED Button


The LedButton is a template class, that you can use in your own Java-project. You can take any Button with a LED you want to. Like for example the big button bellow in the picture gallery.

The Template Class gives you the option to check the state of the button, and to create simple events if the button is pressed or depressed, or the whole time is is being pressed. Also it lets you control the LED.


LEDButton Layout


A simple example on how to use the Button-Class from the Hardware-Catalog:

        System.out.println("LED button app started ...");

// Initialize the button component
final LedButton ledButton = new LedButton(pi4j, PIN.D26, Boolean.FALSE, PIN.PWM19);

// Turn on the LED to have a defined state
//see the LED for a Second

// Register event handlers to print a message when pressed (onDown) and depressed (onUp)
ledButton.btnOnDown(() -> System.out.println("Pressing the Button"));
ledButton.btnOnUp(()   -> System.out.println("Stopped pressing."));

// Wait for 15 seconds while handling events before exiting
System.out.println("Press the button to see it in action!");

// Make a flashing light by toggling the LED every second
// in the meantime, the Button can still be pressed, as we only freeze the main thread
for (int i = 0; i < 15; i++) {

// Unregister all event handlers to exit this application in a clean way

System.out.println("LED button app done.");

Further application

The class is implemented in the two sample projects Theremin and Photobooth.

Further project ideas

  • An application, which includes a button. if the button is pressed, the app will order you a crate of beer from your favorite store.
  • An application, where you can play “whack a mole”. If the LED is on and you hit the right button, you get points.