Connecting an Android to an Arduino Mega and exchanging information through the USB.
The Arduino Mega should be fitted with an Atmega8u2 or Atmega16u2, FTDI chips will not work.
This does the trick, a OTG-cable
[img:6a588af553]http://members.home.nl/hobbycorner/images/Android2Arduino/OTG_cable.jpg[/img:6a588af553]
In the attachment a small program on the Android. Written in Basic4Android.
You need two extra libraries, RandomAccessFile (I have used version 1.50) and USBSerial (I have used version 1.00).
Here a screen shot of the Android program. 4 buttons and when you have a connection pushing one of the 4 buttons will return a reaction from the Arduino.
[img:6a588af553]http://members.home.nl/hobbycorner/images/Android2Arduino/Android2Arduino.jpg[/img:6a588af553]
Here the hardware setup
[img:6a588af553]http://members.home.nl/hobbycorner/images/Android2Arduino/Android2Arduino2.jpg.JPG[/img:6a588af553]
Bascom4Android code:
[code:1:6a588af553]'Android2Arduino Small example made by Ben Zijlstra - 2013
'You need the library: RandomAccessFile. I have used version 1.50 and USBSerial. I used version 1.00
Sub Process_Globals
Dim usb As UsbSerial
Dim astreams As AsyncStreams
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
End Sub
Sub btnOpen_Click
If usb.Open(9600) Then
ToastMessageShow("Connected succesfully",False)
astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
Else
ToastMessageShow("Error opening USB port",False)
End If
End Sub
Sub Astreams_Newdata(buffer() As Byte)
ToastMessageShow(BytesToString(buffer,0,buffer.Length,"UTF8") , False)
End Sub
Sub btnClose_Click
astreams.Close
End Sub
Sub btnSend0_Click
astreams.Write("1".GetBytes("UTF8"))
End Sub
Sub btnSend1_Click
astreams.Write("2".GetBytes("UTF8"))
End Sub
Sub btnSend2_Click
astreams.Write("3".GetBytes("UTF8"))
End Sub
Sub btnSend3_Click
astreams.Write("4".GetBytes("UTF8"))
End Sub
Sub AStreams_Error
astreams.Close
End Sub
Sub Astreams_Terminated
ToastMessageShow("Terminated",False)
astreams.Close
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
[/code:1:6a588af553]
And the Bascom-AVR-code:
[code:1:6a588af553]'Android2Arduino
'Connecting an Arduino to an Android
'Arduino receives commands from the Android using the main serial port,
'reacts on commands and returns status information via the same port.
'To be recognized by the Android the Arduino device must be fitted with either
'an Atmega16U2 or Atmega8U2 programmed as an USB-to-serial converter.
'The Android software will not work with FTDI or Prolofic serial based boards.
'Android 3.1 or above attached via USB cable to a suitable Arduino Uno or Mega
'Idea from G.White (Westy53) - Nov. 2012
'Converted to Bascom-AVR by:
'Ben Zijlstra - July 2013.
$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 64
$framesize = 64
$baud = 9600
Open "Com1:" For Binary As #1
Print #1 , "Start programma"
Dim A As Byte , S As String * 2
Do
A = Ischarwaiting(#1)
If A = 1 Then 'we got something
A = Waitkey(#1)
Select Case A
Case 49 : Print #1 , "A";
Case 50 : Print #1 , "B";
Case 51 : Print #1 , "C";
Case 52 : Print #1 , "D";
End Select
End If
Loop
End
[/code:1:6a588af553]
Just a simple example.
Have fun
Ben Zijlstra
↧