1

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

GPIO.cleanup()

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 – Jake Charman Jan 30 '16 at 22:38
2

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. – Jake Charman Jan 30 '16 at 21:26
  • 1
    @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
  • What would the correct syntax be then? I've always been taught to use GPIO in this way. – Jake Charman Jan 30 '16 at 22:33
  • @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
  • Forgive me if I'm missing something but does this not state the same method I'm using? "State can be 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True." – Jake Charman Jan 30 '16 at 22:47

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.