Introduction: 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.
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.
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
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.
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
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.
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'
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.
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'
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.
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.
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.
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.
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.
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.
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 one-way 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)
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.
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 one-way 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)
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.
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.
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.
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.
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.