Android Tutorial Arduino ADK Introduction

Introduction

This is one of a series of tutorials about using the Arduino Mega ADK microcontroller with Android devices

There already exists a Basic4android library for the IOIO microcontroller that exposes its I/O capabilities to an Android application program so allowing the program to control external devices. IOIO however is itself not programmable by the user and so is limited to applications where the host device program merely wants to directly access the I/O capabilities of the microcontroller.

There exists a popular class of microcontrollers called Arduino. These devices are intelligent and programmable in their own right. The official Arduino website at Arduino - HomePage is the best place to start investigating this. In particular there is one device, the Arduino Mega ADK that is designed to be used with Android devices and it is on the use of this device that we will be focussing. There is a description of it here Arduino - ArduinoBoardADK. Near the bottom of that page is a link for connecting the Arduino to a Windows computer “For installation follow the same procedure on how install an UNO board on your computer.” Ignore that link as the instructions are no longer correct and an updated inf file that works for Windows 8 as well as earlier versions is now supplied with the IDE. We will look at the IDE in the second of these tutorials and connect the Arduino to our PC in the third.


Android devices and USB

Any Android device with a USB port can act as a slave device. This means that it can be connected to a host device, such as a PC, where it appears as a memory device, or for later versions of Android, a media device. Such slave devices cannot connect to other slave devices such as keyboards, mice, memory sticks and RS232 adaptors.

Newer Android devices support host as well as slave operation and are switched into this mode by connecting an “On The Go” (OTG) or “Camera” cable to the Android device. Such a cable has a grounded connector not present on a normal cable that causes the device to switch to host mode.

Android devices capable of host mode can normally identify and use keyboards, mice and memory sticks when connected with an OTG cable and do not pass such devices on to the USB programming APIs so such devices are not available for use by application programs. However unknown devices, like RS232 adaptors are usually passed on to the USB Host APIs so that programs can take control of them. There do however seem to be some Android devices that fail to pass any device to the APIs; presumably this is a bug or software omission on those devices. If you have a Android 3.1 or later rooted device you could try what is detailed in this forum post to enable host mode. http://www.b4x.com/forum/additional...03-usb-rs232-adaptor-driver-5.html#post121049

Later versions of Android also support a slave mode of operation called Android Open Accessory Mode. Android Open Accessory support is included in Android 3.1 (API Level 12) and higher, and supported through an Add-On Library in Android 2.3.4 (API Level 10) and higher. Accessories that implement this are host mode devices and the Arduino Mega ADK is one such device.

A brief description may be found here
Build Accessories for Android | Android Open Source
and here
USB Accessory | Android Developers


There are two versions of the Android Open Access protocol. We are concerned with version 1, described here.
Android Open Accessory Protocol 1.0 | Android Open Source


This set of tutorials is based on this web page
Accessory Development Kit 2011 Guide | Android Developers


Arduino Mega ADK

The Arduino Mega ADK, referred to from now on as just the Arduino, has two USB connections, one host and one slave. The slave mode connector connects to a PC and emulates an RS232 port on the PC and through this port the Arduino IDE can download programs to the Arduino and communicate with programs running on the Arduino. The host port connects to an Android device and allows programs on the Android device to communicate with programs running on the Arduino.

To follow the tutorial you will need an Arduino Mega ADK and two USB cables. A standard A-B cable to connect the Arduino to your computer and whichever cable you use to connect your Android device to your computer which we will use to connect the Android device to the Arduino. It is also possible that you might need a power supply for the Arduino if your computer USB port cannot supply enough current to both run the Arduino and charge your Android device. This is because the Android device sees the Arduino as a host and so will try to charge from it. In this case if your device is chargeable from a source other than its USB port connecting that charger might be enough to make things work. Possible power issues are addressed in the fourth tutorial in this series.

The official Arduino Mega ADK is available world-wide from many distributors. The tutorial on the web page above assumes that the Arduino has a special Android Demo Shield plugged in to it. However, as well as being listed as discontinued by the few distributors that I could find who once sold it, it is an unnecessary expense so we will make do with only the Arduino and its single on-board LED. Hey, if you can wink a LED you can do anything!


Tutorials

In addition to this introductory tutorial there are other tutorials. They are:


1. This one!

2. Installing the Arduino IDE and compiling your first program. Installation

3. Connecting the Arduino Mega ADK to your computer and running programs on it. Programming

4. Connecting the Arduino Mega ADK to your Android device and writing both an Arduino and a Basic4android program to communicate with each other. Consummation

5. Some commentary on the two programs in tutorial 4. Coda


Possibly also, if there is sufficient interest in the Arduino:

6. Advanced topics about programming both the Arduino and the Android device.

and/or

7. A collection of FAQs based upon the experience of users.


The next tutorial will be number two above where we will install the Arduino IDE and ensure that we can compile a program that includes the Arduino libraries that are needed to talk to an Android device over USB using the Android Accessory protocol.
 
