Using Eclipse with Arduino Duemilanove

The Arduino IDE is good for simple projects, but as soon as you start building more complex multi file projects it just not up to the job. Eclipse on the other hand is free, powerful, and full-featured development environment that can be set up to work with AVR and Arduino. Arduino’s website has some decent instructions on how to compile with Eclipse for Arduino but its has some missing/confusing steps and is meant for C development not C++. It still has some good reference material that you should review.

In this tutorial we will be showing you how to set up the eclipse environment, Build the Arduino library, Create a Arduino project, Compiling C/C++ code, Uploading code to the Arduino, FAQ. This tutorial was created in response to VHS Hack Challenge.

Setting up the Eclipse environment

Download
You may not be able to find the exact versions that are used in this tutorial, just you the most updated versions.

Installation
  • Install Java runtime. Most computers will already have Java installed as it is used for many other applications.
  • Install Arduino software. The Arduino software comes with the Arduino library and source code examples that will be useful when developing your own projects.
  • Install Eclipse. This should be pretty straight forward. You can install Eclipse in any directory you want. For this tutorial we installed it in C:\Dev\eclipse-cpp
  • Install WinAVR, You can install WinAVR in any directory you want. For this tutorial we installed it in C:\Dev\WinAVR-20100110
  • Install Eclipse AVR plugin. You will have to manually copy this “plugin” into the the Eclipse installation. Move the folder which begins with the name “de.innot.avreclipse” to the “dropins” folder located within the eclipse folder. Restart Eclipse if you have it already running. There should now be a new “AVR” toolbar button in your workspace.

Build the Arduino library

Before we can create our first Arduino project we have to link in the Arduino library for Eclipse. There are several different ways of doing this described on Eclipse for Arduino tutorial but for this tutorial we will be creating a static library.

You can download the project and the library directly and skip this step.

  1. Open Eclipse and start a new C++ project.
  2. C++ Project
    • Project Name: ArduinoCore
    • Project Type: AVR Cross Target Static Libary
    • Toolchains: AVR-GCC Toolchain
  3. Select Configurations, Uncheck debug we will not be using the debug version of this library in this tutorial.
  4. Set the MCU type and frequency for your Arduino board. For example, for the Diecimila, use ATmega168 running at 16000000 Hz. For new versions of the Duemilanove, use ATmega328P running at 16000000 Hz. If you look closely at the Arduino board, you can see the target platform written on the main processor. Click Finish.
  5. In Eclipse, click Project->Properties.
  6. Select C/C++ Build and expand the category (e.g., click the diamond to the left of “C/C++ Build”).
  7. Select Settings under C/C++ Build.
  8. In the right pane, Click AVR Compiler
    • Debugging. Set “Generate Debugging Info” to No debugging info.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  9. In the right pane, Click AVR C++ Compiler
    • Debugging. Set “Generate Debugging Info” to No debugging info.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  10. Right click on the ArduinoCore project in the project explorer on the right and select Import.
  11. On the Import dialog, select General => File System and click next.
  12. On the File system dialog click browse and select the Arduino IDE’s hardware folder  “Arduino-0018\hardware\arduino\cores\arduino”
  13. Select all the files except main.cpp. Click finish
  14. Build the project, On the mail dialog click the hammer in the too bar or by selecting Project =>Build All.

Congratulations you have created the Arduino C/C++ library for Eclipse.  The library ( libArduinoCore.a ) can be found in the projects release folder. It should be around 26k

Path: \workspace\ArduinoCore\Release\libArduinoCore.a

Creating a Arduino project

Now that we have the Arduino library we can build our first Arduino project. We are going to start with the Blink tutorial from Arduino.cc its one of the simplest tutorials and should be a good building block for future projects.

