Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hacking Arduino with Android

Pearl Chen
October 12, 2013

Hacking Arduino with Android

Presented at Big Android BBQ 2013 (#babbq13)

Pearl Chen

October 12, 2013
Tweet

More Decks by Pearl Chen

Other Decks in Technology

Transcript

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

    Arduino with Android Pearl Chen karma-laboratory.com Slides: go.klab.ca/babbq13
  2. go.klab.ca/babbq13 About me Technologist frontend web developer emerging technology hacker

    UX designer (former ‘artist’) HTML + CSS + JavaScript Android + Arduino/NFC + Educator
  3. go.klab.ca/babbq13 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...
  4. go.klab.ca/babbq13 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
  5. go.klab.ca/babbq13 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.
  6. go.klab.ca/babbq13 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
  7. go.klab.ca/babbq13 Need more detail? Check out the Sparkfun buying guide:

    https://www.sparkfun.com/arduino_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
  8. go.klab.ca/babbq13 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: https://www.sparkfun.com/arduino_guide
  9. go.klab.ca/babbq13 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
  10. go.klab.ca/babbq13 BeagleBone? What about... MaKeyMaKey? FLORA? mbed? PIC? netduino? Espruino?

    RFduino? Basic Stamp? plus all the other stuff showing up on Kickstarter every month???
  11. go.klab.ca/babbq13 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
  12. go.klab.ca/babbq13 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:
  13. go.klab.ca/babbq13 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
  14. go.klab.ca/babbq13 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)
  15. go.klab.ca/babbq13 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).
  16. go.klab.ca/babbq13 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.
  17. go.klab.ca/babbq13 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().
  18. go.klab.ca/babbq13 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.
  19. 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!
  20. go.klab.ca/babbq13 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
  21. klab.ca/spotlightandroid 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
  22. Basic firmware/sketch on Arduino ADK #include <Max3421e.h> #include <Usb.h> #include

    <AndroidAccessory.h> AndroidAccessory acc("Manufacturer", "Model", "Description", "1.0", "http://www.android.com", "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 = acc.read(msg, sizeof(msg), 1); } } Note: You can get a fully working but barebones Arduino ADK sketch here: iheartrobotics.com/2011/07/arduino-mega-adk-setup-notes.html 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
  23. 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>
  24. 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
  25. 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 = inputStream.read(buffer); 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
  26. go.klab.ca/babbq13 Electronics shopping • Electronics store listings (compiled by me)

    Adafruit and Sparkfun have invested a lot of time to make tutorials • Makertronic (makertronic.com) Go find Mike Eber around the BBQ tonight and thank you for a 10% discount code: BABBQ13