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


This driver provides easy access to the peripherals available on the Rainbow Hat for Android Things: BMP280 temperature and pressure sensor (I2C) HT16K33 segment display (I2C) Capacitive buttons (GPIO) LEDs (GPIO) APA102 RGB LEDs (SPI) Piezo Buzzer (PWM) Servo header (PWM) The pin/bus mapping and helper methods provided by this driver are only compatible with the following boards: Raspberry Pi 3 and NXP Pico i.MX7D.

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

. . .

Add the dependency

dependencies {
    compile ''


Open a connection to an LED

Import the driver


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?) {

    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() {

    // Remove pending blink Runnable from the handler.


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


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

I’ve been excited at the prospect of IoT and building with Android Things for a long time I had visions of building complex and exciting projects over my winter vacation. I finally decided the time was right, and started down the rabbit hole with a simple Google search “android things kit”.


Thoughts or advice 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 and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel

Categories: Android Development, Software Development

Tags: , , ,

1 reply

  1. What’s the length of your standoffs?

Leave a Reply