Wednesday, April 6, 2011

Using Eclipse with Arduino

 

In this tutorial, we’ll be showing you how you can use Eclipse, which is an open-source, powerful and full-featured Integrated Development Environment (IDE) to develop your Arduino sketches. The Arduino IDE is simple, lightweight and easy to use and is best suited for small programs that you want to quickly try out. Larger programs often means more files, libraries and it’s quite difficult to manage it all efficiently using Arduino’s IDE. Using Eclipse makes it simple.

Unfortunately making Eclipse work with Arduino can be quite challenging. There are many tutorials on the web for doing that, but most of them are out-dated and needs modification. This tutorial will show you that using Eclipse to develop your Arduino programs is not nearly as daunting as it might seem. The step-by-step approach with screenshots will make your journey simpler and once you’ve used Eclipse, there is no going back.

With the introduction out of the way, let’s get started. The first thing to do is to get the latest version of the software that is needed.

STEP 1: Download the necessary programs

  1. Java Runtime 6 or higher (Most likely you already have this installed on your computer, but it’s good to download the latest version anyway)
  2. Eclipse IDE for C/C++ Developers ( We used Eclipse Helios, but you can download the latest version) Remember to download the Eclipse IDE for C/C++ Developers
  3. Arduino Software (We used version 0022, but again download the latest version)
  4. WinAVR (We used version 20100110) – Only for Windows Users
  5. AVR PlugIns for Eclipse – We’ll configure Eclipse to download and configure it in the next section.

STEP 2: Installation

  1. Install Java (this is installed automatically by the Java installer)
  2. Install Eclipse (install in any directory you want, we used C:\Program Files\Eclipse)
  3. Install Arduino (we used C:\Program Files\Arduino-0022)
  4. Install WinAVR (we used C:\Program Files\WinAVR21011110)

STEP 3: Eclipse Configuration

At this point, we have downloaded and installed almost all the software we need, but there is still one task left before we are done with the software installation. We need to install the AVR PlugIns for Eclipse. To do that, open Eclipse and

  1. Click Help on the menu bar and then click Install New Software.
  2. Click Add and in the popup dialog box type: AVR Plugin in the name field and type: http://avr-eclipse.sourceforge.net/updatesite/ in the URL field
  3. Click OK
  4. Click the small arrow to the left of CDT Optional Features and select AVR Eclipse Plugin
  5. Click Next a couple of times, accept the terms of the license and click Finish.
  6. Once Eclipse restarts, you should see a AVR button on the top button panel.

image

 

STEP 4: Building the Arduino Library

Before we can use Eclipse for developing Arduino projects, we’ll have to build a Arduino library which we can link to our projects. There are several ways to do this, but we’ll be creating a static library.

  • Open Eclipse and start a new C++ project.
  • C++ Project
    • Project Name: ArduinoCore
    • Project Type: AVR Cross Target Static Libary
    • Toolchains: AVR-GCC Toolchain

arduinolibrary1

  • Select Configurations, Uncheck debug we will not be using the debug version of this library in this tutorial.
  • Set the MCU type and frequency for your Arduino board. For example, for Duemilanove, use ATmega328P running at 16000000 Hz.  Click Finish. 

arduinolibrary1

  • In Eclipse, click Project->Properties.
  • Select C/C++ Build and expand the category (e.g., click the triangle to the left of “C/C++ Build”).
  • Select Settings under C/C++ Build.
  • 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 “C:\Program Files\arduino-0022\hardware\arduino\cores\arduino
  • 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 “C:\Program Files\arduino-0022\hardware\arduino\cores\arduino
  • Right click on the ArduinoCore project in the project explorer on the right and select Import.
  • On the Import dialog, select General => File System and click next.
  • On the File system dialog click browse and select the Arduino IDE’s hardware folder  “Arduino-0018\hardware\arduino\cores\arduino”
  • Select all the files except main.cpp. Click finish
  • Build the project, On the mail dialog click the hammer in the too bar or by selecting Project =>Build All.

That’s it! You’ve build an Arduino Library. You can find it in the Eclipse workspace folder. It’s name libArduinoCore.a

image

 

STEP 5: Creating a simple Arduino Project Template

