0

I am so new to Raspberry Pi. I want to use pi4j 2 to flash a led that is connected to a raspberry pi. This is my code but the led does not turn on at all:

import com.pi4j.Pi4J;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.gpio.digital.DigitalState;

public class RobotResource {
private static final int PIN_LED = 16;

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

    var pi4j = Pi4J.newAutoContext();
    int x = 0;

    var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
            .id("led")
            .name("LED Flasher")
            .address(PIN_LED)
            .shutdown(DigitalState.LOW)
            .initial(DigitalState.LOW)
            .provider("pigpio-digital-output");

    var led = pi4j.create(ledConfig);


    while (x != 5) {

        led.high();
        sleep(1000);
        led.low();
        sleep(500);
        x++;
    }
}

static void sleep(int z) {

    try {
        Thread.sleep(z);
    } catch (InterruptedException ex) {
        System.out.println("Thread.sleep");
    }
}
}

What should I do to turn on the led?

Thanks in advance for your guide.

3
  • Which tutorial are your reading? Perhaps try this one: (1) The Pi4J Project v1.2 - 2019-03-05 pi4j.com/1.2/index.html (2) Simple GPIO Control using Pi4J - The following example demonstrates the simple control of a GPIO pin on the Raspberry Pi pi4j.com/1.2/example/control.html
    – tlfong01
    Commented Jun 3, 2022 at 13:09
  • @tlfong01 I found the code in the StackOverflow. I cant use pi4j v1.2. must use pi4j v2.0. Commented Jun 3, 2022 at 14:59
  • Try the following. sudo pigpiod then pigs w 16 1 then pigs w 16 0. Does the LED go on then off? If not check you have connected it to GPIO 16 (NOT pin 16) and that it is connected the right way around. pinout.xyz
    – joan
    Commented Jun 3, 2022 at 15:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.