saltydog
Posts: 39
Joined: Mon Dec 24, 2012 10:40 am

gpiozero input/output

Thu Jun 15, 2017 12:35 am

Is there a way to change the same pin from an output to an input.

Code: Select all

a = LED(23)
b = Button(23)
a.on()

sleep(0.01)

count  = 0

while b.is_pressed == True:
    count = count + 1
return count
The above code returns the error: pin GPIO23 already in use by another gpiozero object.

Cheers

Chris

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: gpiozero input/output

Thu Jun 15, 2017 6:47 am

Maybe I am being a bit slow, but how are you wiring one pin to both the button and LED? I don't follow how it does both.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

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

Re: gpiozero input/output

Thu Jun 15, 2017 7:03 am

Hi.

If you wanted to use a Pin for both in and out you would need to first define as output then turn it on wait for some period, then redefine it as input and check for input.

but connecting both input and output device ( LED and switch) to the same Pin at the same time would not only be unlikely to work but might result in damage to your Pi.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
bensimmo
Posts: 4175
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: gpiozero input/output

Thu Jun 15, 2017 8:33 am

I doubt it, just use another pin for the switch.
When you press the switch it will short the LED
GPIO are I believe Input OR Output, not Input AND Output, so you would need to do as above and loop through switching between states?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: gpiozero input/output

Thu Jun 15, 2017 11:26 am

It's worth adding that, from a python perspective, you can use the "input" method on a pin that is set up as an output (i.e. to read its current state).

However, I'm not remotely qualified to talk about wiring implications and potential damage to your pi so I'd listen to the other comments!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

saltydog
Posts: 39
Joined: Mon Dec 24, 2012 10:40 am

Re: gpiozero input/output

Thu Jun 15, 2017 2:05 pm

Thanks for the replies
The code is for a 'line following sensor' that only has one control line. You need to charge a cap (output) and then read it's discharge time (input)
pcmanbob wrote:Hi.
If you wanted to use a Pin for both in and out you would need to first define as output then turn it on wait for some period, then redefine it as input and check for input.
Yes, that works fine with other libraries like RPi.GPIO and pigpio. But it would have been preferable to use a simple library like gpiozero. It's intended for kids to solve from a python library that was already been used.
My initial thought was that gpiozero would change the state of the pins when a different function was called. Not to be.

Cheers

Chris

User avatar
bensimmo
Posts: 4175
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: gpiozero input/output

Thu Jun 15, 2017 2:30 pm

You could ask the question on github for gpiozero and see what they know (though they will probably read it here too)

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

Re: gpiozero input/output

Thu Jun 15, 2017 2:34 pm

bensimmo wrote:You could ask the question on github for gpiozero and see what they know (though they will probably read it here too)
They will just give the same answer you cant use the same pin for input and output at the same time. even if you put a software fix in place, having both input and output circuitry connected to the same pin at the same time will at best cause errors and worst damage the GPIO.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
bensimmo
Posts: 4175
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: gpiozero input/output

Thu Jun 15, 2017 3:03 pm

pcmanbob wrote:
bensimmo wrote:You could ask the question on github for gpiozero and see what they know (though they will probably read it here too)
They will just give the same answer you cant use the same pin for input and output at the same time. even if you put a software fix in place, having both input and output circuitry connected to the same pin at the same time will at best cause errors and worst damage the GPIO.
He's saying he can do something in pigpio/RPi but not in gpiozero (which can also make use of pigpio/RPi).
[post above mine]

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: gpiozero input/output

Thu Jun 15, 2017 4:00 pm

If you want to read the state of a device set as an output in gpiozero, can you use the "value" property?

By my reading of the source, this should return the state of the pin.

If I've misunderstood your needs, tell me I'm being stupid and I'll leave you to the pros!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”