The next step is to create a simple Arduino Project Template that can be used for future projects. This will create a simple main.cpp file with the basic functions needed for the file to compile and will be linked with the libArduinoCore.cpp file we created earlier.

 

  1. Create a new Eclipse C++ project, File => New => C++ project
  2. C++ Project
    • Project Name: ArduinoTemplate
    • Project Type: AVR Cross Target Application => Empty project
    • Toolchains: AVR-GCC Toolchain
  3. Select Configurations, Uncheck debug.
  4. Set the MCU type and frequency for your Arduino board. For Duemilanove, use ATmega328P running at 16000000 Hz. Click Finish.
  5. In Eclipse, click Project => Properties.
  6. Select C/C++ Build and expand the category (e.g., click the triangle 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”.
    1. Directories, Add the path to the Arduino IDE’s Hardware folder e.g “C:\Program Files\arduino-0022\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 e.g “C:\Program Files\arduino-0022\hardware\arduino\cores\arduino”
  1. 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’. It’s best to copy the library into the new project.
  2. Create a new source file,  File => New => Source file, Call it main.cpp
  3. Type in the following content:
   1: #include <WProgram.h>
   2:  
   3:  
   4: // The setup() method runs once, when the sketch starts
   5: void setup()   {
   6:  
   7: }
   8:  
   9: // the loop() method runs over and over again,
  10: // as long as the Arduino has power
  11:  
  12: void loop()
  13: {
  14:            // wait for a second
  15: }
  16:  
  17: int main(void) {
  18:  
  19:   /* Must call init for arduino to work properly */
  20:   init();
  21:   setup();
  22:  
  23:   for (;;) {
  24:       loop();
  25:   } // end for
  26: } // end main

Build the project, On the mail dialog click the hammer in the too bar or by selecting Project =>Build All.


STEP 6: Uploading code to the Arduino


AVRDude is a flash programmer for Atmel microcontrollers. 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 ( You can input any name you want)
    • Programmer Hardware: Arduino
    • Override default port: COM11
      Note:  you will have to change this to your comport. To find out which COM port to use, open the Device Manger on your Windows machine and look for the USB Serial Port. 
    • Override default baudrate: 57600

  5. Click OK,
  6. Highlight ArduinoTemplate in the project explorer and click AVR => Upload to target device or the AVR icon with the green downward arrow

 


 


That’s it. Congratulations! You have made Arduino work with Eclipse. Happy Coding!

11 comments:

  1. I'm the first post. F Yeah. Anyway, awesome post it helped me a lot, it even worked with Ubuntu, obviously I didn't download WinAVR, I just dowloaded the avr package from the repos.

    Thanks

    ReplyDelete
  2. Just the right thing for me. Been searching around for a way to use arduino code. Been using raw C code for programming the Atmega. Thanks Robin.

    ReplyDelete
  3. Cool, I used to copy-paste the core.a from the Arduino IDE - now I can get the core library directly compiled in Eclipse. Problem is - what about the other libraries? What if I want to get the Ethernet library (e.g.)? This I haven't been able to do yet...

    ReplyDelete
  4. Carlos, which package?

    ReplyDelete
  5. You dont need WinAVR. All tooling needed is already available in the Arduino tool directory (compilers, linkers, libs etc).
    I did an install without WinAvr and it works perfectly. Why I did it? The WinAvr latest version is not tested with the Arduino. The version from e.g. "C:\arduino-0022\hardware\tools\avr" is.

    ReplyDelete
  6. I followed all the instructions and built the project first, but got this error when I clicked the upload to AVR button:

    The file [${workspace_loc:/ArduinoTemplate/Release}/ArduinoTemplate.hex] for the Flash memory does not exist or is not readable

    ??

    ReplyDelete
  7. I figured out the error. Under Step 5 (AVR C++ Linker => Libraries), be sure to also enter the path to ArduinoCore in the Libraries Path (-L) box.

    ReplyDelete
  8. I copied my libArduinoCore.a file to the new project folder, added it to the AVR C++ Linker->Libraries->Libraries with C:\Users\David\workspace\ArduinoTemplate\ArduinoCore

    And every time I do a build all it can't find the file.

    Any ideas?

    ReplyDelete
  9. The Arduino 1.0 is out. In addition to "C:\Program Files\arduino-1.0\hardware\arduino\cores\arduino" we must now also add "C:\Program Files\arduino-1.0\hardware\arduino\cores\variants\mega". There are different directories for mega, leonardo, micro, and standard.

    The WProgram.h file, which provides declarations for the Arduino API, has been renamed to Arduino.h. After modifying the build with these additional paths and replacing WProgram.h with Arduino.h. The compiles work.

    Thanks again for your execllent work.

    ReplyDelete
  10. It all is a lot simpler now with the plugin that can be found at http://eclipse.baeyens.it
    Jantje

    ReplyDelete
  11. There is now an eclipse plugin that makes it all lots easier.
    See eclipse.baeyens.it for more info

    ReplyDelete