Arduino in Eclipse

This tutorial describes how to set up Eclipse for Arduino (AVR) C/C++ development. Eclipse is a full-featured Integrated Development Environment with modern editor features such as syntax highlighting, code completion and error checking. I became interested in using Eclipse for Arduino development as I’m often simultaneously developing firmware for the chip and visualization software in a Java / Processing applet. Since I’m already using Eclipse for Java / Processing, it’s nice to be able to work in the same environment for both.

The Arduino site has a decent walkthrough, but I found some of the steps to be confusing or outdated. Hopefully, this will get you up and running quickly.

Contents

Overview

Most of this setup will have to happen once, then starting new projects is typically as easy as duplicating a simple, skeleton project. Getting the paths and configuration options correct for this initial project was the most confusing for me, but I haven’t needed to tinker with them again.

To begin, download Eclipse for C/C++ Developers, or install the C/C++ plugin for your existing Eclipse version (these instructions assume Eclipse 3.5 Galileo). To install an Eclipse plugin:

  • install newOpen Eclipse. Navigate to Help > Install New Software…
  • Select CDTSelect CDT from the drop down menu. If CDT is not present, then click the add button and paste the link to the plugin update site in the Location field: http://download.eclipse.org/tools/cdt/releases/galileo
  • Select Eclipse C/C++ Development Tools, then click “Next >” and “Finish” on the following screen. This may prompt you to restart Eclipse, however select No so we can install the next plugin.
  • AVR pluginAVR tools. You should install tools appropriate for your platform:
    • Mac OS X: CrossPack (Note: I’m targeting this tutorial for an OS X system)
    • Linux: apt-get install avrdude binutils-avr gcc-avr avr-libc gdb-avr
    • Windows: WinAVR

Setup Eclipse

AVR Paths

After the plugins have been installed they need to be configured. Specifically, the paths to the AVR Tools need to be verified.

  • Open the Eclipse Preferences: Eclipse > Preferences. Twirl down the AVR category and select “Paths”. Verify that the paths have located the correct AVR toolkit (CrossPack-AVE on OS X). You can edit these fields as necessary.
  • AVRDudeSelect the AVRDude category. I’ve found that I need to add a Programmer configuration for each Arduino (or USB to Serial adapter) that I use. Please let me know if you can get a better solution working with wildcards.
    • Give this configuration a descriptive name. I’m using “Arduino Diecimila”.
    • Select “Arduino” as the Programmer Hardware
    • Enter the correct port for your Arduino. You can display this by plugging the Arduino into the computer, opening the Terminal then typing: /dev/tty.usbserial then hitting the Tab key. The remainder of the port name should appear.
    • Set the baud rate to 19200
    • Click OK. Then close the preferences.

Create an Arduino project

For the first project you’ll need to manually set up the configuration. I’d *highly* recommend making a basic blink sketch, then duplicate it for each new project. This is what the tutorial will set up. Alternatively, you can try importing this basic project, but may have to set up the paths for your system anyway. Eclipse Blink Sketch

  • Go to File -> New -> C Project
  • Select Empty Project under AVR Cross Target Application and name the project “Hello_Blink”.
  • Click Finish.
  • Select the “Hello_Blink” project in the Project Explorer panel, then navigate to Project > Properties menu.
  • Twirl down the AVR category and select AVRDude.
    • Select your Arduino from the Programmer configuration menu
  • Select Target Hardware at left.
    • Ensure that your Arduino is plugged into your computer via USB (and is powered on).
    • Click “Load from MCU”. This should query the Arduino for it’s type and detect it correctly.
      • If this doesn’t work, typically the Diecimila is an ATmega168 while the Duemilanove is an ATmega328

Each Ardiuno project needs to include the Ardunio core library. This file is compiled automatically by the Arduino IDE. While you could compile it yourself in Eclipse, the easiest way to get it is to build a simple sketch in the Arduino IDE, then copy the “core.a” file into your Eclipse project.

  • core.a fileStart the Arduino IDE
  • Open the Blink sketch (File > Examples > Digital > Blink)
  • Click the “Verify” button to compile the sketch
  • Select Sketch > Show Sketch Folder
  • Drag core.a into your Eclipse Hello_Blink project to copy it in.
  • Right-click on core.a, select Rename and change it to “libcore.a”

