Android Things: LED on/off using UI Button using NXP Pico i.MX7D

Android Things

Hello friends, In our previous article, we have learned How to build an blinking LED using Android Things NXP Pico i.MX7D.

Today, in this article we are going to learn How control an LED through an UI button using Android Things NXP Pico i.MX7D.

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 i.MX7D

We have added two wires with the LED and NXP Pico 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 the 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 AndroidStudio from here : Android Studio, then follow the following steps -

From our project files, we require 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>

Our activity_main.xml is here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="On"/>

</RelativeLayout>

Our MainActivity.java code is here

package com.sodiarc.ledblinkerthings;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManager;
import java.io.IOException;

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Gpio mLedGpio;
    private Button toggle;
    public static final String LED_PIN_NAME = "GPIO6_IO12";
    public static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toggle = findViewById(R.id.toggle);

        PeripheralManager manager = PeripheralManager.getInstance();
        Log.d(TAG, "Available GPIO: " + manager.getGpioList());

        try {
            mLedGpio = manager.openGpio(LED_PIN_NAME);
            mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }

        toggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mLedGpio.setValue(!mLedGpio.getValue());
                    if(!mLedGpio.getValue())
                        toggle.setText("On");
                    else
                        toggle.setText("Off");
                }catch (Exception e){
                    Log.e(TAG, "Error on PeripheralIO API", e);
                }
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mLedGpio != null) {
            try {
                mLedGpio.close();
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }
    }
}

 

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