Skip to content

Most visited

Recently visited

navigation

Implement a Doorbell Button

This lesson teaches you to

  1. Connect hardware to GPIO
  2. Initialize a peripheral driver
  3. Manage peripheral connections

Try it out

A doorbell isn't much to speak of without a button for visitors to press and announce their presence at the door.

In this lesson, you will learn to connect a pushbutton to a GPIO input, initialize it using Peripheral I/O, and listen for state changes on that input.

Connect the button

Using a breadboard, connect a pushbutton switch to the appropriate GPIO port on your board. To connect the button to your board:

  1. Connect one side of the button to the chosen GPIO input pin, and the other side to ground.
  2. Connect the same GPIO input pin to +3.3V through a pull-up resistor.

For this lesson, the following GPIO pins are assumed on each board:

Board Input Signal
Raspberry Pi BCM21

""

Capture button press events

The Button peripheral driver handles the low-level logic of listening for GPIO state changes. It also debounces the input port be default, to avoid multiple trigger events from a single button press. To add the button driver to your app:

  1. Add a button peripheral to your app project by connecting it to an available GPIO port.
  2. Add the button driver dependency to your app-level build.gradle file:

    dependencies {
        ...
    
        compile 'com.google.android.things.contrib:driver-button:0.1'
    }
    
  3. Initialize a Button with the connected GPIO port name, and the appropriate logic state indicating when the button is physically pressed.

  4. Attach an OnButtonEventListener to receive a callback when the user presses or released the button:

    public class DoorbellActivity extends Activity {
    
        /**
         * The GPIO pin to activate for button presses.
         */
        private final String BUTTON_GPIO_PIN = ...;
    
        /**
         * Driver for the doorbell button;
         */
        private Button mButton;
    
        /**
         * Initializes button driver, which will report physical button presses.
         */
        private void initializeDoorbellButton() {
            try {
                mButton = new Button(BUTTON_GPIO_PIN,
                        Button.LogicState.PRESSED_WHEN_LOW);
                mButton.setOnButtonEventListener(mButtonCallback);
            } catch (IOException e) {
                Log.e(TAG, "button driver error", e);
            }
        }
    
        /**
         * Callback for button events.
         */
        private Button.OnButtonEventListener mButtonCallback =
                new Button.OnButtonEventListener() {
            @Override
            public void onButtonEvent(Button button, boolean pressed) {
                if (pressed) {
                    // Doorbell rang!
                    Log.d(TAG, "button pressed");
                }
            }
        };
    }
    

Manage the connection

You should manage the peripheral connection according to the application component lifecycle. Initialize the button component when the parent Activity is created or started, and close the connection when the Activity is stopped or destroyed.

public class DoorbellActivity extends Activity {
    ...

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...

        initializeDoorbellButton();
    }

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

        if (mButton != null) {
            try {
                mButton.close();
            } catch (IOException e) {
                Log.e(TAG, "button driver error", e);
            }
        }
    }
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.