0

I'm trying to turn on and off relay using raspberry pi 3. But after successfully turn on, it will stay on even after I set GPIO pin to low.

I use pi4j example and python example, both of them give me same result.

this is the code

final GpioController gpio = GpioFactory.getInstance();

    final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_29, "MyLED", PinState.HIGH);

    pin.setShutdownOptions(true, PinState.LOW);

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

    pin.low();
    System.out.println("--> GPIO state should be: OFF");
    Thread.sleep(5000);

    pin.toggle();
    System.out.println("--> GPIO state should be: ON");
    Thread.sleep(5000);

    pin.toggle();
    System.out.println("--> GPIO state should be: OFF");
    Thread.sleep(5000);

    pin.pulse(1000, true); // set second argument to 'true' use a blocking call

    gpio.shutdown();

Please help, thank you.

  • Welcome to the site. It isn't possible to provide reasonable answers to your question without some more information. At a minimum, could you please edit your question to include a complete code sample showing how you're turning your pins on and off, and some details on how your Pi is connected to the relay (photographs work best). – goobering Feb 16 '17 at 13:10
  • 1
    You have probably got the wrong sort of relay board. Can you independently test the state of the GPIO to see if it is low or high? – joan Feb 16 '17 at 13:32
  • @joan after test the state of GPIO, it give the low and high state just like the code above, maybe I got the wrong relay board. I use relay from Arduino where it work perfectly. I check the relay and found "Low Level Trigger", any suggestion ? – Arief Taufikurrahman Feb 16 '17 at 13:59
  • Does the relay switch off if you change the GPIO to an INPUT? Some switch on when you set the GPIO as a high OUPUT and switch off when you set the GPIO as an INPUT. – joan Feb 16 '17 at 14:18
  • 1
    That's now what I suggested. I suggested you change the mode of the GPIO from an OUTPUT to an INPUT in your script. Perhaps you can't change the mode with pi4j. – joan Feb 16 '17 at 14:27

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.