adk-chat-android.ino

Arduino Mega ADK (Rev3) · File : adk-chat-android.ino · Language : Arduino · Use case : Dialoguer avec un téléphone Android en mode Accessoire

The Google ADK protocol makes the Android a peer: plugged into the USB-A port, it opens a serial channel with the board, no driver needed. The app matches manufacturer « Arduino » and bytes flow both ways.

Hardware required

An Arduino Mega ADK Rev3 (ATmega2560 + MAX3421E USB host chip with a Type-A port; digital pin 7 is reserved for the MAX3421E reset). 1 Android phone (with an Accessory app, e.g. the ADK demo).

Wiring

USB-A to phone micro-USB cable; D7 stays reserved for the MAX3421E.

How it works

AndroidAccessory negotiates Accessory mode (AOAP) at enumeration; write/read carry the data, isReady reports the link. Select « Arduino Mega ADK » in the IDE. For USB host/ADK work, install the USB Host Shield 2.0 library; do not use D7. The USB-A port powers the accessory at 5 V.

Going further

Drive a motor from the app, show a sensor on the phone screen, or fire a notification.

adk-chat-android.ino

// Chat ADK : la carte detecte un Android en mode Accessory (USB-A).
// Bibliotheque : USB Host Shield 2.0 (AndroidAccessory.h).
#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>

AndroidAccessory adk("Arduino", "Mega ADK", "Pont 1.0", "1.0", "Fab", "Num");

void setup() {
  Serial.begin(9600);
  adk.Init();
  Serial.println(F("Attente d'un Android en mode Accessoire..."));
}

void loop() {
  if (adk.isReady()) {
    // 1) envoyer l'heure de debug vers l'app
    String msg = "Bonjour ADK " + String(millis() / 1000);
    adk.write(msg.length(), (uint8_t *)msg.c_str());

    // 2) lire les commandes de l'app (LED sur '1'/'0')
    if (adk.available()) {
      char c = adk.read();
      digitalWrite(13, c == '1' ? HIGH : LOW);
      Serial.print(F("Recu: ")); Serial.println(c);
    }
  }
  delay(500);
}

← Back to the board