Now we set up the correct building settings:

  • Twirl down C/C++ Build and select Settings
  • Select “Additional Tools in Toolchain”
  • Check the following:
    • Generate HEX for Flash Memory
    • Print Size
    • AVRDude
  • Uncheck Generate Extended Listing
  • Select AVR Compiler
    • Change the command from avr-gcc to avr-g++
  • include pathSelect AVR Compiler > Directories
    • Add an Include Path to the header files within the Arduino.app bundle (look at the screenshot)
    • You can right-click on Arduino.app and select Show Package Contents, then navigate to the Contents/Resources/Java/hardware/cores/arduino folder
  • Select AVR Compiler > Debugging
    • Set “Generate Debugging info” to “No debugging info”
  • Select AVR Compiler
    • Set the Optimization level to Size Optimizations
  • Select AVR Assembler > Debugging
    • Set “Generate Debugging info” to “No debugging info”
  • core libSelect AVR C Linker > Libraries
    • Add a library named “core”
    • Add a library path. If libcore.a is in the Hello_Blink project folder, the path will be “${workspace_loc:/Hello_Blink}”

Add a new main.c source file

This file will contain your typical Arduino sketch code. There are additional bits of code that are typically added automatically by the Arduino IDE which need to be explicitly written in Eclipse. Feel free to copy the below code.

  • Go to File > New… > C Source File. Name this file main.c
  • Paste in the following for a basic blink sketch:
#include "WProgram.h"

// prototypes
void blink(int n, int d);

// variables
int ledPin = 13; // LED connected to digital pin 13

void setup(){
Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
pinMode(ledPin, OUTPUT);      // sets the digital pin as output
Serial.println("------------------");
Serial.println("   hello world");
Serial.println("------------------");
}

void loop(){

  blink(3, 50); // blink led (n times, ms interval)
  delay(1000); // wait 1 sec
}

void blink(int n, int d){
  for (int i=0; i<n; i++) {
    digitalWrite(ledPin, HIGH);
    delay(d/2);
    digitalWrite(ledPin, LOW);
    delay(d/2);
  }
}

int main(void)
{
init();

setup();

for (;;)
loop();

return 0;
}

Building

When you’re ready to build and upload to the Arduino, click the Hammer icon. This should compile your program and then run avrdude, which uploads to the Arduino.

New projects

Duplicating a project in Eclipse is as simple as selecting the source project in the Project Explorer panel, copying it (Edit > Copy) then paste. Eclipse will prompt you for a new name , then will create a new, duplicate project in the workspace.

If you keep the blink sketch intact, you should be able to duplicate it each time and avoid the above setup.

Follow up

perspectives

You should be all set to work on Arduino projects in Eclipse. Note that Eclipse uses various “perspectives” for different modes and programming languages. Typically, you’ll be working in the C/C++ perspective while working on Arduino code. If you’d like to switch back to the Java perspective for working with Processing, select it from the perspectives at the upper right of the main window, or Navigate to Window > Open Perspective > Other…


Posted

in

by

Comments

