Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Created January 9, 2017 02:59
Show Gist options
  • Save patrickhammond/ba3a63d7609103b862bad6827984ac9d to your computer and use it in GitHub Desktop.
Save patrickhammond/ba3a63d7609103b862bad6827984ac9d to your computer and use it in GitHub Desktop.
Raspberry Pi 3 running Android Things driving an Arduino Uno to control an RGB LED

Demo video: https://goo.gl/photos/a4Ge3j8a32Sy2KoS6

To control an RGB LED, we need 3 PWM pins. The Raspberry Pi 3 only has one. Sad.

But the Arudino Uno I had sitting behind me that I've literally never opened has 6 PWM pins...more than enough!

So now...

  • The Raspberri Pi is running my Android Things app
  • That app sends messages over a serial conncetion to the Arduino
  • The Arudino controls the LED.

Circuit diagram for the LED is here: https://www.sunfounder.com/learn/Super_Kit_V2_for_RaspberryPi/lesson-5-rgb-led-super-kit-for-raspberrypi.html

Raspberry Pi GPIO to Arduino GPIO Pins are as follows:

  • Raspberry Pi 3 Ground -> Arduino Uno Ground
  • Raspberry Pi 3 Pi GPIO14 -> Arduino Uno GPIO 0/RX
  • Raspberry Pi 3 Pi GPIO15 -> Arduino Uno GPIO 1/TX

We need to add in the extra pins because USB devices are not support with Android Things Preview 1 (see: https://plus.google.com/112090467711235891544/posts/78pNQa52Li2). The USB connection from the Arudino in the video above is just for power.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:id="@+id/red_label"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"/>
<TextView
android:id="@+id/green_label"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"/>
<TextView
android:id="@+id/blue_label"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
while (Serial.available() > 0) {
int red = Serial.parseInt();
int green = Serial.parseInt();
int blue = Serial.parseInt();
if (Serial.read() == '\n') {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
}
}
package com.madebyatomicrobot.things
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import com.google.android.things.pio.PeripheralManagerService
import com.google.android.things.pio.UartDevice
import java.io.IOException
import java.nio.charset.Charset
class MainActivity : Activity(), OnSeekBarChangeListener {
companion object {
private val TAG = MainActivity::class.java.simpleName
}
lateinit var redLabelView : TextView
lateinit var greenLabelView : TextView
lateinit var blueLabelView : TextView
lateinit var redBarView : SeekBar
lateinit var greenBarView : SeekBar
lateinit var blueBarView : SeekBar
lateinit var device : UartDevice
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
redLabelView = findViewById(R.id.red_label) as TextView
greenLabelView = findViewById(R.id.green_label) as TextView
blueLabelView = findViewById(R.id.blue_label) as TextView
redBarView = findViewById(R.id.red) as SeekBar
greenBarView = findViewById(R.id.green) as SeekBar
blueBarView = findViewById(R.id.blue) as SeekBar
redBarView.max = 255
greenBarView.max = 255
blueBarView.max = 255
redBarView.setOnSeekBarChangeListener(this)
greenBarView.setOnSeekBarChangeListener(this)
blueBarView.setOnSeekBarChangeListener(this)
val manager = PeripheralManagerService()
try {
Log.i(TAG, "UART: " + manager.uartDeviceList)
device = manager.openUartDevice("UART0")
device.setBaudrate(9600)
device.setDataSize(8)
device.setParity(UartDevice.PARITY_NONE)
device.setStopBits(1)
showColor(0, 0, 0)
} catch (ex: IOException) {
Log.w(TAG, "Error in onCreate", ex)
}
}
override fun onDestroy() {
super.onDestroy()
try {
device.close()
} catch (ex: IOException) {
Log.w(TAG, "Error in onDestroy", ex)
}
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
val red = redBarView.progress
val green = greenBarView.progress
val blue = blueBarView.progress
showColor(red, green, blue)
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
// Don't care
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// Don't care
}
fun showColor(red: Int, green: Int, blue: Int) {
redLabelView.setText("Red ($red)")
greenLabelView.setText("Green ($green)")
blueLabelView.setText("Blue ($blue)")
val serialMessage = "$red $green $blue\n"
val bytes = serialMessage.toByteArray(Charset.forName("ASCII"))
device.write(bytes, bytes.size)
}
}
@rsavutiu
Copy link

rsavutiu commented Jun 7, 2017

How is an app on a tablet connected serially to an embedded device? Is it an USB cable? And do these amateur boards have Bluetooth? Classic or Low Energy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment