Android connected to Arduino

The goal of this article is to explain how you can easily communicate between an Android application and an arduino board.
It’s a good way to quickly prototype some stuff to connect some electronic to Internet!

2014-03-06 00.36.30

– You need to be able to write a simple program on your ardunio.
– You need to be able to write a simple application for android.
– You need an android phone that support USB OTG mode.

Please note that the code is not “clean”. My goal was just to create a simple demo.
You can download the code here: AndroidArduino.tar. My code is totally free to use if you remove any references to my name.

It’s not the only solution. You can implement a USB protocol on the device. Another way is to use an external serial module or some bluetooth adapters.

Shopping time!

Cost: ~$25 + mobile phone.

You can use your own stuff, but the samples work with the following material on Please note that the average delivery time is 4-6 weeks. If you buy for less than $50, you don’t have to pay taxes in Switzerland.

Connect a USB cable to your phone

A USB to TTL port to communicate with arduino using USB port.

Arduino Micro-Controller

A RGB LED controller

A temperature sensor

You may want to buy
A nice kit

Build the stuff

If you want to use my code sample:

Connect the RGB led:
* – Connect red to pin 5
* – Connect green to pin 6
* – Connect blue to pin 7
* – Connect V to VCC

Connect temperature sensor to:
* – S to pin 12
* – Middle pin to VCC
* – Pin “-” to GND

Connect the programmer to the device and burn my firmware. You should see a red, green and blue “lightshow”.

Connect the USB port to the OTG adapter.

Install my application on the phone, connect the device to the phone and you will be able to communicate between the phone and the arduino using the USB port of your phone and a serial protocol.

You can get the temperature of the device on your phone. You can set the level of the leds.

You will see the battery of the phone going down really fast 🙂 You can install a powered USB hub, but it doesn’t work on every phone.

On the Ardunio side

You need to include #include <HardwareSerial.h>

In setup(), set the speed of the serial port
void setup() {

in loop()
int r =;
if (r != -1) { <– If we have received something
if (r == ‘x’) {  <– If we have received the char  ‘x’.


Send some char back

It’s really easy to communicate using the serial port on the arduino part.

In “tools -> serial port” of the development kit, you can communicate with your device and test that you communication protocol is working as expected  before testing on the phone.

On the Android side

You can see the documentation here:

In my activity, I start the communication with the serial port.
private Ardunio ardunio;

arduino = new Arduino(this) {

“Arduino” is an abstract class in a thread. I instantiate the class and implement the abstract method in my activity.
public void info(final String txt) {
runOnUiThread(new Runnable() {
public void run() {

In my Activity, I start / stop a thread that communicate with the Ardunio.

public void onPause() { super.onPause();

protected void onResume() { super.onResume();

I use the library provided by FTDI to interact with the device. FTDI is the manufacturer of most popular USB<->Serial chips.
private final D2xxManager ftD2xx;

Open the first FTDI device
int cntDev = ftD2xx.createDeviceInfoList(activity);
if (cntDev >= 1) {
FT_Device dev = ftD2xx.openByIndex(activity, 0);

Read what is on the serial port. Timeout after 0.2 seconds. Please note that I read one character at a time – the FTDI implementation seams to have a bug if you read more than one character at a time…
int c = 1;
while (c > 0) {
c =, 1, 200);
if (c > 0)
buff.append((char) b[0]);

I can send something to the serial port using
String s = “r” + this.r + “g” + this.g + “b”+ this.b;

Don’t hesitate to see my code to understand the details and some “tips” that are not included in this article!