I have a python program what control relay board via GPIO. When I set a GPIO port LOW the relay closes and HIGH opens the relay As example. I use this line to close the relay and open it again after 10 seconds

GPIO = webiopi.GPIO

GPIO.digitalWrite(18, GPIO.LOW)
GPIO.digitalWrite(18, GPIO.HIGH)

Now I've bought a new relay board, which works to opposite. On a GPIO.LOW it opens and a GPIO.HIGH the relais close

I thought to use this code, which is not working

GPIO.digitalWrite(18, GPH)
GPIO.digitalWrite(18, GPL)

GPH=GPIO.LOW NameError: name 'GPIO' is not defined

My question is: what is the proper way to put GPIO.HIGH and GPIO.LOW in a variable? so I can easy switch those.

  • what do you mean "it's not working"? And what's the point of the sleep line, you are not changing the state of the relay.
    – JayEye
    May 1, 2016 at 23:46
  • GPH=GPIO.HIGH gives this error : NameError: name 'GPIO' is not defined (I've correct this in the question now) May 2, 2016 at 0:03
  • I assume you remembered the import RPi.GPIO as GPIO line?
    – dpdt
    May 2, 2016 at 0:18
  • yes, using webiopi -> GPIO = webiopi.GPIO May 2, 2016 at 0:28
  • It's not working because GPIO does not exist. You need to have GPIO=webopi.GPIO prior to using GPH=GPIO.LOW.
    – joan
    May 2, 2016 at 5:44

1 Answer 1


GPIO.HIGH is a True or 1

GPIO.LOW is False or 0

So to solve this issue the following changes must be made:

GPIO.digitalWrite(18, GPH)
GPIO.digitalWrite(18, GPL)

Your Answer

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

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