Sunday, March 31, 2013

Control Arduino Due LED from Android using ADK

In this exercise, we are going to implement a app on Android, it have two RadioButtons to control the LED on Arduino Due, by sending command via USB with ADK support.

Arduino code.
#include "variant.h"
#include <stdio.h>
#include <adk.h>
// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "HelloADKLED"; // the app on your phone
char accessoryName[] = "Arduino Due"; // your Arduino board
char companyName[] = "Arduino-er";
// Make up anything you want for these
char versionNumber[] = "0.1";
char serialNumber[] = "1";
char url[] = "";
USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
void setup() {
    pinMode(led, OUTPUT);
    //Indicate start of program
    digitalWrite(led, LOW);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        digitalWrite(led, LOW);
#define RCVSIZE 128
void loop() {
    char helloworld[] = "Hello World!\r\n";
    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
    if (adk.isReady()){
     , RCVSIZE, buf);
        if (nbread > 0){
            adk.write(nbread, buf);
            //Convert nbread to String
            String s = "";
            for (uint32_t i = 0; i < nbread; ++i) {
              s += (char)buf[i];
            if(s == "LEDON"){
              digitalWrite(led, HIGH);
            }else if(s == "LEDOFF"){
              digitalWrite(led, LOW);

Download the source code for Arduino.

The Android code refer to the next post.

Updated@2015-10-15: Android control Arduino Due LED, using ADK (Accessory Development Kit)


  1. Will it work on a Arduíno uno? Where CA i get the libraries for variante.h and usbhost? Thanks

    1. hello,

      The IDE I used is Arduino 1.5.2 from Arduino site (, the libraries for variante.h and usbhost are already included.

      But I think it cannot work on uno directly! When I tried to change the Tools->Board to Arduino Uno, errors of variant.h, USBHost, ADK... are reported.

    2. I had try to use Arduino 1.5.2 and your sketch but return many errors most on library variant.h.
      Why? Thanks

    3. Can I know your setup? What error returned?

      Did you select Tools->Board->Arduino Due (Programming Port) in Arduino software?

      I test on Ubuntu.

    4. My setup is :
      - Arduino 1.5.2
      - Your Sketch get on this page
      - Arduino Mega 2560 ADK

      After paste this code and compiling as Arduino Due(Programming Port), this is the errors :

      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.cpp:248: error: too many initializers for 'const PinDescription'
      -/Users/mino/Documents/Arduino/libraries/adk2/variant.cpp:233: error: 'NO_PWM' was not declared in this scope
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.cpp:233: error: 'NO_TC' was not declared in this scope
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:92: error: redefinition of 'const uint8_t SS'
      -/Users/xxx/Downloads/Arduino error: 'const uint8_t SS' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:93: error: redefinition of 'const uint8_t MOSI'
      -/Users/xxx/Downloads/Arduino error: 'const uint8_t MOSI' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:94: error: redefinition of 'const uint8_t MISO'
      -/Users/xxx/Downloads/Arduino error: 'const uint8_t MISO' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:95: error: redefinition of 'const uint8_t SCK'
      /Users/xxx/Downloads/Arduino error: 'const uint8_t SCK' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:133: error: redefinition of 'const uint8_t A0'
      /Users/xxx/Downloads/Arduino error: 'const uint8_t A0' previously defined here
      -/Users/xxx/Documents/Arduino/libraries/adk2/variant.h:134: error: redefinition of 'const uint8_t A1'

      and many others but similar.

    5. This exercise run on Arduino Due, I haven't Mega 2560 to test it.


    6. hello, how did you connect arduino with htc oneX? they are all micro-usb connector

    7. Hello, How did you connected with HTC OneX & Arduino Duo??
      Dual Micro USB connector ??



  2. hi,
    i have arduino DUE and i want to show my work in my phone.
    can you help me and you tell me about the steps for do that? what i put in my phone ? please help me.

    1. hello,
      what you means "show my works in my phone"?

    2. I'm working with Arduino DUE for signal decomposition to find the harmonics and show the signal and the spectrum of this signal and I used the software processing before for display, but this time I want to show this work in the phone (galaxy) or tablet,i mean (phone+processing+arduino due).

    3. Great project!

      may be you can read here:

      wish you success:)

    4. hi again , I've found the solution to this problem, but I have another problem appeared my i conneced my arduino in my phone with USB host but he tell me that he doesn't accept.why ?? and what is the adk ? need some help