简体   繁体   中英

Android Things - Gpio.getValue() always returning true

I have trying to set and get gpio value on Android Things, I am using raspberry pi 3 and have my connections at BCM26(output), BCM16(input).

I have also tried changing the pins and checked them using DMM as well, no matter what I do I am unable to set the output high. and even getValue gpio also return false.

mMotorGpio = service.openGpio(MOTOR_PIN_NAME);
            mMotorGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

            Log.i(TAG, "Output GPIO set");

. . .

 try {
                boolean newVal = !mMotorGpio.getValue();

                Log.i(TAG,"setting port value as " + newVal);

                mMotorGpio.setValue(newVal);

            }catch (IOException e){
                e.printStackTrace();
            }

Seems like you are trying to read from the pin,which is configured for output:

mMotorGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

and also you didn't configured voltage signal to be returned as true (active), for example:

mMotorGpio.setActiveType(Gpio.ACTIVE_HIGH);

if you want high voltage as active as described in Official Documentation (section Reading from an input).

So,you will need 2 separate Gpio objects (one for Input, other for Output) to do, what you want. Something like this:

private static final String MOTOR_PIN_OUT_NAME = "BCM26";
private static final String MOTOR_PIN_IN_NAME = "BCM16";

private Gpio mMotorGpioIn;
private Gpio mMotorGpioOut;

...

mMotorGpioIn = service.openGpio(MOTOR_PIN_IN_NAME);
mMotorGpioIn.setDirection(Gpio.DIRECTION_IN);
mMotorGpioIn.setActiveType(Gpio.ACTIVE_HIGH);

mMotorGpioOut = service.openGpio(MOTOR_PIN_OUT_NAME);
mMotorGpioOut.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mMotorGpioOut.setActiveType(Gpio.ACTIVE_HIGH);

...

try {
    boolean newVal = !mMotorGpioIn.getValue();

    Log.i(TAG,"setting port value as " + newVal);

    mMotorGpioOut.setValue(newVal);

} catch (IOException e){
    e.printStackTrace();
}

You cannot reliably read the state value of a pin configured as an output. From the GPIO reference docs for getValue() :

Get the current value of the GPIO pin (for GPIO pins configured as input only). Returns an unpredictable value when the GPIO is configured as output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM