Slide 1

Slide 1 text

Google+: klab.ca/+ Twitter: @PearlChen (or the other way around?) Hacking Arduino with Android Pearl Chen karma-laboratory.com Slides: go.klab.ca/babbq13

Slide 2

Slide 2 text

go.klab.ca/babbq13 About me Technologist frontend web developer emerging technology hacker UX designer (former ‘artist’) HTML + CSS + JavaScript Android + Arduino/NFC + Educator

Slide 3

Slide 3 text

go.klab.ca/babbq13 My first hardware project

Slide 4

Slide 4 text

go.klab.ca/babbq13 More projects

Slide 5

Slide 5 text

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...

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

go.klab.ca/babbq13 Arduino basics

Slide 8

Slide 8 text

go.klab.ca/babbq13 Read about all the Arduino boards: http://arduino.cc/en/Main/Boards What is Arduino? It’s hardware (or rather, a family of hardware)

Slide 9

Slide 9 text

go.klab.ca/babbq13 The Arduino IDE. Download the latest here: http://arduino.cc/en/Main/Software What is Arduino? And it’s software!

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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???

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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:

Slide 18

Slide 18 text

go.klab.ca/babbq13 The 4 basic “modes” Digital Input Digital Output Analog Output Analog Input

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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)

Slide 21

Slide 21 text

go.klab.ca/babbq13 analog pins for analog outputs • Analog output pins (~): 3, 5, 6, 9, 10, 11

Slide 22

Slide 22 text

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).

Slide 23

Slide 23 text

go.klab.ca/babbq13 analog pins for analog inputs • Analog input pins: A0, A1, A2, A3, A5, A5

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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().

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

go.klab.ca/babbq13 example Android+Arduino projects and other hardware

Slide 28

Slide 28 text

go.klab.ca/babbq13 by Cute Circuit ⇪ A-Nerve

Slide 29

Slide 29 text

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!

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

klab.ca/spotlightandroid Android Development Kit (ADK) Arduino Mega ADK

Slide 33

Slide 33 text

klab.ca/spotlightandroid More Android Development Kits! SparkFun IOIO ODroid ADK Microchip PIC24F ADK

Slide 34

Slide 34 text

Are there wireless ADKs? Bluetooth Shields IOIO w/ USB Bluetooth dongle Coming soon? ADK2012

Slide 35

Slide 35 text

Basic firmware/sketch on Arduino ADK #include #include #include 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

Slide 36

Slide 36 text

Accessory Filter Resource Needs to match the Arduino sketch exactly In xml/accessory_filter.xml:

Slide 37

Slide 37 text

Android Manifest In AndroidManifest.xml (3.1+): From previous slide Use intent filter to launch this activity when ADK is plugged in

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

go.klab.ca/babbq13 Additional Resources

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

go.klab.ca/babbq13 Books • Programming Interactivity (2nd Edition) by Joshua Noble • Making Things Talk (2nd Edition) by Tom Igoe

Slide 42

Slide 42 text

go.klab.ca/babbq13 But we need more! Anyone want to help me with an idea? + + + + + + BLE

Slide 43

Slide 43 text

go.klab.ca/babbq13 Thank you! Google+: klab.ca/+ Twitter: @PearlChen Pearl Chen karma-laboratory.com Slides: go.klab.ca/babbq13