Control an Arduino from Android device

This article shows you how to control an Arduino from your Android device. We will use USB connection. There are two ways to talk beetwen Arduino and Android: Android Debug Bridge (ADB) MicroBridge mode and Android Open Accessory Protocol. In this example we will consider a MicroBridge mode and will transfer data from Android device to the Arduino.

For Arduino programming we need Arduino IDE

For Android programming we need: Java SE Development Kit, Eclipse IDE, plugin Android Development Tools (ADT) for Eclipse and Android SDK.

After Eclipse is installed, you must install Android Development Tools (ADT). For this go to “Help->Install New Software…” and press button “Add”. In the field “Location” enter url: https://dl-ssl.google.com/android/eclipse/

arduino1-1

Now we are ready for our first project.

Arduino Code

#include "spi.h"
#include "adb.h"

// Adb connection.
Connection * connection;

// Event handler for the shell connection.
// This event handler is called whenever data is sent from Android Device to Seeeduino ADK.
// Any data / command to be sent to I/O of ADK has to be handled here.
//
// For eg: 1.Controlling an ouput port 2.Interacting with a device connected
// to ADK via IIC or Serial Port.

void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
{
if (event == ADB_CONNECTION_RECEIVE) // If recieve data
{
digitalWrite(13, data[0]); // Change LED state
Serial.println(data[0],DEC); // Debug
}
}

void setup()
{
Serial.begin(57600);

pinMode(13,OUTPUT); // Use internal LED L Seeeduino ADK

// Initialization ADB.
ADB::init();

// Open an ADB stream to the phone's shell. Auto-reconnect. Use any unused port number eg:4568
connection = ADB::addConnection("tcp:4568", true, adbEventHandler);
}

void loop()
{
// Poll the ADB subsystem.
ADB::poll();
}

Android code

In Eclipse IDE go to “New -> Project…” and fill the fields. Make sure that the Build SDK is selected to Google APIs and not to Android!

arduino1-2

The next step you need to add permission to manifest file:
<uses-permission android:name=”android.permission.INTERNET” />

Still, we need library for MicroBridge mode. You need copy 4 files in folder /src/org/microbridge/server/ to your project. After, our project (in Package Explorer) will look like this:

arduino1-3

Then we need to do main Activity of our Android application. For it we need 2 elements: TextView and ToggleButton.

arduino1-4

Source code of our MainActivity:

package com.example.arduino52;

import java.io.IOException;
import org.microbridge.server.Server;

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.widget.ToggleButton;
import android.widget.CompoundButton;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

    Server server = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ToggleButton mButton = (ToggleButton)findViewById(R.id.toggleButton1);
        mButton.setOnCheckedChangeListener(this);

        // Create TCP server (MicroBridge LightWeight)
        try
        {
            server = new Server(4568); //Port
            server.start();
        } catch (IOException e)
        {
            Log.e("arduino52", "Unable to start TCP server", e);
            System.exit(-1);
        }

    }

    @Override
    protected void onDestroy (){
        super.onDestroy();
        server.stop();
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        byte data;
        if(isChecked)   // If button checked
        {
            data = 1;
        }
        else
        {
            data = 0;
        }

        try
        {
            //Send data
            server.send(new byte[] {(byte) data});
        } catch (IOException e)
        {
            Log.e("arduino52", "Problem sending TCP message", e);
        }
    }
}

To send data using the method server.send ()

Video:

Download source code for Arduino, Android and Libraries

10 thoughts on “Control an Arduino from Android device”

  1. I’m working on arduino mega 2560 and android 4.2. I download the sample and i run it then i made a usb connexion between android and arduino (using an usb adaptor) just to make the led blinking but it doesn’t work ?? any suggestions?? thanx

  2. Interesting code!

    One question:

    I have Android 4 (which is locked by pattern lock) and Arduino M0 Pro (it has two Usb ports). Can I use this to detect, if Android has been unlocked? Thanks!

  3. Hi I have one question. Where did you write your arduino code? Is it in Arduino IDE or in Eclipse. Sorry I am a little confused with that. xD

Leave a Reply to Vili Volčini Cancel reply

Your email address will not be published. Required fields are marked *