I'm using the GPIO pins on my Pi under Raspian to control a relay and a computer fan. In Python 2, the command

GPIO.setup (18, GPIO.OUT)

Immediately supplies power to the pin and triggers the relay.

GPIO.output (18, False)

Has no effect but

GPIO.output (18, True)

turns off the pin!

Has anyone experienced this behaviour before? There seems to be a small amount of power supplied to the pin on boot as the LED lights dimly but there is not enough to trigger the relay. This is fixed after


is run.

Thanks Jake

  • 1
    In electronics often the logic is reversed. That mean when you mean logical "1" that means 0V on the pin.
    – nsilent22
    Jan 30 '16 at 20:30
  • What are you using between the GPIO pin and the Relay - the pins themselves can only supply a maximum of 50mA at 3.3V across ALL the pins and the amount you can expect to get from a single one is much less than that - you risk damaging a pin permanently by overloading it as an output!
    – SlySven
    Jan 30 '16 at 22:01
  • 1
    @SlySven I'm using relays designed for the Pi which use an external 5v supply as well as the GPIO pin. ebay.co.uk/itm/252051910091 Jan 30 '16 at 22:38

GPIO.setup (18, GPIO.OUT) correctly sets 18 to be an OUTPUT. I say 18 rather than pin 18 or GPIO 18 as you haven't specified the numbering scheme you are using.

When you set 18 as an output its level will be that last written. So if 18 was high it will output high and if it was low it will output low.

GPIO.setup (18, False) is an incorrect use of the function and makes no sense. It should preferably be trapped as an error. False has the value 0. The value of GPIO.OUT is 0 so coincidentally this call duplicates the previous call.

GPIO.setup (18, True) is an incorrect use of the function and makes no sense. It should preferably be trapped as an error. True has the value 1. The value of GPIO.IN is 1 so coincidentally this set 18 to be an INPUT. Given that no voltage is set by the relay on 18 then it will be low.

GPIO.cleanup() sets all used GPIO/pins to be inputs. So 18 will again presumably read low.

  • Using GPIO.HIGH and GPIO.LOW in place of true and false is still reversed. LOW turns the relay on and HIGH turns it off. Jan 30 '16 at 21:26
  • 2
    @JakeCharman You shouldn't use inappropriate constants. The setup function sets the mode of a pin/GPIO to be an INPUT (GPIO.IN) or an OUTPUT (GPIO.OUT). Using random constants like True, False, LOW, HIGH will have random effects.
    – joan
    Jan 30 '16 at 21:29
  • 1
    @JakeCharman I'm sorry to say you should ignore anyone who has taught you to use the GPIO in that way. See sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage
    – joan
    Jan 30 '16 at 22:42
  • 1
    That is for the output function. Are you using the output function? You have not mentioned that function in your question.
    – joan
    Jan 30 '16 at 22:56
  • 1
    @JakeCharman No problem. That just means the relay uses inverse logic, so write 0 switches it on and write 1 switches it off. It is not uncommon, it is just the choice of the relay board hardware designer. The same can go for switches, they can read 0 when on and 1 when off, again the choice is made by the hardware designer.
    – joan
    Jan 30 '16 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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