Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPIO value always change

First time with android things, I tried to detect a button hit button GPIO but the value always change :

2018-07-09 15:53:00.123 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button true
2018-07-09 15:53:00.134 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button false
2018-07-09 15:53:00.143 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button true
2018-07-09 15:53:00.154 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button false
2018-07-09 15:53:00.163 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button true
2018-07-09 15:53:00.174 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button false
2018-07-09 15:53:00.183 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button true
2018-07-09 15:53:00.194 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button false
2018-07-09 15:53:00.203 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button true
2018-07-09 15:53:00.214 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button false
2018-07-09 15:53:00.223 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button true
2018-07-09 15:53:00.234 1539-1539/com.pocdetector I/HomeActivity: GPIO changed, button false

Here is my raspberry and breadboard breadboard

And my code

val TAG = "HomeActivity"
val BUTTON_PIN_NAME = "BCM21"

lateinit var mButtonGpio: Gpio

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    var pioManager = PeripheralManager.getInstance()
    Log.d(TAG, "Available GPIO : " + pioManager.gpioList)

    try{
        mButtonGpio = pioManager.openGpio(BUTTON_PIN_NAME)

        mButtonGpio.setDirection(Gpio.DIRECTION_IN)
        mButtonGpio.setEdgeTriggerType(Gpio.EDGE_BOTH)
        mButtonGpio.setActiveType(Gpio.ACTIVE_HIGH)

        mButtonGpio.registerGpioCallback(mCallback)
    }catch (e: IOException){
        Log.w(TAG, "Error opening GPIO", e)
    }
}

private val mCallback = GpioCallback { gpio ->
    try {
        Log.i(TAG, "GPIO changed, button " + gpio.value)
    } catch (e: IOException) {
        Log.w(TAG, "Error reading GPIO")
    }

    // Return true to keep callback active.
    true
}

I tried with an other button, with less or more resistor but with the same result everytime. Tried the Android things sample too but the button hit is never triggered.

like image 657
Alex R. Avatar asked May 20 '26 13:05

Alex R.


1 Answers

The wire you have connected to your GPIO on is currently floating, meaning it's not connected to any other signal. This is what generates the slew of events you see as the signal floats freely between high and low values. The reason our button driver doesn't trigger any events in this case is because we have built-in debounce protection to ignore events that happen in such a short time span.

You need to move your input wire to the other side of the switch. I drew a quick Fritzing diagram of the correct wire placement:

Correct Wiring

So why didn't your setup work? It's because internally those push button switch have to separate sets of contacts that aren't internally connected. See the following diagram.

Switch Internals

So the connections on one side of the breadboard aren't internally connected to the connections on the other.

like image 134
devunwired Avatar answered May 22 '26 02:05

devunwired



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!