Wednesday, April 3, 2013

Write and read analog on Arduino Due from Android using ADK

In this example, Android device can write analog output (DAC0/DAC1) and read analog input (A0) on Arduino Due board by using ADK. The video below show how it work, with DAC1 pin and A0 pin connected together, such that the analog out loop back to analog input.


Code in Arduino side:

#include "variant.h"
#include <stdio.h>
#include <adk.h>
#include <stdlib.h>

 
// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "ArduinoDueAdkADC"; // 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[] = "https://sites.google.com/site/arduinosite/exercise/androidadkadc/AndroidAdkADC_0.1.apk";
 
USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);
 
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
int analogInputA0 = A0;   //ADC input
int analogOutput0 = DAC0;  //DAC output
int analogOutput1 = DAC1;  //DAC output

int varA0;
uint8_t adkVarA0[4];
 
void setup() {
    Serial.begin(9600);
    cpu_irq_enable();
    
    analogReadResolution(12);  //set ADC resolution to 12 bits
   
    pinMode(led, OUTPUT);
    pinMode(analogInputA0, INPUT);
    pinMode(analogOutput0, OUTPUT);
    pinMode(analogOutput1, OUTPUT);
    
    //Indicate start of program
    digitalWrite(led, LOW);
    delay(2000);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        delay(250);
        digitalWrite(led, LOW);
        delay(250);
    }
}
 
#define RCVSIZE 128
 
void loop() {

    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
   
    Usb.Task();
     
    if (adk.isReady()){
      
        adk.read(&nbread, RCVSIZE, buf);
        if (nbread > 0){
            uint8_t dacValue = buf[0];
            analogWrite(analogOutput0, dacValue);  //Write to ADC
            analogWrite(analogOutput1, dacValue);  //Write to ADC
        }
        
        varA0 = analogRead(analogInputA0);

        //Send as 2 bytes
        adkVarA0[1] = varA0 & 0xFF;
        adkVarA0[0] = (varA0 >> 8) & 0x0F;
        adk.write(2, adkVarA0);
        
        delay(500);
    }    
}

Download>>

Android side code to write and read analog on Arduino Due with ADK



Take caution when you work on the IO pins, it's easy to be burnt out! The DAC0 on my board have no function now, because I mis-connected it to GND accidentally.

7 comments:

  1. This is cool. I like your work.

    ReplyDelete
  2. Hi,

    I have tried your code on my Arduino DUE and on three different smartphones.

    I observed a different behaviour with two different versions of Android:
    - the demos work very well on a Samsung Galaxy S2 and on a Samsung Galaxy Poket Neo (both with Android v4.1.2)
    - the demos work only partially on a Samsung Galaxy Note 3 (Android v4.3).

    I observe that on Galaxy Note 3 the communication between the Arduino and Android App works only in one direction, that is, Arduino receives the data sent to it by the Android App but not the vice-versa, that is, the data sent that Arduino send to Android is not received (and displayed) by the App.

    I used Arduino v1.5.5 to compile and upload the sketch to my Arduino DUE; both steps finish whitout errors.

    I don't have other smartphones/tablet with Android v4.3 or greater, so I would like to know if you have tried your code on this versions of Android.

    Thank you in advance for your answer.

    Regards

    ReplyDelete
  3. Hello Francesco Adamo,

    As shown on the video, I tested on HTC One X only.

    And In my trial and from comments in Internet, the power supply to Due will cause unstable.

    ReplyDelete
    Replies
    1. Dear Eric,

      thank you for your answer!

      Unfortunately in my case the problem cannot be related to power supply issues because I powered my Arduino DUE using a laboratory PSU with an output current well beyond the needs of Galaxy Note 3.

      I'm looking to find other smartphones/tablets based on Android 4.3 or greater to make a comparison.

      Thank you again for your demos and answer; regards.

      Delete
    2. I tested it again on Nexus 7 1st gen, running 4.4.2, it work one time only! After then, cannot connect again! May be I need more time to check it.

      Delete
  4. Hello, I got this example to work successfully with a Due and Nexus 7 (2013), thanks!

    ReplyDelete