Getting Started with Android things, Lesson 1: Blinking LED

Edit

So today I am starting the lesson 1 of the Sunfounder super kit v3.0, which is just about making an LED blink.

The Sunfounder manual provided with the Kit.

I started by removing the Rainbow HAT and replaced it by the 40 pin GPIO cable and the T-Extension Board. This makes it pretty convenient to power the breadboard. The manual explains in detail how to plug the cable with pictures and explanations. Long story short the black side of the cable goes on pin 1 and 2.

As a beginner I really had no idea how the breadboard was connected. If you follow the green arrow on the schematic, it starts with a 3.3v line on the left. The T-extension board makes it so that the first line is ground and the second line is 3.3v. (Similarly the 2 bottom lines are Ground and 6Volts).

The provided schematic

The columns are connected vertically. I added a bit cable (In red on the schematic) to power the column that leads to the resistor. Then back up to the LED, down to the blue cable, that exits to port B17.

Notice I used port B6 for mine.

After the wiring was done, all I needed was to create the application in Kotlin instead of the provided Python code.

Android Thing Test 1 (ATT1) project:

If you are familiar with Android development, this step is pretty straight forward. (Otherwise you can follow this tutorial to get started)

Here is how the code looks:

class MainActivity : Activity() {
private var ledRed: Gpio? = null
private val handler = Handler()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val manager = PeripheralManager.getInstance()
try {
val ledRed = manager.openGpio("GPIO2_IO02")
this.ledRed = ledRed
ledRed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
handler.postDelayed(blinkRunnable, 1000L)
} catch (e: IOException) {
Log.e("Error on PeripheralIO API", e)
}

}

override fun onStop() {
super.onStop()
handler.removeCallbacks(blinkRunnable)
ledRed?.close()
}

private val blinkRunnable = object : Runnable {
override fun run() {
try {
ledRed?.let {ledRed ->
ledRed.value = !ledRed.value //turn on/off
handler.postDelayed(this, 1000L)
}
} catch (e: IOException) {
Log.e("Error on PeripheralIO API", e)
}
}
}

}

So onCreate, I open the GPIO named GPIO2_IO02 corresponding to the B6 port on the T Extension (more on this later).

val ledRed = manager.openGpio("GPIO2_IO02")

Then the documentation tells us that the DIRECTION_OUT_INITIALLY_LOWconstant is used to configure a GPIO pin as an output and initialize its state to false.

ledRed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)

then I post a delayed runnable to execute some code after 1000ms:

handler.postDelayed(blinkRunnable, 1000L)

that runnable just calls:

ledRed.value = !ledRed.value

which basically turn the LED ON or OFF depending on it’s previous state.

Finally onStop, we do a bit of clean up:

handler.removeCallbacks(blinkRunnable)
ledRed?.close()

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

Wait!

Why did I switch between the port B17 to B6 and why does it correspond to GPIO2_IO02?

As I mentioned before, the manual and the kit are made for the Raspberry pi 3. So the schematics of the T-extension board are also given for the Rapsberry Pi output. My Android things is the NXP i.MX7D Starter Kit.

Here is the information I used :

The T-extension schematic shows that B6 correspond to GPIO22 on the raspberry pi
GPIO 22 on the raspebrry pi 3 correspond to pin 31 32 on a regular 40 pin connector.

Given the 2 schematic I deduced that port B6 corresponds to pin 31 of the 40 pin connector.

And given the reference for the NXP i.MX7D I was able to conclude that B6 on the T-extension corresponds to GPIO2_IO02.

That’s it!

if someone reads this and has better references for the T-Extension board, please drop me a note. Otherwise I will add the ports that I use as I walk through the lessons.

next up: Lesson 2 Controlling an LED by a Button.

--

--