You are on page 1of 14

Home Sign Up!

Browse Community Submit


All Art Craft Food Games Green Home Kids Life Music Offbeat Outdoors Pets Photo Ride Science Tech

Android G1 Serial To Arduino Robot


by mranalytical on January 24, 2010 Table of Contents Android G1 Serial To Arduino Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Intro: Android G1 Serial To Arduino Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 1: Items needed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 2: Android G1 with serial output enabled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 3: Install the Android Scripting Environment (ASE) with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 4: Copy and run the cellbot.py script to launch the Python program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 5: Telnet into the G1 and test sending it commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 6: Connect a 3.3v to 5v level shifter to the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 3 5 6 7 8 8

Step 7: Load the Cellbots program on the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Step 8: Run the whole process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Intro: Android G1 Serial To Arduino Robot


Learn how your Android cell phone can control a robot using an Arduino board. The phone takes commands via telnet from another phone or a PC, so you can control the robot remotely. This project will not otherwise modify the G1 so you can continue using it as your normal phone after undocking it. Why? By using cheap electronic components such as Arduino boards, you can pair them with your $400 phone to make an awesome robot. It would cost hundreds of dollars to add GPS, LCD's, motion sensors, wi-fi, cellular connections, speakers, and more to a hobby bot, but your phone already has these!

Together, the Android G1 and Arduino board allow you to use inexpensive electronics such as simple servos and sensors, to build powerful devices such as robots, remote telepresence, or fun toys for kids. More information at Cellbots.com . Notice: This project currently requires an Android G1 with root access to use serial output from the phone to the Arduino robot. You can add a $20 BlueTooth module to your Arduino board to have the phone talk to it over serial BlueTooth if you want to use the commercial Android software. Special thanks: We have the Hacker Dojo in Mountain View, CA to thank for putting us in touch, helping with some tricky issues via their mailing list of awesome members, and for having 74LS04 chips in stock. Most of the assembly was done at the Tech Shop in Menlo Park.

Image Notes 1. Goes to nothing

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Image Notes 1. Freeduino SB 2. Breadboard 3. HTC USB and phone off screen in this direction

Image Notes 1. This goes to pin 8 on the HTC USB board 2. Goes to pin 8 ground on HTC USB board 3. 5v power to the Arduino is at the other end of the breadboard and not in this picture 4. 5v Power 5. Jump pins 2 and 3 together 6. Connects to the RX pin 0 on the Arduino 7. Ground 8. Connects to ground on the Arduino at the other end of the breadboard out of this picture

Image Notes 1. These two are not used during this tutorial but I used them when testing serial to USB from the other linked instructable 2. Pins 7 and 8 are used to connect to your breadboard

Step 1: Items needed