39 responses to “Arduino in Eclipse”

  1. SeeDoubleYou Avatar

    Hi, I’ve tried your tutorial and I must say, very well written. I seem to be getting much further than with the post on arduino.cc, but I still haven’t succeeded so far. It seems that something goes wrong with the stk500 programmer. Whenever I start arduino I can upload sketches without a problem. But when I use eclipse it can’t seem to connect (I get many timeouts). If I then start arduino again it cannot connect to the stk500 either. I have to do a restart to make it work again. Do you have any clues or can you check your settings for points that may be missing in the tutorial? Thanks in advance.

  2. Robert Avatar

    Well, I can only guarantee that the settings described in the tutorial work for my system. Double-check the AVR device settings in eclipse. I’m not using a specific programmer, but either a SparkFun FTDI USB to Serial board or a Diecimila itself. I’ve selected “arduino” as the programmer within eclipse rather than stk500.

  3. Wes Avatar

    Hi Robert,

    Thanks for the write-up, which was much more accurate and pleasant to work with than the one found at Arduino.cc.

    I’m very new to c & Arduino, so please read my comments under that light. I ran into a compilation error with the above example (with Print.cpp.o and ‘undefined reference to ‘__cxa_pure_virtual’) and found that if I comment out the ‘Serial.’ statements I was able to compile. Obviously, your code example works with the serial calls in Arduino 0017, if I remove the ‘main’ block first :).

    I skimmed through again, but I don’t think I overlooked an explanation. I was curious how your Eclipse is set up to handle/views serial output.

  4. Wes Avatar

    I ran into an issue similar to what “SeeYouDouble” had said, after looking at the Eclipse console, AVRDude responds that ‘arduino’ is not a valid programmer; I had forgot that Arduino.cc’s guide asked me to replace ‘avrdude.conf’ with the one supplied from Arduino’s own IDE (Arduino 0017, fyi). After reverting to WinAVR’s default ‘avrdude.conf’ and ‘avrdude.exe’, I was able to upload Arduino code via Eclipse.

  5. Robert Avatar

    @Wes I recall having to make a small change for 0017. I’ll post the change when I get back to my computer next week.

    Check my comment at the end: http://robertcarlsen.net/2009/02/28/using-arduino-in-xcode-532

  6. Wes Avatar

    Robert,

    Awesome — That works :).

    I added ‘extern “C” void __cxa_pure_virtual() { while (1); }’ which skips the earlier compilation “errors”.

    After I installed Eclipse plug-ins for Target Management & RXTX, and set the Eclipse view ‘Terminal’ for my serial, I can view the Arudino’s serial output. I just need to disconnect before invoking AVRDude ;).

    update-sites for TM & RXTX, respectively:
    http://download.eclipse.org/dsdp/tm/updates/3.1/
    http://rxtx.qbang.org/eclipse

  7. Robert Avatar

    thanks for the mention of the target management plug-in. i’ve been using Cornflake as my serial terminal, or just running the serial port through screen. i’ll check out the plug-in, however.

  8. jo Avatar
    jo

    hi and thanks for this tutorial!!!
    I’m on linuxMint and eclipse 3.5.1

    so far everything worked as discribed, but I got stuck at the “setup eclipse-step” as I can see in your picture you changed the paths for AVRDude, AVR-GCC and GNU make to “usr/local/CrossPack……..”

    I just followed your steps for installing CDT and AVR-plugin. further I’ve installed the mentioned AVR packages with “apt-get”, but can’t find the Crosspack location for configuring the path.

    If you could give me a tip it would be great!
    regards, jo

  9. Robert Avatar

    CrossPack is for OS X. I haven’t tried to set up AVR+Eclipse on linux.
    Where are your avr tools installed? (specifically, look for avrdude – try using “which avrdude” at a command line).

  10. Jim Schrempp Avatar

    You are a life saver. I’ve been wrestling with Eclipse and the Arduino for a month now. When I found your blog I immediately got your Hello_Blink to compile and link. What a big step forward!

    Since then I’ve gotten my bigger project to compile and just today solved my AVRDude issue. When I saw my message “from the eclipse version” in my terminal window I knew the search was over.

    I’m running on MSWindows so things were a little different for me, but not much.

    Thanks again for an excellent post.

    Best of luck to you,
    Jim

  11. Dryw Filtiarn Avatar
    Dryw Filtiarn

    Excellent walkthrough, was a nearly effortless install of the required tools and the configuration was easy when following the guide. I’m glad I can now use Eclipse for programming the Arduino as I wasn’t too happy with the standard Arduino IDE.

    In regards to the Target Management terminal I found that Putty was also very usefull for monitoring the serial output. I still have to try the Terminal in TM as when I installed the site hosting the RXTX install for Eclipse was not functioning.

  12. mtz Avatar
    mtz

    hi,

    How i could use an external library in the main program? I mean, how do i should configure eclipse to use the s65display library?

    thanks

  13. Robert Avatar

    Try adding the libraries path to AVR Compiler/Directories and to AVR C Linker/Libraries in the Project Properties.
    If you’ve installed the S65Display library with the others, the path is likely (in OS X): /Applications/Arduino.app/Contents/Resources/Java/hardware/libraries

    Then include the library in main.c and see what happens! Let us know since I haven’t tried this yet…

  14. Pieter Avatar
    Pieter

    Excellent description, you ‘re a hero!
    Robert, adding the Arduino Ethernet library does not seem to work the way you describe here, in my case.
    Building is fine, but the AVR C Linker complains about about an undefined reference.

    Any ideas?

  15. Rob Avatar
    Rob

    hi,
    nice tutorial, compiles fine on ubuntu 9.10, … but I got a major size problem when doing it this way. After compilation my binaries are over 8000 bytes in size, the Arduino IDE results in about 2300.

    It might be the problem but as I did not find a .a library in my arduino IDE I compiled it myself. By leaving out the Serial calls and changing global includes in Tone.cpp to local ones I could get the basic size (just one simple blink-delay loop) down to about 1300 bytes, but once the Serial commands were back in the binaries exceeded 8000 bytes in size. I would usually not care about that, but as there are only 14k available on my Diecimila and the almost empty project takes more than half of the memory its a problem. I compiled the same code in the Arduino IDE and it told me the generated file was about 2300 bytes … so whats goin on? Might it be that there is some other serial lib compiled in? Shouldn’t that be the same Wiring code in the core anyway?

    cheers rob

  16. Willi Avatar
    Willi

    Sorry, my English isn´t so good.
    This is a very good tutorial. I have a problem with the Arduino lippath. My path looks like so:
    C:\arduino\hardware\arduino\cores.
    Is this the right path?

    Errormessage : G:\UserDaten\Willi\Documents\Programmierung\Arduino\workspace\Hello_Blink\libcore.a(Print.cpp.o):(.rodata._ZTV5Print+0x4): undefined reference to `__cxa_pure_virtual’
    make: *** [Hello_Blink.elf] Error 1

    Wes says:
    I added ‘extern “C” void __cxa_pure_virtual() { while (1); }’ which skips the earlier compilation “errors”.

    Where must I added this code?
    Best regards from a newbie

  17. Robert Avatar

    You add that somewhere in your main.cpp file. I’d put it near the top, just above the setup() method.

  18. Willi Avatar
    Willi

    Thank you Robert for the quickly anewer. But the error remains. Here my complete Code.

    /*
    * main.c
    *
    * Created on: 18.04.2010
    * Author: Willi
    */

    #include “WProgram.h”

    // prototypes
    void blink(int n, int d);

    // variables
    int ledPin = 13; // LED connected to digital pin 13

    extern “C” void __cxa_pure_virtual()
    {
    while(1);
    }

    void setup(){
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    pinMode(ledPin, OUTPUT); // sets the digital pin as output
    Serial.println(“——————“);
    Serial.println(” hello world”);
    Serial.println(“——————“);
    }

    void loop(){

    blink(3, 50); // blink led (n times, ms interval)
    delay(1000); // wait 1 sec
    }

    void blink(int n, int d){
    for (int i=0; i<n; i++) {
    digitalWrite(ledPin, HIGH);
    delay(d/2);
    digitalWrite(ledPin, LOW);
    delay(d/2);
    }
    }

    int main(void)
    {
    init();

    setup();

    for (;;)
    loop();

    return 0;
    }

    it´s very fine, if can you hold my hand :-)
    Willi

  19. […] set the baud rate to 57600, which seems to be necessary but other tutorials don’t mention. Robert Carleson’s tutorial almost works, but uses a C project. The playground page specifies a code size optimization, I […]

  20. Newton Bitar Avatar

    Please, help me.

    I’m with problem in use the command Serial.print. Follow bellow the command full:

    int val = 10;

    Serial.print(val,DEC);
    ———————-

    The error is showed:

    Building target: rastreador_new.elf
    Invoking: AVR C++ Linker
    avr-g++ -Wl,-Map,rastreador_new.map,–cref -L”/home/test/source/test” -mmcu=atmega168 -o”rastreador_new.elf” ./SoftwareSerial.o ./gps_api.o ./main.o ./template.o -lcore
    ./main.o: In function `loop’:
    main.cpp:(.text+0x28): undefined reference to `Print::print(int, int)’

    I’m using linux ubuntu 9.10.

    Thanks.

  21. google.com/profiles/11… Avatar

    Great tutorial. Note that the applet compile folder is not
    generated in new versions of the Arduino IDE on the mac, so your
    instructions for finding the core.a file won’t work. Instead, use
    “shift” + upload to create verbose output and find the temp compile
    folder in the output. Then use terminal to go to that directory and
    copy the core.a file.

  22. Robert Avatar

    Thanks for that insight!

  23. VE2YMV Avatar
    VE2YMV

    Duuude, that tutorial is awesome!
    I just imported a LED blink program I originally created with the Wiring IDE, but I was frustrated by the (over)simplified options/project management options.
    What’s really cool is the option to switch between Java and c/c++ perspectives in a single IDE. Really nice to see what’s going on when the MCU is being programmed: Eclipse rocks!

    Some tech specs of my dev machine: Ubuntu Linux 10.10 (that rocks too), Eclipse Galileo with CDT and AVR plugin, AVRDude 5.10.

    Target is the Arduino Duemilenove + AsyncLabs Wifi shield + proto board. Interestingly, AVRDude detects an ATMega328P, not ATMega328: AVRDude claims the latter to be unsupported. Odd. Baud rate is indeed 57600 bps.

  24. josino Avatar
    josino

    Please help!

    I have follow this tutorial but I can not compilate the project.

    I am using eclipse helios for 64 bits and I have done the set up as in the tutorial but I get the error:

    Invoking: AVR C++ Linker
    avr-gcc –cref -s -Os -o”ArduinoCore.elf” ./main.o -llibArduinoCore -lm -Wl,-Map,ArduinoCore.map,–cref -L”D:\EclipseWorkSpace\ArduinoCore” -mmcu=atmega1281
    c:/winavr-20100110/bin/../lib/gcc/avr/4.3.3/../../../../avr/bin/ld.exe: cannot find -llibArduinoCore

    However, the path of libArduidoCore.a is well specified in the AVR C++ Linker.

    Thanks for the tutotial.

  25. Andy Avatar

    Great tutorial but got stuck with the same error as Willi undefined reference to `__cxa_pure_virtual’ error when compiling?

    Details:

    /usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp:144: undefined reference to `__cxa_pure_virtual’
    make: *** [Hello_Blink.elf] Error 1

    Hope someone can help me out?

  26. JR Avatar
    JR

    Dryw Filtiarn mentioned using PuTTY to access the serial terminal. For people who haven’t done that before…start PuTTY. Change “Connection type” from SSH to Serial. Set the serial port associated with your USB port and click Open. The default speed of 9600 worked for me (presumably it’s related to the “Serial.begin(9600);” line in the sample program) It’s not rocket science, but it took me 15 mins to figure it out (in particular, I tried hitting Enter from the Connection->Serial settings screen, but that doesn’t have any effect until you change the Connection type.

  27. Michael Avatar
    Michael

    Has anyone attempt to add other Arduino Libraries? for example messenger, I have eclipse working fine with everything that came with the Arduino IDE but I cant add new libraries

  28. jantje Avatar

    There is now a eclipse plugin that works with the avrdude and compiler delivered with Arduino IDE.
    This makes the installation process a lot easier.
    Read more at http://eclipse.baeyens.it
    Best regards
    Jante

  29. Evan Avatar
    Evan

    Hey, great tutorial, i just got throught it. The only problem i am having is the entire “Serial” class isnt being picked up. “HIGH” and “LOW” both seem to not link to the library correctly. Oddly enough, Eclipse does pick up on digitalWrite() and delay() without a problem. Could someone please help me out? i dont know whats going on here. thanks guys.

  30. aguegu Avatar

    awesome. Would you please make a guide about how to import the lib in Arduino-IDE. You know, it is really the libs make Arduino functional.

    The original IDE sucks!

  31. Mark Seaborn Avatar

    On the __cxa_pure_virtual linker error:

    The reason the Arduino IDE avoids this linker error for some simple programs is that it compiles with the options “-ffunction-sections -fdata-sections” and links with “-Wl,–gc-sections”, which drops some references to unused symbols. So, try passing those options to avr-gcc to avoid this problem.

  32. Dusty Avatar
    Dusty

    The latest version of Arduino has renamed the include file WProgram.h to Arduino.h

  33. Nate Avatar
    Nate

    This has been a really useful tutorial! I’m not sure if you’re still responding to this post, but I have run into a slight issue that I was hoping to get clarification on. I am using the most recent Arduino IDE 1.5.6-r2 and when I get the stage of your tutorial where we… Select AVR compiler -> Directories… the path you suggest is Contents/Resources/Java/hardware/cores/arduino , but the path for this version of the IDE seems to be
    Contents/Resources/Java/hardware/arduino/avr/cores. Is this correct? The reason to ask is that when I use this directory I get an error in compiling the blink example. All other aspects of the tutorial are the same so I suspect that this may be where the error is, but I am not certain. Do you know if the directory has changed in more recent Arduino IDEs?
    Thank you

  34. Robert Avatar

    Nate,

    Thanks for the comment. I haven’t been using this method for some time but I do know that things have changed a bit under the hood with the Arduino IDE. If you sort it out please let me know and I’ll update the tutorial.

    FWIW, I’ve been using EmbedXcode instead lately, but that because I spend most of my day in Xcode anyway: http://embedxcode.weebly.com/

  35. Nate Avatar
    Nate

    Great! Thanks for the recommendation. Does embedXcode bypass the arduino boot loader providing more space on the Arduino chip? This is an additional goal of not using the Arduino IDE.

    Thank you,
    Nate

  36. Robert Avatar

    I haven’t tried bypassing the bootloader; I don’t know the answer, I’m afraid.

  37. Greensasquatch Avatar
    Greensasquatch

    Worked through your tutorial step by step, he only change I made was to use Eclipse Mars instead of Juno. The AVR paths are confirmed and after setting up AVRDude I can load target hardware from MCU successfully. I also loaded up your Hello-Blink project but am unable to build it. Build results in an error ‘avr-g++.exe Application Error’ ‘The application was unable to start correctly (0xc0000142)…. ”
    Great tutorial, but will it still work in 2015?

  38. Robert Avatar

    I haven’t worked with Arduino for some time; I’m afraid this is woefully out of date.

Leave a Reply