Last edited:

masterleous

Member
Licensed User
Longtime User
Excellent Work, I am developer of AVR Microcontroller based Embeded Systems, This tutorial opens alot of new and innovative ideas in my mind.

Thanks for your precious contribution :)
 

NewB4a

Member
Licensed User
Longtime User
Arduino Mega2560 and B4a

Hello,
I want to sent and receive Bytes from my Arduino Mega2560 (not ADK!) and I got problems with interpreting Bytes.
b4a-Side
B4X:
Sub Astreams_NewData (Buffer() As Byte)
    For i = 0 To Buffer.Length -1
    temp=temp & BytesToString(Buffer,i,1,"ASCII")
   receivedData.Text=temp
    Next   
End Sub

Sub btnSend_Click  
    Dim SingleCommand
   SingleCommand="ST,2,55"
   testdata=SingleCommand.GetBytes("UTF8")
   astreams.Write(testdata)
End Sub
arduino side:
B4X:
int count;
void setup() 
 {Serial.begin(9600); count=0;Serial.println("Start");} 
 void loop()
    {count++;Serial.write("Test");Serial.writeln(count);delay(1000);}

On my tablet I only get cryptical signs while the serial monitor of the arduino delivers correct results.
What changes are necessary in my b4a-app to show the correct value send by the arduino?
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Mismatch often depends on different settings for the communication protocol...have both sides the exact same parameters (baud, stopbit, etc...) ?

On my equipment all works fine, i transfer mass of sensor datas between Arduino and PC and Tablet.
 

shmulik taiber

Member
Licensed User
Longtime User
Arduino Mega2560 and B4a

Hello,
I want to sent and receive Bytes from my Arduino Mega2560 (not ADK!) and I got problems with interpreting Bytes.
b4a-Side
B4X:
Sub Astreams_NewData (Buffer() As Byte)
    For i = 0 To Buffer.Length -1
    temp=temp & BytesToString(Buffer,i,1,"ASCII")
   receivedData.Text=temp
    Next  
End Sub
 
Sub btnSend_Click 
    Dim SingleCommand
   SingleCommand="ST,2,55"
   testdata=SingleCommand.GetBytes("UTF8")
   astreams.Write(testdata)
End Sub
arduino side:
B4X:
int count;
void setup()
{Serial.begin(9600); count=0;Serial.println("Start");}
void loop()
    {count++;Serial.write("Test");Serial.writeln(count);delay(1000);}

On my tablet I only get cryptical signs while the serial monitor of the arduino delivers correct results.
What changes are necessary in my b4a-app to show the correct value send by the arduino?


Hello .
I'm new to the B4A, and struggling to do exactly what you succeeded .
The arduino side is not the problem, but the right code in the B4A . could you post your entire app so I can reverse engineer it ?
I still don't know how to setup the communication between them in B4a . I tried the example code in Bluetooth , it recognizes my UNO but doesn't communicate .
please help,
thanx
 

Beja

Expert
Licensed User
Longtime User
Thanks Agraham for this useful and thoughtful tutorial..
For quite a while I am trying to understand the importance of this Arduino device.. according to so many posts
on this and the questions forum, it looks popular. But when a few weeks ago I went to Radio Shack and saw a
number of them, I see they are so expensive compared to any simple PIC or AVR pcb that you can design and
assemble. I have a few mControllers with PIC and Atmel and they are more powerful in processing, more memory
space and IOs.. but the cost is down to earth. I program them easily with B4A. I don't know what's the special
thing in Arduino.
 

fdx12345

Active Member
Licensed User
Longtime User
Thanks Agraham for this useful and thoughtful tutorial..
For quite a while I am trying to understand the importance of this Arduino device.. according to so many posts
on this and the questions forum, it looks popular. But when a few weeks ago I went to Radio Shack and saw a
number of them, I see they are so expensive compared to any simple PIC or AVR pcb that you can design and
assemble. I have a few mControllers with PIC and Atmel and they are more powerful in processing, more memory
space and IOs.. but the cost is down to earth. I program them easily with B4A. I don't know what's the special
thing in Arduino.


Is the high level (C-based) programming software free for the PIC and AVR? If so, were is it located? Thanks much
 

Beja

Expert
Licensed User
Longtime User
MCURTC.png
Hi,
This is a very old thread..
The controller I was talking about is custom built for our company.. I finished it when I was in China about 6 years ago and
made only 20 pieces of it. it has RTC with internal battery that will keep time for 10 years without power supply, so your
application will be running (without output) for years. The firmware is programmed in C but all commands are through the
serial port.. it has 2 ports RS232 and RS485, which means you can use any language to send the binary commands in HEX format
the one thing you need to know is the instruction set, then you just send it as plain hex bytes... The MCU is Atmel.
 
Top