0

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)
time.sleep(10)
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

GPH=GPIO.LOW
GPL=GPIO.HIGH
GPIO.digitalWrite(18, GPH)
time.sleep(10)
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 '16 at 23:46
  • GPH=GPIO.HIGH gives this error : NameError: name 'GPIO' is not defined (I've correct this in the question now) – Richard May 2 '16 at 0:03
  • I assume you remembered the import RPi.GPIO as GPIO line? – dpdt May 2 '16 at 0:18
  • yes, using webiopi -> GPIO = webiopi.GPIO – Richard May 2 '16 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 '16 at 5:44
2

GPIO.HIGH is a True or 1

GPIO.LOW is False or 0

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

GPH=False
GPL=True
GPIO.digitalWrite(18, GPH)
time.sleep(10)
GPIO.digitalWrite(18, GPL)

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.