Tutorials:JavaGPIO

Aus openAAL
Wechseln zu: Navigation, Suche

Java

Java using the Pi4J Library

This uses the Java library available at http://www.pi4j.com/. (Any Java application that controls GPIO must be run as root.)

Please note that the Pi4J library uses the WiringPi GPIO pin numbering scheme <ref>http://pi4j.com/usage.html#Pin_Numbering</ref> <ref>https://projects.drogon.net/raspberry-pi/wiringpi/pins/</ref>. Please see the usage documentation for more details: http://pi4j.com/usage.html


public static void main(String[] args) {

    // create gpio controller
    GpioController gpio = GpioFactory.getInstance();
        
    // provision gpio pin #01 as an output pin and turn off
    GpioPinDigitalOutput outputPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.LOW);
    
    // turn output to LOW/OFF state
    outputPin.low();

    // turn output to HIGH/ON state
    outputPin.high();


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

    // get input state from pin 2
    boolean input_value = inputPin.isHigh();
}

More complete and detailed examples are included on the Pi4J website at http://www.pi4j.com/.

The Pi4J library includes support for:

  • GPIO Control
  • GPIO Listeners
  • Serial Communication
  • I2C Communication
  • SPI Communication