Getting Started with Android things, Lesson 5: RGB LED

Julien Saito
Sep 5, 2018 · 3 min read

Edit

The goal of this lesson is to change the intensity of the LED to make it change color.

The hardware part is almost the same as lesson 4 but multiplied by 3. Because an RGB LED has 3 controlling pins and a ground, you will need 3 resistors an a cable connecting to the ground. Just connecting the RED pin to the PWM and running the ATT4 project, the LED is “breathing” correctly. It should be easy to control each color of the LED using one PWM per color, unfortunately the android things only has 2 PWM ports. So we will need to do additional programming for the blue pin.

We need to use one PWM per color:

An RGB LED

For the Red pin, We are using the PWM1 port so on the T-extension board it correspond to port B18. Again If you do not know what a PWM is, read this documentation

For the Green pin, we are using PWM2 port that is port B13 on the extension board.

For the blue pin, we will use GPIO2_IO02 and a library to use it as a PWM.

Here is the programming part

Android Thing Test 5 (ATT5) project:

I just reuse ATT4 as the based and modified it a bit.

on create we open the PWM1 port and set the duty cycle to 0%,

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try {
pwmRed = manager.openPwm(PWM1_PORT)
pwmRed.setPwmFrequencyHz(2000.0)
pwmRed.setPwmDutyCycle(0.0)
pwmRed.setEnabled(true)

} catch (e: IOException) {
Timber.e(e,"Error on PeripheralIO API")
}

we repeat the same code for PWM2 for the green LED

try {
pwmGreen = manager.openPwm(PWM2_PORT)

// Always set frequency and initial duty cycle before enabling PWM
pwmGreen.setPwmFrequencyHz(2000.0)
pwmGreen.setPwmDutyCycle(0.0)
pwmGreen.setEnabled(true)

} catch (e: IOException) {
Timber.e(e,"Error on PeripheralIO API")
}

And for the Blue LED we use the Soft pwm library:

try {
pwmBlue = SoftPwm.openSoftPwm(PWM3_SOFT_PORT);

// Always set frequency and initial duty cycle before enabling PWM
pwmBlue.setPwmFrequencyHz(300.0)
pwmBlue.setPwmDutyCycle(0.0)
pwmBlue.setEnabled(true)

} catch (e: IOException) {
Timber.e(e,"Error on PeripheralIO API")
}

onStop the usual closing methods:

override fun onStop() {
super.onStop()
handler.removeCallbacks(changePWMRunnable)
pwmRed?.close()
pwmGreen?.close()
pwmBlue?.close()
}

and here is the repeating runnable (it is the same as ATT4):

we increment the activePulseDuration up to 100 then back to 0.

private val changePWMRunnable = object : Runnable {
override fun run() {
if (pulseIsIncreasing) {
activePulseDuration += STEP
} else {
activePulseDuration -= STEP
}
// Bounce activePulseDuration back from the limits
if (activePulseDuration > MAX_DUTY_CYCLE) {
activePulseDuration = MAX_DUTY_CYCLE
pulseIsIncreasing = !pulseIsIncreasing
} else if (activePulseDuration < MIN_DUTY_CYCLE) {
activePulseDuration = MIN_DUTY_CYCLE
pulseIsIncreasing = !pulseIsIncreasing
}
try {
//the duty cycle is a percentage
pwmRed.setPwmDutyCycle(activePulseDuration)
pwmGreen.setPwmDutyCycle(activePulseDuration)
pwmBlue.setPwmDutyCycle(activePulseDuration)
handler.postDelayed(this, INTERVAL_BETWEEN_STEPS_MS)
} catch (e: IOException) {
Timber.e(e, "Error on PeripheralIO API")
}
}
}

Here is the full source code : https://github.com/otiasj/AndroidThings/tree/master/ATT5

Julien Saito

Written by

Android Things and Sunfounder Super Kit v3.0

Android thing kit using the Sunfounder starter kit V3

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade