My Android Things

Android Things Tutorials (IoT)

Tutorial 7 : Your first blinking LED with Android Things

Now that we know how to get started with Android Things, it’s time for us to blink our first LED.

An LED is just like a tiny light-bulb with two legs (leads). When those leads are powered correctly the LED emits lights depending on its color. Let’s look at how to control an LED from an Android application.

Our tools

  • Android Studio
  • A Raspberry Pi 3 with Android Things
  • LEDs 
  • Jumper wires
  • Breadboard
  • A 220O Ohm resistor (Red, Red, Brown, Gold)

Connecting Hardware

Lets start off by connecting the LED and resistor to the breadboard. As a rule of thumb, remember that the long leg on the LED is always the anode (positive).

The resistor can go either side and either way of the LED. Connect the BCM6 pin on the Raspberry Pi to the other and of the resistor, and one of the 0v (Ground) ports on the Raspberry Pi to the other end of the LED, as shown below:



My Setup:


Now, we are done with hardware connection. We will now move forward to program it.


Programming it

Now, as discussed in Tutorial 4, create a new project as "MAT7" with "Android Things" template. There is no need to select UI layout file while creating project. 

Open up MainActivity.java and add the following static and member variables at the top of the class and resolve the dependencies to Handler and Gpio:

private static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;private static final String LED = "BCM6";private Handler mHandler = new Handler();private Gpio mLedGpio;


Change the onCreate method so it opens up a new connection to our LED pin and continuously posts a runnable to change the state of that pin.

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);PeripheralManagerService service = new PeripheralManagerService();try {mLedGpio = service.openGpio(LED);mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);Log.i(TAG, "Start blinking LED GPIO pin");mHandler.post(mBlinkRunnable);} catch (IOException e) {Log.e(TAG, "Error on PeripheralIO API", e);}}


Create the runnable inside the class. This will toggle the state of our LED pin.

private Runnable mBlinkRunnable = new Runnable() {@Overridepublic void run() {if (mLedGpio == null) {
            return;
        }
        try {
            // Toggle the GPIO state
            mLedGpio.setValue(!mLedGpio.getValue());
            Log.d(TAG, "State set to " + mLedGpio.getValue());
            mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }
    }
};


The setValue method on mLedGpio takes a boolean, so by passing its current value with the exclamation mark in front of, we always switch it to the opposite (Not Operator).

We then use INTERVAL_BETWEEN_BLINKS_MS to control the frequency in which we blink our LED. You can change that value to have an LED that blinks faster or slower.

Lastly, let’s make sure we clean after ourselves by closing the GPIO port we opened in onCreate by overriding the onDestroy method.

@Override
protected void onDestroy() {
    super.onDestroy();
    mHandler.removeCallbacks(mBlinkRunnable);
    Log.i(TAG, "Closing LED GPIO pin");
    try {
        mLedGpio.close();
    } catch (IOException e) {
        Log.e(TAG, "Error on PeripheralIO API", e);
    } finally {
        mLedGpio = null;
    }
}

Party time

Run the app on your Raspberry Pi by pressing run button on your Android Studio, and you should see that as soon as it’s installed the LED will start flashing.


Final Run

Download Project Source


Shutting Down
Now, we can safely uninstall our program from raspberry pi with following command (otherwise, it will run everytime when we will restart our raspberry pi; and we will discuss the reason for it in later tutorials)
adb uninstall com.myandroidthings.mat7

Now it's time to safely turn off our raspberry pi with following adb command:
adb shell reboot -p

Now we will meet again in Tutorial 8. Bye for now.

Add comment

Loading