To complete this tutorial you will need the following: Hardware: - Android G1 Dev Phone (or other Android device with root access and serial output) - Arduino (I'm using a Freeduino SB but any one should do) - 3.3v to 5v converter if you aren't using a 3.3v Arduino (I'm using a 74LS04 chip for under $1 but other options are available) - HTC USB break-out board for the G1 - Soldering equipment for two quick connections - A robot body with micro servos (cardboard, acrylic, treads, wheels, anything will do) Software: - Android Scripting Environment (ASE) - Telnet client for your PC (I'm using PuTTY on Windows) - Arduino development environment - (optional) Serial client for your PC (I'm also using PuTTY on Windows for this) - (optional) Android SDK

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

If you can follow instructions you can complete this tutorial with little knowledge of Python , Arduino, Android, or electronics. You'll want to know those things if you want to go beyond a blinking LED but this will get you started.

Image Notes 1. The TX0 pin of the HTC USB board connects here 2. Connect these two togther 3. This does to the RX pin 0 on the Arduino 4. Connect to ground 5. Connect to the 5v power from the Arduino

Image Notes 1. You can scan this with a barcode reader to download the apk file. Or go to the official project site.

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Image Notes 1. Use ddms from the Android SDK to take screen shots from your phone (among other tricks) 2. Use adb shell from the Android SDK to execute commands from the PC instead of typing them into the phone. 3. I used PuTTY on Windows to telnet into the phone.

Image Notes 1. We use the Android Dev Phone 1, also known as the G1 in this tutorial but ADP2 should also work in theory.

Image Notes 1. Android Scripting Environment (ASE) logo

Step 2: Android G1 with serial output enabled


G1's do not ship with the ability to send serial commands out of the USB port and there is no native option to enable it. If you are an advanced Android SDK user you could make your own build but I chose to use the Cyanogenmod 4.2.13 . If you know of other Android devices with serial out working, add them to the comments. Optionally, you can follow the links there to this instructable on talking to your G1 over USB from your PC. That serial to USB connection isn't required for this tutorial but I used it as a nice sanity check to verify the phone was sending serial out. If you don't use the other intructable to verify serial output is working, you can try this simple check: 1. Open the Terminal app on the G1 (comes with Cyanogen but download one from the Market if you have a different image) 2. Navigate to the /dev/ directory by typing in cd /dev/ 3. Type ls (that's an L) and look for ttyMSM2 in the list returned The Python script we'll use later sends commands to '/dev/ttyMSM2' in order for them to go out the serial connection. Since that requires root access, you'll need to change permissions on that every time you reboot the phone. To do that: 1. Open the terminal app on the phone 2. Enter 'chmod 777 /dev/ttyMSM2'

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Then you can run the Python script from the Android Scripting edit in the next step and it will have access to send serial output.

Image Notes 1. We use the Android Dev Phone 1, also known as the G1 in this tutorial but ADP2 should also work in theory.

Image Notes 1. If you don't see this the phone does not have serial out enabled.

Step 3: Install the Android Scripting Environment (ASE) with Python


The scripts we'll be using to create an open socket connection on the phone and send commands out is written in Python. To run this on the G1 we'll need the Android Scripting Environment . If you don't see it in the Market you can download it by scanning the barcode on that page which links to the apk file on this page . Once you install and run ASE you will want to add on the Python module from the menu: 1. Open ASE and make sure you have a working internet connection (wi-fi or 3G) 2. Press the menu button on the phone and select Interpreters 3. Press menu again and select Add 4. Select Python (currently v2.6.2 as of writing this) and it will download some zip files You may want to explore creating, opening, editing, and running scripts to become familiar with Python via ASE but it isn't required.

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Image Notes 1. Android Scripting Environment (ASE) logo

Image Notes 1. You can scan this with a barcode reader to download the apk file. Or go to the official project site.

Step 4: Copy and run the cellbot.py script to launch the Python program
This tutorial uses a Python script to be the "brains" of the robot. Get the latest code from our open source Google Code project . You only need the cellbot.py file but others may help with various things you want to explore. I simply plugged the phone into my PC's USB connection and mounted the drive before copying the file to /sdcard/ase/scripts. The program creates an open socket connection to accept an incoming telnet session. It also prints the received commands to the screen while sending them out the serial port. Put this file on the phone's SD card in the /ase/scripts/ directory. Detailed steps to load and run the scripts: 1. Copy the cellbot.py script to the SD card's /ase/scripts/ directory 2. Be sure to dismount the SD card from your PC if you copied them that way since the phone can't access the files at the same time your PC is. 3. Open the Android Scripting Environment app 4. Click on cellbot.py to launch it You should see a confirmation that the device is ready at this point to accept incoming telnet sessions on port 9002. Tip: Be sure to run the "chmod 777 /dev/ttyMSM2" command from step #3 first. See step #5 for finding the phone's IP address.

Image Notes 1. This is where the received commands get sent out the serial port

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Step 5: Telnet into the G1 and test sending it commands


The phone should be ready for you to telnet into it and send it commands from your PC. It will print them to the phone's screen to confirm what it receives. I used PuTTY on Windows but we've confirmed that minicom works great on Macs as described in this instructable . You will first need to find your phone's IP address. This is available by going to Menu > Settings > Wireless controls > Wi-Fi settings and then pressing the currently active connection. A pop-up toast message will come up with the current IP address on the local network. Write this down as you'll use this every time you want to open a telnet session from your PC. IP assignments typically expire after a certain number of days so you may need to check this again. Note: This tutorial assumes your PC and phone are on the same local network. Routing to the phone from outside of the local network should be possible but is not covered here. Open your telnet client of choice and connect to the IP of the phone on port 9002. From a command line you do this as "telnet 192.168.1.1 9002" using the actual IP of the phone. Type in some characters and hit enter to see them show up on the phone's screen. You can type a q to cause the Python script to quit, which should close your terminal session. If for any reason you can't connect via telnet and need to kill the program, a simple reboot of the phone should do the trick. Advanced users may want to find the process ID via ps and then use kill to stop it. Advanced: A future version of this could run a local web server from the phone rather than accepting commands via telnet. We're also exploring XMPP to chat with your robot.

Step 6: Connect a 3.3v to 5v level shifter to the Arduino


The Arduino used in the this tutorial is a 5v model so we need to convert the 3.3v signal coming out of the G1 using a level shifter. It should be possible to connect directly to a 3.3v Arduino but that was not something I tested. There are several ways to approach this but we'll use a 74LS04 chip in this example. You can search for one here and they are likely under $1. Tim and I picked ours up from the Hacker Dojo in Mountain View , CA but these are VERY common and should be plentiful wherever chips are sold or donated. At a high level we're simply going to send the TX signal from the HTS USB breakout board into pin 1 of the 74LS04 chip. To make it work we go through the chip twice and come out pin 4 to the RX pin on the Freeduino SB (your serial pin may be different if you have another Arduino board but all should support this). Follow these steps to wire up the level shifter and connect the HTC USB board (do not plug it into the phone yet and unplug power to the Arduino): 1. Insert the 74LS04 chip onto your breadboard. Make sure the chip hurdles the center break so the pins aren't shorted (a dumb move I made at first) 2. Soldier two wires to the HTC USB board as described in this instructable , but we'll only be using pins 7 (Ground) and 8 (TX0) since we're only doing oneway transmission for this tutorial. 3. Connect the other end of the ground (pin 7) wire to a ground on your breadboard (which should be connected to a ground on your Arduino) 4. Connect the other end of the TX0 (pin 8) wire to the breadboard where it runs into pin 1 of the 74LS04 chip. (do an image search for a full diagram of the chip)

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

5. Use a wire to connect pin 2 and 3 of the chip 6. Connect pin 4 of the chip to the Arduino RX point (pin 0 on the Freeduino SB and Arduino Duemilanove) 7. Connect pin 7 (GND) on the chip to the ground for your breadboard (which also connects to the Arduino ground) 8. Connect pin 14 (VCC) to the 5v power on your breadboard (which gets the power from the Arduino 5v output) You should now be ready to plug in the HTC USB break-out board into the bottom of the phone and power on the Arduino. Check for sparks and smells and touch things to make sure they are cool. Note: The current cellbot code turns on LED #13 when the servo motors of the robot should be running. If you don't have a robot yet you can check to see that the LED turns on and off to confirm it is working.

Image Notes 1. This goes to pin 8 on the HTC USB board 2. Goes to pin 8 ground on HTC USB board 3. 5v power to the Arduino is at the other end of the breadboard and not in this picture 4. 5v Power 5. Jump pins 2 and 3 together 6. Connects to the RX pin 0 on the Arduino 7. Ground 8. Connects to ground on the Arduino at the other end of the breadboard out of this picture

Image Notes 1. Goes to nothing

Image Notes 1. These two are not used during this tutorial but I used them when testing serial to USB from the other linked instructable 2. Pins 7 and 8 are used to connect to your breadboard

Image Notes 1. Not used 2. Used

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Image Notes 1. Freeduino SB 2. Breadboard 3. HTC USB and phone off screen in this direction

Image Notes 1. 5v power 2. Ground 3. Connects to pin 4 on 74LS04 chip 4. Optional - connects to external LED but the onboard LED would suffice for this tutorial. 5. I'm using external 9V power but you could use another voltage in the Arduino's 7-12v range, or use power over USB (but that doesn't demonstrate the coolness of being disconnected from the PC) 6. USB plugged in during this picture but unplugged when fully running.

Image Notes 1. The TX0 pin of the HTC USB board connects here 2. Connect these two togther 3. This does to the RX pin 0 on the Arduino 4. Connect to ground 5. Connect to the 5v power from the Arduino

Step 7: Load the Cellbots program on the Arduino


Get the Arduino source code from our Google Code project site . Open the Cellbots.pde Arduino program in the Arduino editor. Then push it to your Arduino board. You can test talking to the Arduino code by opening the serial monitor in the Arduino editor. This is a great way to test your robot or device by talking directly to the Arduino before hooking everything up to the phone. The code sends serial commands back to the serial monitor to confirm what it is doing even if you don't have your robot built yet. Note: You can't load programs onto the Arduino while the wire is connected to the RX serial input pin. So you might want to put this one a switch but I simply unplugged it when I needed to load a new program.

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Image Notes 1. You should be able to control the LED from the Arduino serial monitor without having the phone plugged in. Image Notes 1. Type commands in here to test sending ASCII characters over serial to the Arduino

Step 8: Run the whole process


You should be able to connect the HTC USB board to the phone, fire up the cellbot.py file in ASE, and open a terminal sessions into the phone. Type "H" to have the phone say hello or any other commands from the README.txt file. A "q" will quit the Python script on the phone and close the terminal socket. Here is a recap of how it all works: 1. The Python script opens a socket to accept incoming telnet connections and echo the commands out the serial port 2. We connect from our PC to the phone via telnet on port 9002 and send it commands that we see on screen 3. The HTS USB board interfaces with the G1's USB port and sends the 3.3v signal into the 74LS04 pin 1. 4. The signal comes out of the chip on pin 2, goes back in on pin 3, and comes out again on pin 4 at 5v 5. Our Arduino accepts the serial signal on RX pin 0 and process it via the Cellbot.pde program 6. We can type 'q' to kill the Python script and close the telnet connection Now that you've completed this very complicated process to send basic commands to an Arduino robot, it is time for you to hack it to be more awesome! We don't yet have 2-way serial working so the Arduino can't send commands back into the phone but we're working on that. Stay current by subscribing to our blog at Cellbots.com . About the Authors: Tim Heath Lead researcher who first put this process together at the Tech Shop in Menlo Park, where he is a member. Ryan Hickman Programmer who worked on the Python and Arduino code and authoered this Instructable.

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Image Notes 1. Use ddms from the Android SDK to take screen shots from your phone (among other tricks) 2. Use adb shell from the Android SDK to execute commands from the PC instead of typing them into the phone. 3. I used PuTTY on Windows to telnet into the phone.

Image Notes 1. Goes to nothing

Related Instructables

iAndroidRemote - Control Android mobile using an Apple Remote by sudar

Android G1 Dance Pad with Serial to USB Color LED using Cable by Arduino by macpoddotnet woshialex

Tweet-a-Pot: Twitter Enabled Coffee Pot by frenzy

Arduino XMAS hitcounter by alex_weber

Snow Clock (Arduino Based) by insingertech

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

Comments
20 comments Add Comment

mertaxoy says:
is it possible with HTC Wildfire. Wildfire has another USB break-out.. Can you please help me about this.,

Feb 5, 2011. 4:10 AM REPLY

rex358 says:

Dec 14, 2010. 4:15 AM REPLY hey, can i run my camera app that i have built using the ecclipse , the same time when this appp on python is running....illl be glad if some body helps me on this Dec 12, 2010. 7:56 PM REPLY For the newer generations of android electronics (phones and tablets) including the Droid. They come with USB host already built in> Could i use the same programing cable for my arduino to connect to an android tablet? If i did, what app would i need to accomplish a serial port to the arduino? Chow.

Chowmix12 says:

wareneutron says:
it not goes nothing?

Nov 26, 2010. 12:37 AM REPLY

radhoo says:

Nov 1, 2010. 4:14 PM REPLY One of my last projects was a 4x4 differential drone, built from scratch, remotely controlled via Bluetooth using an Android smartphone. Schematics / demo video here: http://www.pocketmagic.net/?p=1398 Besides ultrasonic sensors, the robot is about to be equipped with a homemade geiger counter sensor to detect "exotic" environment parameters like the dangerous ionizing radiation. There are quite a few possibilities since the robot has an ATmega microcontroller that can easily support additional sensors. The data is sent back to the Android device, via bluetooth.

carterson2 says:
Can I buy a kit?

Jul 25, 2010. 7:23 PM REPLY

wenjiun says:
You can also choose the latest Super D 1.10.2 as well while older version 1.9.3 does not support the feature.

Apr 3, 2010. 5:58 AM REPLY

roadog says:
COOL!I wanna try it!

Apr 2, 2010. 1:53 AM REPLY

christian2gothic says:
is it necessary for the phone on the bot to have service turned on??

Mar 27, 2010. 8:49 PM REPLY

are there ways to control the bot through satellite instead of using cellphone service. that way you wouldn't be dependent on a service. Is there any way to bypass the use of cell phone towers all together???

mranalytical says:
We typically test with phones that have no SIM card and use wi-fi for local communications.

Mar 28, 2010. 6:28 PM REPLY

christian2gothic says:
what if you wanted the bot to travel across several states? and i've been looking into flying bots, it's been hard to find anything here on instructables. how hard is it to send live video and sound feed from the bot to your PC?? i'm trying to gather info for a different project i'm working on??

Mar 28, 2010. 7:51 PM REPLY

mranalytical says:

Mar 30, 2010. 11:21 AM REPLY We just added XMPP support so as long as the phone has a data connection (any kind will do), you can command it from anywhere.

Feel free to start a thread here with ideas: http://groups.google.com/group/cellbots

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

captFuture says:

Jan 27, 2010. 7:43 AM REPLY Thanks for this nice Tutorial - I can confirm that the latest kernels of Cyanogen have Serial enabled. (I'm personally using a HTC Magic - not a G1) I did a bit of research as well and found out that Arduino sees 3,3V from the Phone as valid "high" state, so you would not need the 74LS04 in between for sending the data to the arduino. This is of course different in the other direction - there you have to reduce from 5V to 3,3V :) Cheers

mranalytical says:

Jan 28, 2010. 1:36 AM REPLY Great find captFuture. I'll update the instructable to reflect that. Still trying to get two-way working but the issue isn't hardware so much as making an incoming serial socket using Python when pySerial isn't in ASE.

captFuture says:
Maybe it would be an option to do it in java - some guys are developing a serialport API for the SDK. code.google.com/p/android-serialport-api/

Jan 29, 2010. 3:39 AM REPLY

rice103 says:
but you need the serialport dll..... can you send me?? i can't compile it!!! (rice103@gmail.com)

Mar 1, 2010. 5:16 AM REPLY

mranalytical says:

Mar 1, 2010. 9:06 AM REPLY You'll have to reach out to the Android Serialport API project team for the code they are using. My work has been using Python on Android via the Android Sripting Environment.

tim_programmer says:

Jan 27, 2010. 5:49 AM REPLY Good to see someone else looking at this... I'm planning on interfacing to an AVR soon for a couple of projects soon (one is a universal IR remote - sending serial commands to the AVR which flashes an IR LED... the other involves the USB host support on the AT90USB1287.. but that ones a secret ^_^)

Jorad says:
That is pretty awesome.... perhaps programming on the android to the arduino is on the horizon?

Jan 26, 2010. 12:57 AM REPLY

mranalytical says:

Jan 26, 2010. 2:34 PM REPLY That could certainly be possible. If not programming on the phone it would certainly be nice to send new programs remotely to it instead of plugging it in each time. Glad you like the tutorial and hope you make something cool with it.

http://www.instructables.com/id/Android-G1-Serial-To-Arduino/

You might also like