Your first blinking LED with Android Things using NXP Pico i.MX7D

Android Things

Hello friends, In this article you will learn how to blink an LED with Android Things using NXP Pico i.MX7D.

An LED is a tiny light-bulb with two legs. When we connect those legs with power supply, the LED starts emitting lights depending on its colour. Let's learn how to control an LED from an Android application.

1. Prerequisite: 

No prerequisite is required, just basic programming skills will be needed for this tutorial.

2. Hardware Requirements:

1. Breadboard

2. Connecting Wires

3. One LED

4. Android Things Starter Kit nxp pico series

We added two wires with the LED and nxp board GPIO. We added ground number 6 with the led and GPIO6_IO12 as VCC. 

Note: As we can see, the GPIO6 series is giving high voltage, other gpio is giving low voltage. I don't know the reason, but mentioned here because of your doubt.

We have added the positive part of the LED with gpio and negative part of the led with the ground using the connecting wires.

3. Creating the App for the Android Things: 

3.1 Creating the project

To create a new project please install Android Studio from here : Android Studio, then follow the following steps -

From our project files, we required these three files. 

In our AndroidManifest.xml File, we have to add this permission and the library in between <application> tag

<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
<application>
        <uses-library android:name="com.google.android.things" />
        ...
</application>

Our AndroidManifest.xml file is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sodiarc.ledblinkerthings">
    <uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
    <application>
        <uses-library android:name="com.google.android.things" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.IOT_LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

We don't need to worry about the UI here. So I am just mentioning the MainActivity.java code

package com.sodiarc.blinkinganled;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManager;

import java.io.IOException;

public class MainActivity extends Activity {
    private Gpio mLedGpio;
    private static final String LED_PIN_NAME = "GPIO6_IO13";
    private boolean toggle = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PeripheralManager manager = PeripheralManager.getInstance();
        try {
            mLedGpio = manager.openGpio(LED_PIN_NAME);
            mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
            mLedGpio.setValue(toggle);
            blinkLed();

        } catch (IOException e) {
            Log.e("Error1", "Error on PeripheralIO API", e);
        }
    }

    private void blinkLed() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    changeState();
                } catch (Exception e){
                    Log.e("Error2", "Error on changing State", e);
                }
            }
        },500);
    }

    private void changeState() throws Exception {
        toggle = !mLedGpio.getValue();
        mLedGpio.setValue(toggle);
        blinkLed();
    }
}

 

Android Things Article Author

Author

Hi folks, I am co-founder and CEO at Sodiarc Technologies Pvt. Ltd. I am working on android for more than 5 years. I have skills, which includes Android, iOS, PHP and lot more. If you have any project related queries we can discuss it briefly, mail me at info@sodiarc.com

Android Things Article Author

Author

Hi folks, I am CTO at Sodiarc Technologies Pvt. Ltd. I am programming enthusiast. I have skills, which includes Android, iOS, PHP and lot more. If you have any project related queries we can discuss it briefly, mail me at info@sodiarc.com

Android Things Article Author

Author

Hi folks, I am a content writer and a blogger. Writing is my passion. A B.Tech graduate in Information Technology, who is still paving the way for achieving the goals of her life.


Website Security Test