Introduction: Use Your Android Phone Sensors on the Arduino !

About: My name is Jonathan Rico, I'm 18 and I live in France.I'm in my first year of an electronics degree. And, the most important, I love robotics and electronics ! I'm currently learning ARM assembly and Android p…

Hello, this is my very first step-by-step instructable, and today, we're going to learn how the sensors in your android smartphone can communicate with your arduino.
In modern smartphones, there are a lot of useful sensors for an ingenious arduino project. I have a motorola DEFY, for example and it has (at least) 6 sensors :
- light sensor
- proximity sensor
- aGPS (assisted gps)
- compass/magnetometer
- accelerometer
- orientation sensor
This instructable's purpose is to get you started using these sensors from your arduino. For this, we're going to use an application that was made for that single purpose : Amarino 2.0 . This app was first started by Bonifaz Kaufmann during his visit at the High-Low Tech Group in 2009 at the MIT Media Lab in Cambridge, USA and further developed at the University of Klagenfurt in Austria.

The overall cost of this project, considering you already have an arduino and an android smartphone, can be pretty low (it cost me about 12$).

If you liked this instructable, please rate it !


Step 1: Required Parts/Tools

Required tools :

- solder iron with fine tip
- tiny solder wire
- good magnifying glass
- "Third hand" helping tool
- good soldering skills

Required parts :

- Arduino
- Android smartphone
- Bluetooth serial adapter (not needed if you happen to have an arduino BT)
- 10 and 20 kOhm resistors 
- Breadboard
- Standard servo motor
- jumper wires and/or small wires


Since the ArduinoBT has Bluetooth functionality built-in, setup is much easier (no soldering, risks of messing up bt adapter, etc...) but since it costs pretty much, I went the cheap way, and bought a bluetooth to serial adapter from ebay.

Okay, so where do you buy those parts/components...

