samuel_john64
Posts: 17
Joined: Wed Mar 15, 2017 9:55 am

Unable to use GPIO

Sat May 13, 2017 6:13 am

I try to run this code to trigger my relay no changes on the GPIO. I have tried to make it active high using terminal to write command gpio write 7 0 it changes once than i try to change it back to high the gpio still stays low. What should i do to solve this issue?

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
      while True:
   GPIO.output(7, GPIO.LOW)
   print("0")
   time.sleep(1)
   GPIO.output(7, GPIO.HIGH)
   print("1")
   time.sleep(1)


finally:
   # Reset the GPIO Pins to a safe state
   GPIO.output (7, GPIO.LOW)
   GPIO.cleanup()

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Unable to use GPIO

Sat May 13, 2017 6:23 am

Try fixing the indentation.

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
     while True:
        GPIO.output(7, GPIO.LOW)
        print("0")
        time.sleep(1)
        GPIO.output(7, GPIO.HIGH)
        print("1")
        time.sleep(1)

except:
   # Reset the GPIO Pins to a safe state
   GPIO.output (7, GPIO.LOW)
   GPIO.cleanup()
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

samuel_john64
Posts: 17
Joined: Wed Mar 15, 2017 9:55 am

Re: Unable to use GPIO

Sat May 13, 2017 6:39 am

I have tried but no change happen. in command it changes when mode into either in or our (gpio mode 0 IN or gpio mode 0 IN) but the output doesn't change when write command write "write 0 0" or "write 0 1" on the result.
DougieLawson wrote:Try fixing the indentation.

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
     while True:
        GPIO.output(7, GPIO.LOW)
        print("0")
        time.sleep(1)
        GPIO.output(7, GPIO.HIGH)
        print("1")
        time.sleep(1)

except:
   # Reset the GPIO Pins to a safe state
   GPIO.output (7, GPIO.LOW)
   GPIO.cleanup()

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Unable to use GPIO

Sat May 13, 2017 6:43 am

Have you connected to the right pin? https://pinout.xyz/pinout/pin7_gpio4
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

samuel_john64
Posts: 17
Joined: Wed Mar 15, 2017 9:55 am

Re: Unable to use GPIO

Sat May 13, 2017 6:47 am

yap its the right pin because the changed happen in change the mode into input or output for the pin.
DougieLawson wrote:Have you connected to the right pin? https://pinout.xyz/pinout/pin7_gpio4

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Unable to use GPIO

Sat May 13, 2017 2:48 pm

samuel_john64 wrote:I try to run this code to trigger my relay no changes on the GPIO. I have tried to make it active high using terminal to write command gpio write 7 0 it changes once than i try to change it back to high the gpio still stays low. What should i do to solve this issue?

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
      while True:
   GPIO.output(7, GPIO.LOW)
   print("0")
   time.sleep(1)
   GPIO.output(7, GPIO.HIGH)
   print("1")
   time.sleep(1)


finally:
   # Reset the GPIO Pins to a safe state
   GPIO.output (7, GPIO.LOW)
   GPIO.cleanup()
Hi.

first off are you sure its not your relay that's not changing state because its a 5v device?
to test your code/pin replace the relay with an LED and 330ohm resistor and see if that turns on and off.
if LED option works ok post details of the relay board you are trying to use.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”