Turn on LED using pi4j and Raspberry pi 4

287 Views Asked by At

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.

0

There are 0 best solutions below