Pro Yearly is on sale from $80 to $50! »

Hacking Arduino with Android

F6c4a7544807fe0b8a874e83eb84b481?s=47 Pearl Chen
October 12, 2013

Hacking Arduino with Android

Presented at Big Android BBQ 2013 (#babbq13)


Pearl Chen

October 12, 2013


  1. Google+: Twitter: @PearlChen (or the other way around?) Hacking

    Arduino with Android Pearl Chen Slides:
  2. About me Technologist frontend web developer emerging technology hacker

    UX designer (former ‘artist’) HTML + CSS + JavaScript Android + Arduino/NFC + Educator
  3. My first hardware project

  4. More projects

  5. Who this talk is for Everyone is an inventor...

    yes, that includes you! I bought an Arduino a long time ago and I just don’t know what to do with it yet...
  6. Agenda • Arduino basics • hardware and how to

    program • Android+Arduino projects • examples from around the web • what protocols exist • Hands on time! • try out “ADK Andy” • build your own circuits from instructions • in-depth circuit creation
  7. Arduino basics

  8. Read about all the Arduino boards: What is

    Arduino? It’s hardware (or rather, a family of hardware)
  9. The Arduino IDE. Download the latest here: What

    is Arduino? And it’s software!
  10. Why use an Arduino? (Part 1) Analyze incoming data

    using computer logic Affect something physical (or virtual) Sense something happening in the physical world Scenario #1: Sense real world events The computer analyzes the weight of the pressure input to be larger than a sparrow’s weight. Squirrel?? A water hose is triggered and a spray of water is sent out in the direction of the intruder! A pressure sensor detects that something is trying to take seeds from a bird feeder. Examples The computer analyzes the levels to be lower than normal. The interface on a mobile phone app updates and warns the diabetic that they should eat an apple. A diabetic uses a glucose sensor to measure their blood-sugar level.
  11. Why use an Arduino? (Part 2) Affect something physical

    Notification of something happening in the virtual world The computer analyzes the sender to be your best friend. A LED sitting on your desk blinks urgently to get your attention. A Twitter @mention to your Twitter handle is detected. Examples Analyze incoming data using computer logic Scenario #2: Affect real world objects from online events
  12. Need more detail? Check out the Sparkfun buying guide: How to choose an Arduino Uno Leonardo LilyPad Micro Chip Speed Memory Input/Output Pins Price Best used for ATmega328 ATmega32u4 ATmega328 ATmega32u4 16 MHz 16 MHz 8 MHz 16 MHz 32 KB 32 KB 32 KB 32 KB 14 20 14 20 ~$30 ~$25 ~$22 ~$23 general, all purpose general, all purpose, built-in USB comm wearables tiny spaces
  13. How to choose an Arduino Leonardo Mega 2560 Mega

    ADK Yún Chip Speed Memory Input/Output Pins Price Best used for ATmega32u4 ATmega2560 ATmega2560 ATmega32u4 & Atheros AR9331 16 MHz 16MHz 16MHz 16MHz 32 KB 256Kb 256Kb 32Kb 20 54 54 20 ~$25 ~$60 ~$80 ~$90 general, all purpose, built-in USB comm more pins or longer programs USB host, supports Android Open Accessory API networked projects (Ethernet or WiFi), SD card Need more detail? Check out the Sparkfun buying guide:
  14. What about a Raspberry Pi? Arduino (Leonardo) Raspberry Pi

    (Model A) $25 + additional computer, usb cord $35 + mouse, keyboard, monitor, connectors micro-controller mini-computer runs firmware/programs runs an operating system designed to quickly prototype single-purpose electronics projects additional libraries needed to control pins (SPI and I2C missing) established ecosystem of shields and add-ons can buy plain BT or WiFi adapters, more coming? vs MAKE has a good comparision
  15. BeagleBone? What about... MaKeyMaKey? FLORA? mbed? PIC? netduino? Espruino?

    RFduino? Basic Stamp? plus all the other stuff showing up on Kickstarter every month???
  16. Arduino code • Simplified version of C++ • .ino

    extension for Arduino version 1.0+ .pde for older sketches • Called a “sketch” because it should feel like picking up a pencil and making a quick drawing
  17. setup() and loop() • setup() Runs on power up

    or when reset button is pressed. • loop() Executes forever, as long as there is power and no code errors 2 functions are required for every Arduino program:
  18. The 4 basic “modes” Digital Input Digital Output Analog

    Output Analog Input
  19. digitalWrite() for output • pinMode( pin#, OUTPUT ); Set

    a pin to send outgoing commands • digitalWrite( pin#, HIGH ); Output to a pin: HIGH / 5 volts / “on” / 1 • digitalWrite( pin#, LOW ); Output to a pin: LOW / 0 volts / “off” / 0
  20. digitalRead() for input • pinMode( pin#, INPUT_PULLUP ); Set

    a pin be used as a digital input pin with an internal “pull-up resistor” (to avoid erratic “floating” values when the switch is open) • digitalRead( pin# ); Listen for input values on a pin. Since it’s a digital signal, it will be either HIGH (1) or LOW (0)
  21. analog pins for analog outputs • Analog output pins

    (~): 3, 5, 6, 9, 10, 11
  22. analogWrite() for analog outputs • pinMode( pin#, OUTPUT );

    not necessary for analog • analogWrite( pin#, dutyCycle ); Output to a pin a duty cycle between 0 (always off) and 255 (always on).
  23. analog pins for analog inputs • Analog input pins:

    A0, A1, A2, A3, A5, A5
  24. analogRead() for analog input • pinMode( pin#, INPUT );

    Setting pinMode() not necessary for analog -- but won’t hurt. • analogRead( pin# ); Value will be between 0 and 1024.
  25. Serial for debugging • in setup() use Serial.begin( baudRate

    ) • in loop() use Serial.print() or Serial.println() • To see the serial monitor console, go to Tools > Serial Monitor while your Arduino is hooked up to your computer via USB. Make sure that the baud rate in the lower righthand corder is also the same as that in Serial.begin().
  26. analogRead() with serial debugging • pinMode( pin#, INPUT );

    Setting pinMode() not necessary for analog -- but won’t hurt. • analogRead( pin# ); Value will be between 0 and 1024.
  27. example Android+Arduino projects and other hardware

  28. by Cute Circuit ⇪ A-Nerve

  29. Project ideas So what can you make with an Android

    phone/tablet talking to external hardware? 1 + 1 = 3 Things already available on your phone Electronics components you can add Super awesome stuff!
  30. Things already available on your phone accelerometer camera wifi/3G

    touch screen GPS mass storage proximity/light sensors SMS microphone NFC anything that can fire an Intent (e.g. notifications) IR blaster
  31. Super awesome stuff! Music Beta: “Now Playing” by Chris

    Juergen Aerogarden Monitor by Sam Steele Space Spheres by NASA Floating Sensor Project by UC Berkeley
  32. Android Development Kit (ADK) Arduino Mega ADK

  33. More Android Development Kits! SparkFun IOIO ODroid ADK Microchip

    PIC24F ADK
  34. Are there wireless ADKs? Bluetooth Shields IOIO w/ USB Bluetooth

    dongle Coming soon? ADK2012
  35. Basic firmware/sketch on Arduino ADK #include <Max3421e.h> #include <Usb.h> #include

    <AndroidAccessory.h> AndroidAccessory acc("Manufacturer", "Model", "Description", "1.0", "", "0000000012345678"); Google-supplied C++ library USB and USB host libraries Create a new instance of the AndroidAccessory void setup() { acc.powerOn(); } void loop() { byte msg[0]; if (acc.isConnected()) { //send something to the Android app acc.write(msg, 1); //or read something int len =, sizeof(msg), 1); } } Note: You can get a fully working but barebones Arduino ADK sketch here: Convenience method that simply calls the powerOn() method in the Max3421e library. Continually check for connection to Android app Create data that the Android app can read Read data from Android app into msg variable
  36. Accessory Filter Resource Needs to match the Arduino sketch exactly

    In xml/accessory_filter.xml: <resources> <usb-accessory manufacturer="Manufacturer" model="Model" version="1.0" /> </resources>
  37. Android Manifest <manifest ...> <uses-sdk android:minSdkVersion="12" /> <uses-feature android:name="android.hardware.usb.accessory" />

    <application ...> <activity ...> <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" /> <intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/> </intent-filter> </activity> </application> </manifest> In AndroidManifest.xml (3.1+): From previous slide Use intent filter to launch this activity when ADK is plugged in
  38. Activity import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; //... UsbManager manager = (UsbManager)

    getSystemService(Context.USB_SERVICE); UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); ParcelFileDescriptor fileDescriptor = manager.openAccessory(accessory); FileDescriptor fd = fileDescriptor.getFileDescriptor(); FileInputStream inputStream = new FileInputStream(fd); byte[] buffer = new byte[16384]; int ret =; FileOutputStream outputStream = new FileOutputStream(fd); mOutputStream.write(buffer); In the .java file for your Activity (3.1+): Read data from the Arduino via a FileInputStream Write data to the Arduino via a FileOutputStream
  39. Additional Resources

  40. Electronics shopping • Electronics store listings (compiled by me)

    Adafruit and Sparkfun have invested a lot of time to make tutorials • Makertronic ( Go find Mike Eber around the BBQ tonight and thank you for a 10% discount code: BABBQ13
  41. Books • Programming Interactivity (2nd Edition) by Joshua Noble

    • Making Things Talk (2nd Edition) by Tom Igoe
  42. But we need more! Anyone want to help me

    with an idea? + + + + + + BLE
  43. Thank you! Google+: Twitter: @PearlChen Pearl Chen