0

I am new to raspberry pi and am trying to use pi4j library to run one of the java samples of pi4j provided. When I issue gpio commandline on my pi, I am able to set the output high or low on the piface. I can't get the java program to do the same. Python emulator works fine too. Here is my java code:

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
public class ControlGpioExample {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("<--Pi4J--> GPIO Control Example ... started.");

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

        // provision gpio pin #01 as an output pin and turn on
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);

        // set shutdown state for this pin
        pin.setShutdownOptions(true, PinState.LOW);

        System.out.println("--> GPIO state should be: ON");

        Thread.sleep(5000);

        // turn off gpio pin #01
        pin.low();
        System.out.println("--> GPIO state should be: OFF");

        Thread.sleep(5000);

        // toggle the current state of gpio pin #01 (should turn on)
        pin.toggle();
        System.out.println("--> GPIO state should be: ON");

        Thread.sleep(5000);

        // toggle the current state of gpio pin #01  (should turn off)
        pin.toggle();
        System.out.println("--> GPIO state should be: OFF");

        Thread.sleep(5000);

        // turn on gpio pin #01 for 1 second and then off
        System.out.println("--> GPIO state should be: ON for only 1 second");
        pin.pulse(1000, true); // set second argument to 'true' use a blocking call

        // 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();
System.out.println("Exiting ControlGpioExample");
    }
}

I am very new to this and am trying to do a simple task of setting an output high or low on the output. I son't know what I am missing. I followed the instructions on http://pi4j.com/example/control.html.

Thanks NH ANy help is appreciated.

  • There are several incompatible GPIO numbering schemes. You need to check that you and the software are both referring to the same GPIO. I think pi4j uses the wiringPi numbering scheme. See pinout.xyz – joan Jul 20 '18 at 21:16
  • Sorry for the question, but, how do I check the GPIO numbering? I tried gpio readall and I could see the numbering of WiringPi and BCM. – nhesuru10 Jul 20 '18 at 21:47
  • BCM 18 corresponds to GPIO_01 which corresponds to wiringPi 1. – nhesuru10 Jul 20 '18 at 21:57
  • Could you edit your question and include details of the commands which worked. That will help. – joan Jul 21 '18 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.