This is the first of a series of three posts. The ultimate goal is to setup a communication interface between an Arduino Uno/Mega board and an Android tablet over USB. Everything will be as user-friendly as possible, i.e. no root will be required on your Android tablet. The following posts Arduino USB transfers and Android USB Host + Arduino: How to communicate without rooting your Android Tablet or Phone conclude the series.
The Arduino Uno is a popular and affordable hardware platform which comes with its own IDE and core libraries for programming. If you like the Arduino IDE you may skip this post, however, if you feel more comfortable developing in Eclipse this might help you out.
We are using Ubuntu 11.10 (oneiric ocelot) and Eclipse 3.6.1 (helios), although this should not play an important role (as it turns out, Eclipse Indigo might need a few tweaks, check the end of this post). Also, setting up Eclipse for programming the Arduino Uno Atmega328P is pretty straightforward. Nevertheless, here is a quick how-to for the Arduino Uno (Arduino Mega 2560 below):
Install avrdude and AVR libraries:
sudo apt-get install gcc-avr avr-libc avrdude
- Start Eclipse and install CDT Plugin (C/C++ Development Tools):
- Help -> Install New Software…
- Work with: (your current Eclipse version)
i.e. “Helios – http://download.eclipse.org/releases/helios”
- Download the “pending” software package (don’t worry, download starts automatically 😉 )
- Choose “Programming Languages” and select “C/C++ Development Tools”
- Accept and continue by restarting Eclipse
- Install AVR Eclipse Plugin:
- Help -> Install New Software…
- Add new repository: http://avr-eclipse.sourceforge.net/updatesite/
- Re-download the “pending” software package, download will be faster since it is probably cached 😀
- Download AVR Eclipse Plugin and restart Eclipse
- Create new C project named “BlinkBlink”:
- Project Type AVR Cross Target Application (Empty Project, AVR-GCC Toolchain)
- Click next…
- Untick “Debug” (in Debug mode, no hex-files are generated and avrdude can’t flash the device)
- Click Advanced settings…
- AVR -> AVRDude -> Programmer configuration…
- Create a new programmer and name it “Arduino Uno”. Make sure this newly created programmer configuration is selected for the current project.
- Programmer Hardware: Arduino
- Override default port: /dev/ttyACM0 or similar
- Override default baudrate: 115200
- AVR -> Target Hardware:
- MCU Type: ATmega328P (or load from MCU)
- MCU Clock Frequency: 16000000 (default external clock source of Arduino Uno)
- Click Apply and OK to leave the properties window and click Finish to create the new project in the workspace.
- Create a new source file main.c and copy the contents from this link. Make sure to save main.c before proceeding (File -> Save).
- Project -> Build Project
- Click on the AVR Button within Eclipse to upload the generated hex file from BlinkBlink/Release/BlinkBlink.hex.
Your Arduino Uno’s LED should be blinking on and off repeatedly. If somehow it doesn’t work, right-click your Project and select Properties. Make sure all AVR and Programmer settings are active as mentioned above.
For the Arduino Mega 2560 you should choose Atmel STK500 Version 2.x firmware as Programmer Hardware, and ATMega2560 as target hardware, the rest is the same as with the Arduino Uno. Also, if you are using the above source file for testing, you should change the definition of LED from PB5 to PB7, since the LED on the Arduino Mega is on Pin7 of Port B.
Update: if you are using Eclipse Indigo, some specific AVR symbols such as DDRB (data direction register of port b) may not be recognized.
To solve this problem go to preferences…
- Language Mappings
- Add the following mappings:
- Content Type: C Header File / Language: GNU C
- Content Type: C Source File / Language: GNU C
- Make sure to click on Apply afterwards… also you may need to restart eclipse after adding the mappings
Your Language Mapping preference window should look like the following screenshot:
If it still does not work, also try adding this line at the beginning of your main.c source file: