tristate gpio

1,003 views
Skip to first unread message

boblei...@gmail.com

unread,
Oct 1, 2013, 2:00:38 AM10/1/13
to quick2wi...@googlegroups.com
I am trying to set up a GPIO pin on the raspberry pi using quick2 wire.  I need the pin to not assert high or low until needed in my code.  I have tried to do this by initially setting the pin as an input, the plan being to change it to an output when I need to assert the pin to work my hardware.

My problem is that I can't work out how to change the direction of the pin.   My code goes something like this:

from quick2wire.gpio import pins, In, Out
mypin = pins.pin(5, direction=In)   #Pin 5 set as input, this works fine and the pin works as an input ok

and later I want to switch it to an output with something like

mypin = pins.pin(5, direction=Out)

this causes a runtime error (IOError pin(5) is closed)

I have tried various other syntax without any luck.  Can anyone help me with this?

Bob

Andrew Scheller

unread,
Oct 1, 2013, 6:13:50 AM10/1/13
to quick2wi...@googlegroups.com
It's much simpler than that :) The pin's direction is just a
property of the object, i.e. you need to do:
mypin.direction = Out

Andrew
> --
> You received this message because you are subscribed to the Google Groups
> "Quick2Wire Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to quick2wire-use...@googlegroups.com.
> Visit this group at http://groups.google.com/group/quick2wire-users.
> For more options, visit https://groups.google.com/groups/opt_out.
Message has been deleted

boblei...@gmail.com

unread,
Oct 1, 2013, 5:36:23 PM10/1/13
to quick2wi...@googlegroups.com, raspb...@loowis.durge.org
Hi Andrew, thanks for replying

I tried using mypin.direction = Out but I get another runtime error. 

The traceback is:

Traceback (most recent call last):
  File "/home/pi/Desktop/SelectorKeypadTest.py", line 31, in <module>
    mypin.direction=Out
  File "/usr/bin/quick2wire/gpio.py", line 151, in direction
    self._write("direction", new_value)
  File "/usr/bin/quick2wire/gpio.py", line 192, in _write
    with open(self._pin_path(filename), "w+") as f:
IOError: [Errno 2] No such file or directory: '/sys/devices/virtual/gpio/gpio24/direction'

I'm a bit lost at this point.  My system does not have a gpio24 directory in the /sys/devices/virtual/gpio directory.  (The only directory in gpio is  called gpiochip0, and it does not contain a file called direction.)

Bob

Romilly Cocking

unread,
Oct 2, 2013, 1:25:59 AM10/2/13
to quick2wi...@googlegroups.com
The sysfs system (twhcih controls the files under /sys) is a bit wierd. Files and directories are created and destroyed as gpio pins are exported or released. If you can' see a directory called /sys/devices/virtual/gpio/gpio24/ all that means is that the pin is not currently exported. perhaps because your code has not yet opened it (or has opened it and then closed it).

It might be easier to see what's wrong if you could post the SelectorKeyPadTest code in its current form.

Romilly


--

boblei...@gmail.com

unread,
Oct 2, 2013, 6:17:35 AM10/2/13
to quick2wi...@googlegroups.com
Hi Romilly, thanks, I can see my problem now

I won't post the whole code because it is massive (and embarrassingly messy, a masterpeice in progress).  In a nutshell though I tried to change the direction of the pin before opening it like you said, In essence my code boiled down to:

from quick2wire.gpio import pins, In, Out
mypin = pins.pin(5, direction=In)
mypin.direction=Out      --> causes runtime error, but works fine if I move this line to after the 'with mypin:' statement
with mypin:
    mypin.value = 0
    print('mypin set to out, value 0')
    time.sleep(20)
    Print('exiting')

Thanks for your help, I appreciate it.  

Bob

PS I've had fun monitoring in a terminal when the gpio24 folder appears and disappears from /sys when my code runs - never realised this happened. 

Romilly Cocking

unread,
Oct 2, 2013, 11:38:25 AM10/2/13
to quick2wi...@googlegroups.com
I'm glad the problem is solved.


best regards,  Romilly


Chen Jason

unread,
Jul 2, 2014, 10:13:19 PM7/2/14
to quick2wi...@googlegroups.com
Hi,
   Can Raspberry's GPIO pin really be tristated ?  I tried to put the direction as 'in', but that does not work.  There's
still voltage on the pin.

boblei...@gmail.com於 2013年10月2日星期三UTC-7上午3時17分35秒寫道:

CONFIDENTIALITY NOTICE : Please be advised that this e-mail and any files transmitted therewith are privileged or confidential and are intended solely for the individual or entity to whom they are addressed. If you are not the intended recipient, please do not read, copy or retransmit this communication but destroy it immediately. Any unauthorized dissemination, distribution or copying of this communication is strictly prohibited.

Jeffrey Herr

unread,
Jul 2, 2014, 10:46:09 PM7/2/14
to quick2wi...@googlegroups.com
Until it is connected to ground, it might look that way...  Connect with a pull-up or down resistor and it should work as expected. 
--
You received this message because you are subscribed to the Google Groups "Quick2Wire Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quick2wire-use...@googlegroups.com.
Visit this group at http://groups.google.com/group/quick2wire-users.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages