Android Things: Hello Rainbow HAT

Android Things: Hello Rainbow HAT

Blinking an LED using the Rainbow HAT with Android Things.

Setting out to build IoT devices is exciting, but If you’re like me and haven’t worked much with hardware, electronics or peripheral I/O, then it can seem a little intimidating.

If that sounds like you, then it might be time to say “Hello Rainbow HAT”

The Rainbow HAT is an add-on board that provides access to a bevy of peripherals with simple installation.

It’s a perfect tool to get started with peripheral I/O and the Android Things platform.

. . .

 

Installing the Rainbow HAT

The first thing we need to do is assemble our hardware and install Android Things

What will you need?

  • supported development board
  • power cable
  • Rainbow HAT
  • standoffs & screws
  • screwdriver

What I did

I had a little too much hardware setup after my previous dive into Android Things.

To simplify things for this project, I removed the screen, camera, and wifi antenna to make installing the Rainbow HAT easier.

. . .

This left me with just the i.mx7d board, and nothing attached to it.

I then added 4 standoffs to the board

  • (make sure to double check the length first 🙂)
  • the standoffs should be long enough that the HAT doesn’t touch other parts of the board, but short enough to allow a good connection

Attach the Rainbow HAT by connecting the 40-pin connectors on both the HAT & board

“Note that the HAT should not come in contact with the development board, especially the small, square system on a module (SoM) board attached to the main board”

Finally, attach the power cable from your development board to a power source and… presto!

Nate Ebel on Twitter

First time powering up the #RainbowHAT For #AndroidThings 🙂

Installing Android Things

Now that the hardware is ready, it’s time to install Android Things.

I outline the Android Things install process in a previous post based on the the instructions found here.

For more detailed instructions/video on setting up your hardware and OS, see these posts

Android Things: Assembling Your Maker Kit

Android Things: Installing the OS

Setting Up a New Peripherals Project

Our hardware & OS are now ready to go, so we can finally setup our project.

  • create project
  • choose Android Things as only target
  • choose a peripheral Activity (no UI)
  • don’t choose to add any peripheral support

This slideshow requires JavaScript.

Great, now we have an empty project. So.... how do we turn on a light? Click To Tweet

. . .

Let’s Blink a Light

To help interact with the Rainbow HAT, we’ll rely on open source peripheral drivers.

In particular, we’ll use the driver for the Rainbow HAT

androidthings/contrib-drivers

Open source peripheral drivers. Contribute to androidthings/contrib-drivers development by creating an account on GitHub.

Next, we’ll walk through the steps to integrate the Rainbow HAT driver to blink the red LED

. . .

Add the dependency

dependencies {
    compile 'com.google.android.things.contrib:driver-rainbowhat:0.9'
}

Open a connection to an LED

Import the driver

import com.google.android.things.contrib.driver.rainbowhat.RainbowHat

Connect to the red LED

val redLED:Gpio = RainbowHat.openLedRed()

Alternate LED Values

We create a Runnable that will alternate the value of the LED every time it runs.  In onCreate() we will start posting the Runnable.

private val blinkRunnable = object : Runnable {
    override fun run() {
        redLED?.also {
            it.value = !it.value  // Update the GPIO state

            // reschedule the update
            blinkHandler.postDelayed(this, BLINK_DURATION_MILLIS)
        }
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    redLED = RainbowHat.openLedRed()

    blinkHandler.postDelayed(blinkRunnable, BLINK_DURATION_MILLIS)
}

Close the Connection

We want to be sure to close the connection to the LED when the Activity is destroyed

override fun onDestroy() {
    super.onDestroy()

    // Remove pending blink Runnable from the handler.
    blinkHandler.removeCallbacks(blinkRunnable)

    redLED?.close()
}

And that’s it!

We’ve now build our first simple application for Android Things using the Rainbow HAT

Nate Ebel on Twitter

Woo hoo…it’s blinking https://t.co/nTuP6HzwGi

Here’s the  full Activity code.

And the full source code can be found here.

. . .

Want More Android Things

Want to skip the Rainbow HAT and build a UI application on your Android Things hardware? Then check out my previous post where I explore how to get started doing just that.

Android Things: From 0 to Hello World

 

Thoughts or advice on getting started with the Rainbow HAT? Comment below or share on social media.  I’d love to continue the conversation.

 

. . .

Subscribe

Thanks for Reading

 

I love to meet/talk/discuss with others so if you have feedback or want to chat you can follow me on Twitter, YouTube, FacebookInstagram, or Medium,

And don’t forget to subscribe to the blog to stay up to date on the latest posts

One thought on “Android Things: Hello Rainbow HAT

Leave a Reply