AndroidThings: Hello Rainbow HAT

Nate Ebel
goobar
Published in
4 min readFeb 25, 2018

--

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”

Check out this video to see more on installing the standoffs

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

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

Or you can check out the videos in my Android Things YouTube playlist

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

“Great, now we have an empty project. So…. how do we turn on a light?”

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

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.8'
}

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

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.

Check out my YouTube playlist where I’m sharing more of my Android Things Explorations

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

I love to meet/talk/discuss with others so if you have feedback or want to chat you can follow me on Twitter, YouTube, here on Medium, or subscribe here to stay up to date on the latest posts from my blog.

--

--

Nate Ebel
goobar

Building great software and helping others do the same.