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
NXP Pico i.MX7D GPIO_39
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 by 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.3'
    }
    
  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 releases the button:

    public class DoorbellActivity extends Activity {
    
        /**
         * Driver for the doorbell button;
         */
        private Button mButton;
    
        /**
         * The GPIO pin to activate for button presses.
         */
        private final String BUTTON_GPIO_PIN = ...;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG, "Doorbell Activity created.");
    
            ...
    
            // Initialize the doorbell button driver
            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. You already initialized the button component when the parent Activity was created. Now close the connection when the Activity is stopped or destroyed.

public class DoorbellActivity extends Activity {
    ...

    private Button mButton;

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

        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.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

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.