Rainbow Hat Peripherals/Component Example

Android Things

Hi Folks, In this tutorial we will learn about Rainbow Hat and How to use this thing. 

Rainbow HAT has a buffet of sensors, inputs and displays to explore Android Things. Use it as a weather station, a clock, a timer or stopwatch, a mood light, or endless other things.

PIMORONI worked with the Android Things team at Google to create this great add-on board that features displays, sensors, sound, and lots of LEDs! It's the perfect introduction to developing Android Things applications on the Nxp Pico.

Rainbow HAT also has a full Python API for use on Raspbian.

Rainbow HAT features:


1. Seven APA102 multicolour LEDs
2. Four 14-segment alphanumeric displays (green LEDs)
3. HT16K33 display driver chip
4. Three capacitive touch buttons
5. Atmel QT1070 capacitive touch driver chip
6. Blue, green and red LEDs
7. BMP280 temperature and pressure sensor
8. Piezo buzzer
9. Breakout pins for servo, I2C, SPI, and UART (all 3v3)
10. Rainbow HAT pinout

Hardware Requirements: 

1. Nxp Pico Starter Kit

2. Rainbow Hat

Let's Do it 

So, Let's start a new Android Studio project.

Follow the instruction to create a new project

Start A new Android Application > Android Things > Android Things Peripheral Activity

Insteed of Selecting the UI, select peripheral activity, We will have only the programming part. Don't select the checkboxes.


 

Let's start with examples now, before that we have to add this dependency in build.gradle(Module: app).

implementation 'com.google.android.things.contrib:driver-rainbowhat:+'

here '+' means the latest available dependency.

next, to use the latest Rainbow Hat we need minimum sdk version 27 in build.gradle(Module: app) file.

minSdkVersion 27

next, we have to specify the app permission in AndroidManifest.xml file.

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

next, we have to import the driver in our MainActivity.java file

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

Now, we will see example codes from Rainbow Hat.

1. Turn on LED

// Light up the Red LED.
Gpio redLed = RainbowHat.openLedRed();
redLed.setValue(true);
// Close the device when done.
redLed.close();
// Light up the Blue LED.
Gpio blueLed = RainbowHat.openLedBlue();
blueLed.setValue(true);
// Close the device when done.
blueLed.close();
// Light up the Red LED.
Gpio greenLed = RainbowHat.openLedGreen();
greenLed.setValue(true);
// Close the device when done.
greenLed.close();

2. Display a String on Display

// Display a string on the segment display.
AlphanumericDisplay segment = RainbowHat.openDisplay();
segment.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
segment.display("ABCD");
segment.setEnabled(true);
// Close the device when done.
segment.close();

3. Play Buzzer

// Play the buzzer.
Speaker buzzer = RainbowHat.openPiezo();
buzzer.play(440);
// Stop the buzzer.
buzzer.stop();
// Close the device when done.
buzzer.close();

4. Get the current Temparature

// Log the current temperature
Bmx280 sensor = RainbowHat.openSensor();
sensor.setTemperatureOversampling(Bmx280.OVERSAMPLING_1X);
Log.d(TAG, "temperature:" + sensor.readTemperature());
// Close the device when done.
sensor.close();

5. Light up the LED strip

// Light up the rainbow
Apa102 ledstrip = RainbowHat.openLedStrip();
ledstrip.setBrightness(31);
int[] rainbow = new int[RainbowHat.LEDSTRIP_LENGTH];
for (int i = 0; i < rainbow.length; i++) {
    rainbow[i] = Color.HSVToColor(255, new float[]{i * 360.f / rainbow.length, 1.0f, 1.0f});
}
ledstrip.write(rainbow);
// Close the device when done.
ledstrip.close();

6. Detect button pressed

// Detect when button 'A' is pressed.
Button button = RainbowHat.openButtonA();
//for Button B and C use RainbowHat.openButtonB() and RainbowHat.openButtonC() respectively.
button.setOnButtonEventListener(new Button.OnButtonEventListener() {
    @Override
    public void onButtonEvent(Button button, boolean pressed) {
        Log.d(TAG, "button A pressed:" + pressed);
    }
});
// Close the device when done.
button.close();

So, these are the basic components we have in our Rainbow Hat, more tutorial is coming Soon on Rainbow Hat. Stay tuned. 

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