You can download the project for this tutorial from our website.

  1. Create a new Eclipse C++ project, File => New => C++ project
  2. C++ Project
    • Project Name: Blinky
    • Project Type: AVR Cross Target Application => Empty project
    • Toolchains: AVR-GCC Toolchain
  3. Select Configurations, Uncheck debug we will not be using the debug version in this tutorial.
  4. Set the MCU type and frequency for your Arduino board. For example, for the Diecimila, use ATmega168 running at 16000000 Hz. For new versions of the Duemilanove, use ATmega328P running at 16000000 Hz. If you look closely at the Arduino board, you can see the target platform written on the main processor. Click Finish.
  5. In Eclipse, click Project =Properties.
  6. Select C/C++ Build and expand the category (e.g., click the diamond to the left of “C/C++ Build”).
  7. Select Settings under C/C++ Build.
  8. In the right pane, Click Additional Tools in Toolchain
    • Check Generate HEX file for flash memory
    • Check Print size
  9. In the right pane, Click AVR Compiler
    • Debugging. Set Generate Debugging Info to “No debugging info”.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  10. In the right pane, Click AVR C++ Compiler
    • Debugging. Set Generate Debugging Info to “No debugging info”.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  11. In the right pane, Click AVR C++ Linker
    • Command: avr-gcc
    • Command line pattern: ${COMMAND} –cref -s -Os ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lm ${FLAGS} *NOTE — is two short dashes (- – with out the space) not a single long dash, WordPress auto converts two short dashes to a single dash
  12. In the right pane, Click AVR C++ Linker => Libraries
    • Add your Arduino library created above “ArduinoCore“, The name of the library does not include the extension or the pretext of ‘lib’.  Note: In this project I copied the library in to the base folder of the Blinky project for simplicity.
  13. Create a new source file,  File => New => Source file, Call it main.cpp
  14. Copy the contents of this file main.cpp in to main.cpp
  15. Build the project, On the mail dialog click the hammer in the too bar or by selecting Project =>Build All.

Uploading code to the Arduino

Lucky there is already a tool for this built in to AVR plugin for Eclipse called AVRDude. It should already be included with the AVR plugin for Eclipse so you shouldn’t have to download or install anything else.

  1. In Eclipse, click Project =>Properties.
  2. On the left hand side AVR => AVRDude
  3. You will need to create a new programmer configuration for your board but you only have to do this once.  Click New
  4. New programmer configuration
    • Configuration Name: Arduino Duemilanove
    • Programmer Hardware: Atmel STK500 Version 1.x Firmware
    • Override default port: //./COM14
      Note:  you will have to change this to your comport.)
    • Override default baudrate: 57600
  5. Click OK,
  6. On the AVRDude dialog select Advanced
  7. Check “Disable Device signature check” and click OK
  8. Highlight Blinky in the project explorer and click AVR => Upload to target device or the AVR icon with the green downward arrow.
  9. The results will be printed in the console.

Launching C:\Dev\WinAVR-20100110\bin\avrdude -pm16 -cstk500v1 -P//./COM14 -b57600 -F -Uflash:w:Blinky.hex:a
Output:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.05s

avrdude: Device signature = 0x1e950f
avrdude: Expected signature for ATMEGA16 is 1E 94 03
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file “Blinky.hex”
avrdude: input file Blinky.hex auto detected as Intel Hex
avrdude: writing flash (1158 bytes):

Writing | ################################################## | 100% 0.61s

avrdude: 1158 bytes of flash written
avrdude: verifying flash memory against Blinky.hex:
avrdude: load data flash data from input file Blinky.hex:
avrdude: input file Blinky.hex auto detected as Intel Hex
avrdude: input file Blinky.hex contains 1158 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.52s

avrdude: verifying …
avrdude: 1158 bytes of flash verified

avrdude done. Thank you.

Congratulation you have uploaded your source code to the Arduino.

FAQ

Q: I keep getting this error message when I upload code with AVRDude to the Arduino.

avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude done.  Thank you.

A: Disconnect and reconnect your Arduino and try again

More help

For more help with setting up Eclipse to work with Arduino see these two posts on the subject

If you have any other questions or suggestion on how to improve this article, please leave a comment.

Update:

DubiousTech:
First error I got was with the linker command line pattern. I got a compiler error “avr-gcc.exe: –cref: No such file or directory”. It looks like the “–cref” should be “–cref”. After changing that I got a different error saying “ld.exe: cannot find -lArduinoCore”. Eventually, after trying just about everything I could think of I discovered that I needed both the name of the library in the “-l” box and the file location in the “-L” box. This might very well be a simple mistake, but it’s not clear to a novice.

Steve:
You’ve got to update this post so the dash in front of “cref” is actually two dashes