topamax once a day

Android + Arduino + USB Host + Pan Tilt LEDs

Combining analog and digital outputs with Android and Arduino.  This tutorial will demonstrate the basics for using two servos to achieve basic basic pan and tilt functionality. In addition, digital control is demonstrated using two LEDs.

For additional background information on interfacing Android with the real world, check out my other introductory tutorials:

Simple Digital Input
Simple Digital Output
Simple Analog Input
Simple Analog Output

 

Hardware

Parts needed:

  • Android Device (1.6+)
  • 2x Hobby servos
  • Pan / tilt bracket assembly
  • 2x LEDs
  • 2x 330 ohm resistors
  • Breadboard
  • Power supply
  • Hook-up wire
  • Android ADK Board*
  • – OR –
  • Arduino compatible and USB Host shield

*Supported boards include:

Google ADK boardFreeduino ADK board Seeed Studio ADK board, and DIY Drones ADK board

 

Assembly

Connect the red, power lines of the servos to +5v, the black ground lines to GND, and the yellow signal lines to the desired output pins, 5 and 6 in the example (others can be used, but must be PWM capable).  Also, connect the 330 ohm resistors in series with the anodes of the LEDs to the desired digital output pins, and the cathodes of the LEDs to ground.  Here is a diagram of the completed circuit (created with Fritzing):

 

Software

Arduino Firmware

Next, upload the Arduino sketch to the microcontroller. The sketch uses the Microbridge implementation by Niels Brouwers. Microbridge uses Android Debug Bridge (ABD) forwarding over TCP, rather than the official Google Android ADK. You can checkout the source for the Arduino sketch from Github, or just copy and paste the following into the Arduino IDE.

 

Android App

Finally, install the Android Demo application onto the device. You can either download the pre-built .apkor checkout the source from Github:

git clone git://github.com/mitchtech/android_adb_pan_tilt_leds.git

Finally upload the app to the device (or browse to this page on the device and download the apk above). Connect the Android device to the USB Host board/shield, and start up the app.

 

  • heriadhi

    i have galaxy young s5360 and arduino adk. I have tried the existing source code and try to connect with android program, but it does not happen, what should I do for this to work?

  • Hi heriadhi, from the context of your response, I’m not sure where you are failing.

    Have you tried the pre-compiled version to confirm the hardware is connected correctly?

  • heriadhi

    thx michael, now I’ve been able to establish communication between young samsung with arduino mega ADK

  • mahi

    Hi sir you have given nice tutorial, but
    when i run this app(Android side) in my device i am getting the following exception……
    07-27 16:51:32.590: E/microbridge(2940): Unable to start TCP server
    07-27 16:51:32.590: E/microbridge(2940): java.net.BindException: bind failed: EADDRINUSE (Address already in use)
    07-27 16:51:32.590: E/microbridge(2940): at libcore.io.IoBridge.bind(IoBridge.java:89)
    07-27 16:51:32.590: E/microbridge(2940): at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:150)
    07-27 16:51:32.590: E/microbridge(2940): at java.net.ServerSocket.(ServerSocket.java:100)
    07-27 16:51:32.590: E/microbridge(2940): at java.net.ServerSocket.(ServerSocket.java:69)
    07-27 16:51:32.590: E/microbridge(2940): at org.microbridge.server.Server.start(Server.java:86)
    07-27 16:51:32.590: E/microbridge(2940): at net.mitchtech.adb.PanTiltLedsActivity.onCreate(PanTiltLedsActivity.java:59)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.Activity.performCreate(Activity.java:4465)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.ActivityThread.access$600(ActivityThread.java:123)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    07-27 16:51:32.590: E/microbridge(2940): at android.os.Handler.dispatchMessage(Handler.java:99)
    07-27 16:51:32.590: E/microbridge(2940): at android.os.Looper.loop(Looper.java:137)
    07-27 16:51:32.590: E/microbridge(2940): at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-27 16:51:32.590: E/microbridge(2940): at java.lang.reflect.Method.invokeNative(Native Method)
    07-27 16:51:32.590: E/microbridge(2940): at java.lang.reflect.Method.invoke(Method.java:511)
    07-27 16:51:32.590: E/microbridge(2940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-27 16:51:32.590: E/microbridge(2940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-27 16:51:32.590: E/microbridge(2940): at dalvik.system.NativeStart.main(Native Method)
    07-27 16:51:32.590: E/microbridge(2940): Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)
    07-27 16:51:32.590: E/microbridge(2940): at libcore.io.Posix.bind(Native Method)
    07-27 16:51:32.590: E/microbridge(2940): at libcore.io.ForwardingOs.bind(ForwardingOs.java:39)
    07-27 16:51:32.590: E/microbridge(2940): at libcore.io.IoBridge.bind(IoBridge.java:87)
    07-27 16:51:32.590: E/microbridge(2940): … 19 more

    Please suggest about this………..
    What action to be taken to make this work….

    Thanks,
    Mahes

    • Are you running more than one application that is trying to use the same port number? The error:
      java.net.BindException: bind failed: EADDRINUSE (Address already in use) leads me to believe the issue is due to multiple apps contending for the same port.

  • i have the android side working, but i cannot seem to have any luck with the arduino software…. i get “stray / in program” and it won’t complle. this highlights the line “case 0x3:” any ideas?

    • Copying and pasting the code directly from the website seemed to grab extra characters, so I have enclosed it within a github gist. It should now work correctly without stray ‘\’s now!

  • Aof

    Blink:11: error: variable or field ‘adbEventHandler’ declared void
    Blink:11: error: ‘Connection’ was not declared in this scope
    Blink:11: error: ‘connection’ was not declared in this scope
    Blink:11: error: ‘adb_eventType’ was not declared in this scope
    Blink:11: error: expected primary-expression before ‘length’
    Blink:11: error: expected primary-expression before ‘*’ token
    Blink:11: error: ‘data’ was not declared in this scope
    Blink:12: error: expected constructor, destructor, or type conversion before ‘*’ token
    Blink:18: error: variable or field ‘adbEventHandler’ declared void
    Blink:18: error: ‘Connection’ was not declared in this scope
    Blink:18: error: ‘connection’ was not declared in this scope
    Blink:18: error: ‘adb_eventType’ was not declared in this scope
    Blink:18: error: expected primary-expression before ‘length’
    Blink:18: error: expected primary-expression before ‘*’ token
    Blink:18: error: ‘data’ was not declared in this scope

  • hello, I do not understand which version of ‘adb’ and ‘SPI’ library you usato.The official ‘adb MICROBRIDGE’ library does not work! ?? 🙂