- Arduino : you can get this at sparkfun : (http://www.sparkfun.com/products/9950 ), from ebay or you can check the arduino website for other distributors (http://arduino.cc/en/Main/Buy )

- Android smartphone (I guess you already have that...)

- Bluetooth serial adapter : You can get this from sparkfun too, with a breakout board (http://www.sparkfun.com/products/10269 ). These don't need level shifting so if you buy this product, you don't need to buy the resistors. You can also get it from ebay, like I did, but they are more complex to set up and documentation isn't always accurate (mine was wrong on the default baud rate, for example) Here's the link to the one I bought : Bluetooth adapter.

- 10kOhm and 20kOhm resistors : these are needed to shift the 5v from the arduino serial pin to the 3.3v on the bluetooth adapter. You can get these at your local electronics shop.

- Breadboard : Not really necessary, but it is much easier (and cleaner) to wire the voltage divider on a breadboard, than to solder it and have wires everywhere. The breadboard isn't required if you choose to get the arduino BT or the bluetooth module from sparkfun. You can get a breadboard on ebay or again at sparkfun.com

- Jumper wires/small wires : you can get this from sparkfun too, but you can also salvage small wires from old electronics to keep the costs down.

- Servo motor : Not really required, but used in this instructable for an example with the accelerometer sensor. I'm using the futaba s3003. You can get this at your local hobby shop, ebay, or sparkfun  .

Now It's the time to choose :
- you can go the pretty expensive, but also pretty easy way (no soldering) : go get an Arduino BT . (more than 100$)
- you can go the less expensive way, but less easier (a little soldering but no level shifting) : get a bluesmirf silver breakout board from sparkfun (approx. 40$)
- you can go the cheapest and hardest way (soldering AND level shifting required) : get a bluetooth module from ebay (less than 10$). 

In this instructable,I'm going to get you started with the Bluetooth module from eBay.

So make your choice, get the parts/tools, and head to Step 2. 

Step 2: Solder the Bluetooth Adapter

We are going to solder the necessary wires on the Bluetooth module :

- Grab the small wires
- Strip them a little bit
- Tin them
- Use the third hand tool to keep into place the Bluetooth module
- Solder carefully the four wires as shown in the photos (Tx, Rx, 3.3v, GND)

Let the Bluetooth module cool down a bit, and head over to the third step : The level shifter


Step 3: The Level Shifter

Note : If you bought a bluetooth module from sparkfun , skip this step, and head to step four.

Since the Arduino's serial output (Tx pin) is 5-volt logic, if we connect it directly to the Rx pin of the Bluetooth module - which is using 3.3-volt logic -  it will fry the Bluetooth chip. Fortunately, the Arduino supports 3.3-volt logic in reception (Rx pin), so no complex circuit is needed. All we have to build is a simple voltage divider :

- take your breadboard
- grab the resistors and some jumper wires
- connect one end of the 20k resistor to the ground
- connect the other end of the 20k to the 10k resistor
- connect these two ends to the Rx pin of the Bluetooth module
- connect the last end of the 10k resistor to the Tx pin of the Arduino (pin 1)
- check the photos to see if you've wired it correctly
- you're done

Now, head over to step 4 : Wiring everything

Step 4: Wiring Everything Else

So now that you've got your nice level shifter wired up, It's time to wire everything else :

- Connect the Bluetooth module's Tx pin to the Arduino's Rx pin (pin 0)
- Connect the Bluetooth module's 3.3v pin to the 3.3v supply on the Arduino (labeled 3V3)
- Connect the Bluetooth module's GND pin to one of the Arduino's GND pins
- Grab the servo and some other jumper wires
- Connect the servo's Red pin to the Arduino's 5v supply (labeled 5V, next to the 3V3)
- Connect the servo's Black pin one of the Arduino's GND pins
- Connect the servo's White/Yellow pin to Arduino pin 11
- Check the photo
- You're done !

Now the fifth step : The software setup


Step 5: Software Setup

Now that we've done the physical setup, it's time to work on the software side.

I've made some screen captures to help you out.

- Take your Android phone
- Install ES file explorer from the android market (any other file browser will do)
- Download the Amarino application
- Download the Amarino plugins
- Put those two files on your smartphone's SD/microSD card.
- Install Amarino and Amarino plugins
- Pair your bluetooth module
- Open the Amarino application
- Add the newly paired device
- Try to connect. If It connects, then good job !
- You're done with the android-side setup. Now onto the Arduino IDE :

- Download the Amarino library:
Amarino arduino library(updated)
Amarino Arduino Library(old)
- Extract it in your libraries folder :     arduino_install_dir/libraries/extract_meetandroid.zip_here
- Restart the Arduino IDE
- If everything went well, you can find the library under "Sketch->Import library"

Head to step 6 for the first example.

Step 6: First Example - the Compass Sensor

So everything is working, but why stay there ? The MeetAndroid arduino library is provided with some examples. Here's one using the compass sensor on the smartphone to blink a led on the arduino faster or slower depending on the phone's magnetic orientation. 

- Download the zip file here : http://www.box.net/shared/44kz63dc7a  . 
- Extract it in your arduino skecthbook directory
- Restart the arduino IDE
- Open the Arduino IDE
- Open "File->Sketchbook->Compass_Led"
- IMPORTANT : disconnect the bluetooth module's GND pin EACH TIME you want to upload a new sketch to your Arduino.
- Upload the sketch
- Plug the GND pin back in

- Now on the Android side : (again, check the screen captures)
- Open Amarino2.0
- Connect to the bluetooth module
- Add a new compass event.
- Turn your phone... and Whoa ! the led blinks faster or slower.

Now if you want a more advanced example, using the accelerometer and the servo, head to the final step !

Step 7: Last Example - the Accelerometer

Dealing with the accelerometer sensor isn't as easy as with the compass sensor. With the accelerometer, we're dealing with an array of floats for each axis. We're going to use another method to access these values. 

- Open "File->Sketchbook->Amarino_servo"
- Upload to the board (remember to disconnect the GND pin while uploading)
- Open the amarino2.0 app
- Remove the compass event
- Add an Accelerometer event
- Connect to the bluetooth module
- Let the magic begin !

You should get something like this :

Step 8: End

So thanks for following this instructable ! If you have questions/comments, please leave a comment ! I would also really like to thank the guys that made the Amarino application, which is very useful. Visit amarino-toolkit.net !

The next instructable is going to be about installing and using sl4a and python on android, with again, examples with the Arduino.

Now, get creative !