3

I have a raspberry pi connected to a Sainsmart 4-channel relay, where each relay is activated by a LOW signal.

When I set a GPIO pin to output, it defaults to LOW, which of course activates the relay for a split second before I can set it to HIGH. Is there any way to set the default output state before setting a pin to output mode?

Here is a very simplified version of my code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.OUT)

#pin is now outputting LOW by default

GPIO.output(11, GPIO.HIGH)

As you can see above, there is a split second between setting the pin to OUT and setting it to HIGH. During that time the pin is outputting LOW, which is the default state (If I try to set it HIGH before setting it to OUT, I get an error for trying to set the state of an input pin).

Having the pin output LOW even for a split second screws with what I'm trying to achieve. So basically all I want to do is have the pin default to HIGH when I set it to OUT.

4
  • 2
    Odd. I thought most libraries set the level before setting the mode. Please post your code.
    – joan
    Mar 12, 2017 at 17:56
  • I posted some code. The library does set it before switching to output; it just sets it low and doesn't seem to give me any option otherwise. Mar 12, 2017 at 22:30
  • 1
    If you read the RPi.GPIO documentation I think it tells you what to do.
    – joan
    Mar 13, 2017 at 2:17
  • 2
    I think that GPIO.setup(11, GPIO.OUT, inital = GPIO.HIGH) is the best you're going to get.
    – anonymoose
    Mar 24, 2017 at 14:46

1 Answer 1

4

As suggested by anonymoose's comment and RPi.GPIO's documentation RPi.GPIO module basics setting a pin to output allows to specify an initial value:

To set up a channel as an output:

GPIO.setup(channel, GPIO.OUT)

(where channel is the channel number based on the numbering system you have specified (BOARD or BCM)).

You can also specify an initial value for your output channel:

GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH)
2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.