Go Down

Topic: Writing code and flash it on Android devices (Nexus 7) or Linux (Read 7 times) previous topic - next topic


Sep 20, 2012, 10:51 pm Last Edit: Oct 13, 2012, 04:28 pm by calli Reason: 1

I was succesfull to install all needed tools on my Google Nexus 7 to write and upload/flash Arduino Sketches.

Here I will try to get you going, please be kind if I make some errors, I tried quite a bit to get here so there may be quirks  ^_^

I will update this thread as needed.

What do we need:

- Android device with Android >= 4.0 (I use a Nexus 7 with 4.1.1 stock ROM)
- device needs to be rooted (Nexus Toolkit to do that)
- USB Host port on the device (often called USB OTG (on the go))
- the right module for your FTDI and your android architecture (ftdt_sio.ko I compiled it myself on a Ubuntu Linux) (see attachments for two versions, need to match your kernel) 
- USB OTG Cable
- Arduino UNO, Diecemilla or Pro Mini with FTDI Adapter
- [1] AVR Toolchain http://code.google.com/p/andavr/downloads/list
- busybox for Android (common Unix commands, see Market)
- [2] an Makefile: http://ed.am/dev/make/arduino-mk
- terminal Emulator App
- text editor (with c++ syntax highliting)
- Hackers Keyboard App (recommended!)

How it works


+ install busybox
+ install terminal app

From now on we use the terminal app. You can/should use the "su" command to make you work as root, there is no real user we can work as, so be carefull not to mess your device!

+ make an directory on your sdcard, I used "arduino/"
   - put the avr folder from [1] with the tools and includes there (delete bin/su here or it will clash with your system su!)
   - put the folders "hardware" and "libraries" from Arduino 1.0.1 here
   - make a directory with an example sketch, I uses "Blink/" with Blink.ino in it

+ copy the arduino.mk from [2] to the "arduino/" folder, it will be our template
   - we need now to make some changes to the makefile.mk
   - add the line SHELL:=/system/bin/sh in the beginning
   - define the defaults as on [2] documented
   - The busybox rm command don't know the -f param, so I deleted this param (search for "-f ").
   - The check for stty came up with "-f" but the system stty still wants -F, change the second echo also to "-f"
   - copy the makefile.mk to you example folder and rename it to Makefile

+ call make boards, it is in the avr/bin/ folder (e.g. /sdcard/arduino/avr/bin/make or ../avr/bin/make)
   - there should be now some output like this:
Code: [Select]

root@android:/sdcard/arduino/Blink # ../avr/bin/make boards
Available values for BOARD:
uno          Arduino Uno
atmega328    Arduino Duemilanove w/ ATmega328
diecimila    Arduino Diecimila or Duemilanove w/ ATmega168
nano328      Arduino Nano w/ ATmega328
nano         Arduino Nano w/ ATmega168
mega2560     Arduino Mega 2560 or Mega ADK
mega         Arduino Mega (ATmega1280)
leonardo     Arduino Leonardo
mini328      Arduino Mini w/ ATmega328
mini         Arduino Mini w/ ATmega168
ethernet     Arduino Ethernet
fio          Arduino Fio
bt328        Arduino BT w/ ATmega328
bt           Arduino BT w/ ATmega168
lilypad328   LilyPad Arduino w/ ATmega328
lilypad      LilyPad Arduino w/ ATmega168
pro5v328     Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
pro5v        Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
pro328       Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
pro          Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
atmega168    Arduino NG or older w/ ATmega168
atmega8      Arduino NG or older w/ ATmega8
root@android:/sdcard/arduino/Blink #

   - we now set the board we like to use: # export BOARD=atmega328

   - Now call make with no target
   A big stream of compile output should now fill your stream. If it ends with something like
Code: [Select]

/sdcard/arduino/avr/bin/avr-objcopy -O ihex -R .eeprom Blink.elf Blink.hex
rm Blink.elf

You are lucky ;-)

+ Fixing things up
   - most likely you (because of my bad description) forget to set some defaults in the Makefile -> fix it, see [2]
   - an error in util/delay.h about ceil etc can be fixed by adding a #include <math.h> in that file
+ Flashing the .hex
   - remember that ftdi_sio.ko? Do a "insmod ftdi_sio.ko" command (root needed here absolutely). Then connect your FTDI or your Arduino. Check if there is a "/dev/ttyUSB0" device file now!
   - do a "make upload" (should work if you did your homework from [2])
Code: [Select]

root@android:/sdcard/arduino/Blink # ../avr/bin/make upload

Uploading to board...
stty -F /dev/ttyUSB0 hupcl
/sdcard/arduino/avr/bin/avrdude -C /sdcard/arduino/avr/bin/avrdude.conf -DV -p atmega328p -P /dev/ttyUSB0 -c arduino -b 57600 -U flash:w:Blink.hex:i

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f
avrdude: reading input file "Blink.hex"
avrdude: writing flash (1106 bytes):

Writing | ################################################## | 100% 0.33s

avrdude: 1106 bytes of flash written

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

root@android:/sdcard/arduino/Blink #

Hope this gets you started! This is far from beeing error prone so far, test it and give feedback.

- the Makefile uses the gnu screen command to mimic the serial monitor, however I could not find a android port working for me
- you put the avr toolchain in the PATH variable (also from the Terminal App Preferences)
- you *can* work without beeing root after installation. Upload will not work, you can however change the permission for /dev/ttyUSB to "666" (this can be a security risk, and lasts only until the Board is removed)
- Adroid and Linux makes a different in the Case, so a EtherCard.h will only be found in EtherCard/ not in ethercard like on Windows
- it is not possible (as in the IDE, which seems to do some magic) to access a function before it was declared. Maybe you need to reshuffle your functions in the source
- to check the size use "make size", however it seems to differ a bit from the IDE, maybe some gcc optimisations could be used?
- a problem with using float math occured, somehow we need to convince the linker to use c math (http://arduino.cc/forum/index.php/topic,40215.0.html) we need to add that (my Makefile attached, you need to be logged in to get it)




This is impressive.  I also have a Nexus 7 and would like it to interface with my Arduino Mega ADK. I am thinking it would be helpful for it to be able to run Arduino sketches in it.  My knowledge gaps are huge and especially in the area of communication between my Nexus and the Mega. I have the USB Host of course, on the Mega, but really can't find much information on how to use it, can't find any example programs. I have the impression that serial communication over the USB is how it happens, but I sure would want an example or some kind of tutorial.

Do I need to load Arduino 1.01 for Linux on to the Nexus or will it use the hardware and libraries folders from the Windows 7 64 bit version (is that a bone-head question?)

If it would be helpful for me to explain what I am trying to accomplish I can do that.



Could you please upload your ftdi_sio.ko module for the nexus 7?


Oct 13, 2012, 02:30 pm Last Edit: Oct 13, 2012, 04:26 pm by calli Reason: 1

I am sorry Notification did not work for this thread.

@Protheus: I uploaded the modules for Android 4.1.1 and 4.1.2 for Tegra CPUs (Nexus 7 at least) to the origin Post (you need to be logged in here to see them)

@lloydvolson: I am not quite sure what you want to achieve here? There is a Arduino Kit for Android which you can use to communicate with Arduino Boards. In my thread I only describe how to write sketches on the Nexus Tablet, the original Arduino-IDE is only needed to get the libraries (source), no matter if it is Linux, Mac or Windows.


Go Up