Skip to content

Creating a Java GPIO Library for Orange Pi H3

Controlling LEDs and buttons on an Orange Pi H3 can be made easier with a custom Java library that handles LED control and button events such as single click, double click, and long press. This tutorial explains how to build such a library using the WiringOP command-line GPIO interface.


1. Wiring Setup

LED:

  • Physical Pin 7 → LED positive
  • Physical Pin 6 or 14 → LED ground

Button:

  • Physical Pin 11 → button input
  • Physical Pin 6 or 14 → button ground

Use pull-up configuration so the button pin reads HIGH when not pressed and LOW when pressed.


2. OrangePiGpio Java Library

This library encapsulates GPIO control and button event handling.

java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

public class OrangePiGpio {

    private int ledPin;
    private int buttonPin;

    private boolean lastState = true; // pull-up: HIGH when not pressed
    private long lastPressTime = 0;
    private long pressStartTime = 0;
    private Timer timer = new Timer(true);

    // Event callbacks
    public interface ButtonListener {
        void onClick();
        void onDoubleClick();
        void onLongPress();
    }

    private ButtonListener listener;

    public OrangePiGpio(int ledPin, int buttonPin) throws Exception {
        this.ledPin = ledPin;
        this.buttonPin = buttonPin;
        init();
    }

    private void init() throws Exception {
        runCommand("gpio mode " + ledPin + " out");
        runCommand("gpio write " + ledPin + " 0"); // LED OFF
        runCommand("gpio mode " + buttonPin + " in");
        runCommand("gpio mode " + buttonPin + " up"); // internal pull-up
    }

    public void setButtonListener(ButtonListener listener) {
        this.listener = listener;
        startPolling();
    }

    public void lightOn() throws Exception {
        runCommand("gpio write " + ledPin + " 1");
    }

    public void lightOff() throws Exception {
        runCommand("gpio write " + ledPin + " 0");
    }

    private void startPolling() {
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    boolean pressed = readPin(buttonPin) == 0;
                    if (pressed && lastState) {
                        pressStartTime = System.currentTimeMillis();
                    }
                    if (!pressed && !lastState) {
                        long duration = System.currentTimeMillis() - pressStartTime;
                        if (duration >= 2000 && listener != null) {
                            listener.onLongPress();
                        } else if (listener != null) {
                            long now = System.currentTimeMillis();
                            if (now - lastPressTime < 500) {
                                listener.onDoubleClick();
                            } else {
                                listener.onClick();
                            }
                            lastPressTime = now;
                        }
                    }
                    lastState = pressed;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, 50);
    }

    private int readPin(int pin) throws Exception {
        return Integer.parseInt(runCommand("gpio read " + pin).trim());
    }

    private String runCommand(String command) throws Exception {
        ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
        Process process = pb.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) sb.append(line);
        process.waitFor();
        return sb.toString();
    }
}

3. Using the Library

java
public class Main {
    public static void main(String[] args) throws Exception {
        OrangePiGpio gpio = new OrangePiGpio(7, 0); // LED pin 7, Button pin 0

        gpio.setButtonListener(new OrangePiGpio.ButtonListener() {
            @Override
            public void onClick() {
                System.out.println("Single click!");
            }
            @Override
            public void onDoubleClick() {
                System.out.println("Double click!");
            }
            @Override
            public void onLongPress() {
                System.out.println("Long press!");
            }
        });

        while (true) {
            gpio.lightOn();
            Thread.sleep(1000);
            gpio.lightOff();
            Thread.sleep(1000);
        }
    }
}

4️⃣ Features

  • LED control: lightOn() and lightOff()
  • Button events: onClick(), onDoubleClick(), onLongPress()
  • Pull-up enabled by default for safe button wiring
  • Adjustable polling interval for responsiveness

This library can be extended to support multiple buttons and LEDs, or more complex button event patterns for your Orange Pi H3 projects.