Why does pressing one button trigger handler for both
Asked Answered
K

3

6

I am running Android Things 0.4 on a Raspberry Pi. I was following this tutorial to the letter:

https://developer.android.com/things/training/first-device/peripherals.html

Once I go the first button working, I decided I wanted to add a second button prior to continuing to the led portion of the tutorial. I know the hardware set up was correct for the first button, so I duplicated it for the second, but for a reason I cannot understand the buttons do not behave as expected. The first button triggers the event listener for both buttons. The second button will on trigger one direction and will not trigger again until the first button is pressed after the second button is pressed.

I am an experienced Android developer but very new to IoT and Things. Here is my code:

public class MainActivity extends Activity {
private static final String TAG = "ButtonActivity";
private static final String INC_BUTTON_PIN_NAME = "BCM4"; // GPIO port wired to the button
private static final String DEC_BUTTON_PIN_NAME = "BCM17"; // GPIO port wired to the button

private Gpio mIncButtonGpio;
private Gpio mDecButtonGpio;

Handler mHandler = new Handler(Looper.getMainLooper());

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PeripheralManagerService service = new PeripheralManagerService();
    try {
        // Step 1. Create GPIO connection.
        mIncButtonGpio = service.openGpio(INC_BUTTON_PIN_NAME);
        mDecButtonGpio = service.openGpio(DEC_BUTTON_PIN_NAME);
        // Step 2. Configure as an input.
        mIncButtonGpio.setDirection(Gpio.DIRECTION_IN);
        mDecButtonGpio.setDirection(Gpio.DIRECTION_IN);
        // Step 3. Enable edge trigger events.
        mIncButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
        mDecButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
        // Step 4. Register an event callback.
        mIncButtonGpio.registerGpioCallback(mIncCallback);
        mDecButtonGpio.registerGpioCallback(mDecCallback);
    } catch (IOException e) {
        Log.e(TAG, "Error on PeripheralIO API", e);
    }
}

// Step 4. Register an event callback.
private GpioCallback mIncCallback = new GpioCallback() {
    @Override
    public boolean onGpioEdge(Gpio gpio) {
        Log.i(TAG, "GPIO changed, INC button pressed");

        // Step 5. Return true to keep callback active.
        return true;
    }
};

private GpioCallback mDecCallback = new GpioCallback() {
    @Override
    public boolean onGpioEdge(Gpio gpio) {
        Log.i(TAG, "GPIO changed, DEC button pressed");

        // Step 5. Return true to keep callback active.
        return true;
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();

    // Step 6. Close the resource
    if (mIncButtonGpio != null) {
        mIncButtonGpio.unregisterGpioCallback(mIncCallback);
        try {
            mIncButtonGpio.close();
        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }
    }
    if (mDecButtonGpio != null) {
        mDecButtonGpio.unregisterGpioCallback(mDecCallback);
        try {
            mDecButtonGpio.close();
        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }
    }
}
}

Hardware Config Here is my logcat after pressing the first button 1 time:

06-09 14:33:21.717 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:33:21.718 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed

Here it is after pressing the second button right after the first:

06-09 14:33:21.717 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:33:21.718 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:33:58.047 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed

Here is what it looks like if I press the first button, then press the second 4 times, then press the first again:

06-09 14:39:06.804 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:39:06.804 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:39:08.846 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:39:11.377 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:39:11.377 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:39:11.510 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:39:11.510 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed

Like I said I am very new to IoT and things, but I just want to have two separate buttons that consistently trigger separate handlers.

Thanks.

Kilkenny answered 9/6, 2017 at 14:45 Comment(9)
Nothing wrong with the code as far as I can see, check the wiring again github.com/androidthings/sample-button/raw/master/…Campbellbannerman
What size are those resistors? One looks like 10k possibly? I can't make out the colors of the other one.Pelmas
One is 10k and one is 1k initially they were both 10k. I switched the resistor on the second button to 1k to see if it made any difference. It did not.Kilkenny
The only difference between the way my button is wired and this drawing is that I used an additional jumper with from the power source to the row with the resistor for the button. I think there is no differenceKilkenny
Both the code and the wiring looks good to me. Maybe try using a Button Driver, or even a ButtonInputDriver. They are slightly more elaborated that just detecting edges (i.e. debouncing)Smasher
I tried used the button driver from: github.com/androidthings/contrib-drivers/tree/master/button and got the same result.Kilkenny
That doesn't look like a 10K or 1K resistor to me. I see brown, red, black, 12 ohm. Enough to draw too much current from the power supply and cause both inputs to change.Cabbageworm
@Hans Passant I have replaced both resistors with 2k. No change in the results.Kilkenny
hobby-hour.com/electronics/resistorcalculator.phpCabbageworm
U
1

You need to put some diode on your wires going to the ground to prevent the signal from traveling backward. When you press one of the button, the ground short the other button.

See this diagram

When you press your top button, the current is going to the ground line (red wire) down to your white wire of the down button. From the white it goes through your resistor, then through your orange wire back to your gpio 7.

Underbelly answered 13/6, 2017 at 18:46 Comment(1)
I have replaced the top ground line with an LED. When I do that, the button no longer works. clicking it gives no results at all.Kilkenny
C
0

This probably happens because RPi input receiving noise highs/lows which the code picking up. Seems You need debounce circuit (at least RC like this from Official Documentation and tutorial). Or try to connect buttons to separate DC sources (one for 3.3V other for 5V). Also take look at this discussion. May be You need adjust pull-up resistors values. And try schematics with pull-down (not pull-up) like here.

Cambridge answered 19/6, 2017 at 14:59 Comment(3)
You link here was helpful for part of the problem. I copied the wiring in the picture from the sample circuit with the 1K and 10k resistors. That made the first button stop calling both listeners in the code. I repeated these steps for the second button, but it doesn't work at all. It does not fire it's own code for the listener.Kilkenny
Wires? Did it all connected good? Try with only second button. And try change GPIOs: BCM 22 & BCM 27 instead of BCM 4 (there is remark in Release Notes I/O: GPIO pins BCM4, BCM5, and BCM6 are internally pulled up to 3.3V when used as inputs).Cambridge
The wires are good. I have read up on the different GPIO pins and have tried many different combinations. I have not tried the bottom button only. I will try that next.Kilkenny
M
0

Try enable pull-up for each pin or adding physical pull-up resistors. Say 1K ohm resistor attached between the pin and the 3.3V supply.

Mannie answered 11/10, 2017 at 10:40 Comment(1)
Rather sounds like a shot-in-the-dark comment - not an answer.Lacylad

© 2022 - 2024 — McMap. All rights reserved.