read state of an output gpio pin

Started by mauricio, September 21, 2014, 08:01:50 PM

Previous topic - Next topic

mauricio

on an A20 I've defined an output GPIO PIN and want to know if it is high or low but can't figure out

I've set python code similar to the A13 described here https://www.olimex.com/forum/index.php?topic=750.msg4868#msg4868


import A20_GPIO as GPIO

GPIO.init()

GPIO.setcfg(GPIO.PIN_PH23, GPIO.OUTPUT)
GPIO.output(GPIO.PIN_PH23, GPIO.HIGH)

GPIO.setcfg(GPIO.PIN_PH23, GPIO.INPUT)
state = GPIO.input(GPIO.PIN_PH23)

return state


but state variable is allways LOW (0), it seems that setting the PIN to INPUT set it to LOW by default

is there a way to get this ?

I'm doing it the wrong way ?

please any advice is welcome, thanks

micgm

It works for me.
I am using pyA20-0.2.0


from pyA20.gpio import gpio
from pyA20.gpio import port
from pyA20.gpio import connector

button = port.PH27

#initialize module
gpio.init()

gpio.setcfg(button, gpio.INPUT)
gpio.pullup(button, gpio.PULLUP)

state = gpio.input(button)      # Read button state
print state


When idle it returns "1"
When connecting pin2(GND) to pin40(PH27) then it returns "0"

JohnS

Maybe the actual hardware pin does not keep its state when changed from output to input.  (Lots of hardware behaves that way.)

The datasheet may say.

John

jmyreen

If the pin direction is set to input, the value read reflects the external state of the pin. If external circuitry is electrically driving the pin low, you get a zero when reading it. The value is not in any way related to what was previously output to it.

jurij

Be careful ! GPIO may be in  pullup, pulldown or float state. Initial (defined in fex), the most of them are in input float state.
It's mean, if you don't connect physical input these is undefined state 1 or 0, and my change randomly any moment (verified, will change). In real word, if you connect button or switch on float input, when button/switch is off input will change state uncontrolled.
Solution is put GPIO in pulldown (or pullup) input state at initialization time by fex file (script.bin).
Please look at Fex Guide - linux-sunxi.org.html section Port definitions.
Example:
Default input definition:  gpio_pin_21 = port:PH07<0><default><default><default>
Pull down input definition: gpio_pin_22 = port:PH09<0><2><default><default>

mauricio

thank you all

It seems that I need to read that Fex Guide

jmyreen what you say makes sense. I'm actuating over a relay, which I think doesn't send back any signal

micgm I'll try what you suggest, maybe I need to update the library

JohnS I'll review the datasheet also

actually the case is this is a script receiving pin number and desired state, put the desired state at that pin and leaves

I'm just trying to know which the actual state of the pin is before to actuate over it, so the script can respond "nothing done, state already selected" or "state updated, changed from On to Off". Different from the code I posted first, that code was just for testing