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.

maddesa
  • 1,014
  • 7
  • 18
  • Nothing wrong with the code as far as I can see, check the wiring again https://github.com/androidthings/sample-button/raw/master/rpi3_schematics.png – Blundell Jun 09 '17 at 20:40
  • What size are those resistors? One looks like 10k possibly? I can't make out the colors of the other one. – devunwired Jun 10 '17 at 04:24
  • 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. – maddesa Jun 10 '17 at 04:29
  • 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 difference – maddesa Jun 10 '17 at 04:35
  • 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) – shalafi Jun 10 '17 at 07:22
  • I tried used the button driver from: https://github.com/androidthings/contrib-drivers/tree/master/button and got the same result. – maddesa Jun 10 '17 at 21:31
  • 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. – Hans Passant Jun 13 '17 at 18:37
  • @Hans Passant I have replaced both resistors with 2k. No change in the results. – maddesa Jun 16 '17 at 13:44
  • http://www.hobby-hour.com/electronics/resistorcalculator.php – Hans Passant Jun 16 '17 at 13:56

3 Answers3

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.

Distwo
  • 11,569
  • 8
  • 42
  • 65
  • 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. – maddesa Jun 16 '17 at 13:27
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.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • You link [here](https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/) 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. – maddesa Jun 19 '17 at 20:12
  • 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](https://developer.android.com/things/preview/releases.html#developer_preview_4) I/O: GPIO pins `BCM4`, `BCM5`, and `BCM6` are internally pulled up to 3.3V when used as inputs). – Andrii Omelchenko Jun 20 '17 at 07:38
  • 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. – maddesa Jun 20 '17 at 18:11
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.

Humbledream
  • 51
  • 1
  • 3