Search code examples
raspberry-pi3gpioandroid-things

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();
            }

Solution

  • 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();
    }