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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
rezza72
  • 127
  • 1
  • 11
  • 1
    I've run your code on a Raspberry Pi 4 - and although I don't have an LED attached, if I run `raspi-gpio get 16` I can see the level of the GPIO pin change between high and low (I've increased the sleep times so I can see the change). Are you wiring your LED to the correct pin? Pi4J is using the BCM numbering system - so pin 16 is physical pin 36. – cguk70 May 24 '22 at 19:18
  • @cguk70 I connected a LED to pins 2(+5) and 20(GND) and the LED turned on without any problem. but when I connect the LED to pins 2(+5) and 16 (GPIO 23) the LED doesn't flash. what's your mean for pin 36? – rezza72 May 25 '22 at 04:14
  • 1
    First - never connect 5v to the gpio pins - the gpio pins are rated at 3.3v and connecting 5v to them will damage the pi. The LED should be connected between pin 36 (GPIO 16) and pin 20 (GND) in series with a 330 ohm resistor to protect the pi. The GPIO pin will provide +3.3v to power the LED when high. Pin 16 in Pi4j means GPIO 16 not physical pin 16. – cguk70 May 25 '22 at 05:54
  • @cguk70 I did not say I connected +5 to the GPIO pin! I said I connected the +5 output of raspberry pi, which is pin 2, to the LED pin to see if the LED turns on with a +5 or not. then it turned on perfectly. BUT When I connected the LED to pin 16, it did not flash. Now, according to what you guided, I connected the LED to pin 36 and ran the program, but the LED still did not turn on :( – rezza72 May 26 '22 at 08:52
  • 1
    Ah OK - from your comment "when I connect the LED to pins 2(+5) and 16 (GPIO 23)" - I thought you were trying to connect the LED between those two pins. Not sure what else to suggest. I did run your program OK and saw the level of the pin change although I'm not in a position to connect an LED at the moment. You could try running `raspi-gpio get 16` to read the level of the pin whilst your program is running and see if that is working. You could also try posting on [raspberrypi.stackexchange.com](https://raspberrypi.stackexchange.com/) - though they may want to see a diagram of your wiring. – cguk70 May 26 '22 at 15:36

0 Answers0