jcdocka
Posts: 1
Joined: Sun Feb 02, 2020 3:39 am

Python code not able to turn on GPIO

Sun Feb 02, 2020 3:46 am

Hello all,

I can turn my PI zero w GPIO pins on and off by using "gpio write 17 1", (using it to control a transistor that controls a relay, works perfectly fine), in my code though when I set it up by:

GPIO.setmode(GPIO.BOARD)
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, GPIO.HIGH)
print(GPIO.input(21))
GPIO.output(21, 0)
print(GPIO.input(21))
GPIO.output(21, 1)
print(GPIO.input(21))

and i get the terminal output as:
1
0
1

But the gpio pin never picks the relay/never turns on.

Any ideas?

User avatar
rpiMike
Posts: 1218
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Python code not able to turn on GPIO

Sun Feb 02, 2020 7:21 am

Add time.sleep(5) after each output statement.

User avatar
jojopi
Posts: 3194
Joined: Tue Oct 11, 2011 8:38 pm

Re: Python code not able to turn on GPIO

Sun Feb 02, 2020 8:22 am

jcdocka wrote:
Sun Feb 02, 2020 3:46 am
I can turn my PI zero w GPIO pins on and off by using "gpio write 17 1",
Are you sure? The "gpio write" that I am familiar with is part of WiringPi, and it defaults to WiringPi numbering, unless you pass the -g or -1 options. But 17 in WiringPi numbering was on the extra P5 header, present only on some 26-pin Pies; it does not exist on the Zero.

Anyway, there is no combination of numberings where 17 would correspond to 21. Check that you really mean GPIO.BOARD, and pin 21.

ElEscalador
Posts: 817
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Python code not able to turn on GPIO

Sun Feb 02, 2020 2:28 pm

Without a delay (time.sleep(int)) it goes on then off so fast you can't see the light and the relay doesn't have enough time to physically move.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

Return to “Python”