Android of Things ❤️ Arduino

Sunday, Jun 11, 2017| Tags:

The Android of Things platform has some big advantages compared to Arduino or Raspbian. We Android developers can use the same tools we use for building applications for our projects, including all our favorite libraries, Google Play Services, and of course Kotlin.

On the other hand, there’s a big disadvantage. Compared to Arduino, the Raspberry Pi can’t handle the low level custom protocols of some sensors. And compared to Raspbian, many of the available HATs are currently not supported.

Simple Break game I did on Arduino: https://youtu.be/RZPtld5QKvQ Simple Break game I did on Arduino: https://youtu.be/RZPtld5QKvQ

How can we get the best of both worlds together? By using the right tool for the job. Let’s use the Arduino controller to read and write to our cheap and widely available sensors and use the Raspberry Pi to implement all the complex logic the same way we build Android applications.

Then, we will use the UART (universal asynchronous receiver/transmitter) of both platforms to exchange data between them. This is also known as the Serial on Arduino.

Wiring

Connecting the two devices is very simple:

  • Connect the GPIO pin 14 of the Raspberry to the RX pin of the Arduino (aka GPIO zero)
  • Connect the GPIO pin 15 of the Raspberry needs to the TX pin of the Arduino (aka GPIO 1)
  • Connect a Ground pin from the Raspberry to the Arduino.

This is how it should look like This is how it should look like

However there’s still one thing to do:

Take a look at the Configuring the UART mode in the official documentation:

Raspberry Pi 3 | Android Things

Since the UART Bluetooth mode is enabled by default, you can’t start using the UART to talk with the Arduino out-of-the-box. You need to go through that configuration step!

Code

I’ll like to give all the credit of this idea to Marcos Placona, who wrote an article about the same topic months ago and helped me get started:

How to use Arduino libraries with Android Things

The library he started didn’t fit my needs so I went with a simpler, one class solution that you can copy and modify for your projects:

This class provides three simple methods that cover the following:

  • Configure the UART Device to communicate with the Arduino. Use the highest baud rate available but don’t forget to use the same to initialize the Arduino Serial connection.
  • Read method that will read all the data from the UART and return it as a String.
  • Write method that will write any String into the UART.

On the Arduino project, you need to handle the received commands and write the responses afterwards. This is an example of how to do it:

You can write an “H” to the UART, and read back “Hello World” from it:

Now you can happily communicate your Android of Things applications with Arduino and use all the arsenal of sweet sweet cheap Arduino sensors on your Android of Things projects.

If you want to see it in action, here in this project I use an Arduino to read temperature and humidity values every hour and store them on Firebase database: https://github.com/miquelbeltran/android-of-things-experiments

Want to learn more Android and Flutter? Check my courses here.

INTERESTED IN WORKING TOGETHER?

Contact with me