Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Arduino

Creating Arduino programs in Eclipse

5.00/5 (9 votes)
23 Jun 2015CPOL12 min read 165.1K  
Article on setting up Eclipse to build programs for Arduino Uno.

Introduction

This article describes how to set up Eclipse IDE and AVR Eclipse plugin to be able to develop programs on a Windows computer for Arduino Uno. It should work also for other AVR-based Arduinos.

There are many articles on the Internet about developing programs for Arduino, but most of them are out of date, too complicated, based on Linux system and so on. This is quite natural considering the speed with which the tools are changing, so this article will sooner or later also become out of date. But for now (middle of 2015) it should work fine and I believe it is also "reasonably simple" (read as simple as I could make it).

This article is based on the Eclipse Luna SR2 with AVR Eclipse plugin and Arduino IDE 1.6.5-r2 (all tools in their latest versions as of June 22, 2015).

Background

I probably don't need to tell you why use Eclipse instead of the Arduino IDE. If you plan to create more complex project with your Arduino or even see yourself as an embedded software developer some day, you will soon outgrow the Arduino IDE. And then there are two main options: Eclipse or Atmel Studio. If you read this, you probably decided for Eclipse, which is choice I like myself too.

Once you decided for Eclipse, there are two main roads you can choose:

The first road is easier, but I did not like it when I tried it some time ago. Personally, I consider it somewhere in between the real-world programming and the Arduino IDE. For example, the compiler options you can set are very limited. If you decided to use the real-world tools, why stop half way through? But the plugin is developing quickly, so maybe in the current version things have improved. But anyway, this article is about the second road, using AVR Eclipse plugin.

 

Install the tools

We will need the following tools:

Here are installation instructions:

Arduino
From http://www.arduino.cc/en/Main/Software download the Arduino software.

Current version (June 2015) is 1.6.5-r2. You can either download installer or a zip file. In case of zip file just extract it to any folder on your computer, for example, to c:\programs\arduino1.6.5-r2.

Eclipse
From https://eclipse.org/downloads/ download the Eclipse IDE for C/C++ Developers;

Current version (June 2015) is Luna SR2. I recommend using 32-bit version even for 64-bit Windows, because I am not sure if everything would work with 64-bit Eclipse.

There is no installation needed. You download a .zip file, which you can extract into any folder on your computer, for example, to c:\programs\eclipse.
To start Eclipse IDE, go to the eclipse folder and run eclipse.exe.
You may want to create link on your desktop for quick access.

When Eclipse starts, you need to select location for your workspace. This is a folder on your computer where all your projects will be located. Later you can use several workspaces and change the default location, but for now I recommend just accepting the default offered by Eclipse and moving on.

AVR Eclipse plugin
Start the Eclipse IDE and in the main menu select Help > Install New Software.
In the Work with box enter this update site address:
http://avr-eclipse.sourceforge.net/updatesite
and press Enter on the keyboard.

After a while the list below will display AVR Eclipse Plugin item. Select it by checking the box next to the plugin name and click the Next button below.
Follow the wizard to install the plugin. It takes quite a while. You will need to agree with license agreement and confirm installing of unsigned plugin. After the installation, accept the offer to restart Eclipse.

Tip: You can find tutorial with pictures on the AVR Eclipse plugin website at:
http://avr-eclipse.sourceforge.net/wiki/index.php/Plugin_Download


MinGW tools
The MinGW tools are needed to obtain the GNU make utility. If you have Atmel Studio 6.x installed on your computer, you can skip this step; you will find make.exe in the [Atmel Studio location]\shellUtils. We will need this path in AVR Eclipse plugin configuration as described in the next section.

To install MinGW:
From http://www.mingw.org/ follow the Downloads link on the left.
This will take you to http://sourceforge.net/projects/mingw/files/.
Near the top of the page you will find "Looking for the latest version?" line and a link to download.
Download the mingw-get-setup.exe (direct link: http://sourceforge.net/projects/mingw/files/latest/download?source=files).

Run the setup program.  In the first screen leave all settings at defaults, change the Installation Directory if you prefer and click Continue.


Image 1

In the next step wait for the download of some files and click Continue.

You should see a MinGW Installation Manager window. In the list of available packages click on the box next to "msys base" (the last item in the list).

From the context menu select Mark for Installation.

Image 2


From the menu at the top of the window select Installation > Apply Changes.

Image 3

In the next step click Apply button.

Image 4

After a while the MinGW tools should be installed. Close the window. 

We are now ready to configure the AVR Eclipse plugin.

 

Set up the AVR Eclipse plugin

Start Eclipse and in the main menu select Window > Preferences.
In the Preferences window expand AVR > Paths.
Use the Edit... button on the right to set the paths as shown in the following picture.

Image 5

Here are the path on my computer:

AVR-GCC: C:\Programs\arduino-1.6.5-r2\hardware\tools\avr\bin
GNU make: C:\mingw_test\msys\1.0\bin
AVR Header Files: C:\Programs\arduino-1.6.5-r2\hardware\tools\avr\avr\include
AVRDude: C:\Programs\arduino-1.6.5-r2\hardware\tools\avr\bin

You can see that most of the paths point to the Arduino location. The GNU make tool (make.exe) path points to the MinGW location. The paths may be different on your system, depending on where you installed the tools.

To set the paths, use the Browse button in the window for path selection and select Path source: Custom; see the picture below.


Image 6

Next, select the AVRDude category on the left.

Check the box "Use custom configuration file for AVRDude".

In “AVRDude config file” box browse to the path of this file in your Arduino installation. Here is how it looks:


Image 7


The config file path is [your Arduino path]\hardware\tools\avr\etc\avrdude.conf.

Close the preferences window by clicking the OK button.<o:p>

Note: We will return to this window again in the next step, but it seems closing the Preferences window is required to apply the changes. Without this, the next step fails with error that AVRDude cannot find its configuration file.

Open again the Preferences from Window menu.

Select AVR > AVRDude.

Click the Add button on the right-side of the the Programmer configuration list.

In the window which opens, select Arduino from the “Programmer Hardware” list.
Enter some name in the Configuration name field above the list, for example, Arduino.
Enter the serial port to which your Arduino is connected into the "Override default port" box below the list.

Note that the port name must be written like this on Windows: "//./COM15".  (with your COM port number instead of 15, of course). See the picture below.

Image 8

Close the window with OK and then close the Preferences window also. The IDE is now ready for Arduino development.<o:p>

 

Create your first Arduino program

This section describes how to create project with Arduino MCU Framework (the software library), so that you can use the Arduino functions in your program.
First, we will create a path variable in Eclipse which will allow us to easily update our projects when new version of Arduino appears. This is only needed once in a workspace.

In Eclipse main menu select Window > Preferences.

In the Preferences window expand General > Workspace > Linked Resources.


Click the New button to add a new path variable.
Name it ArduinoPath and point it to the main folder of your Arduino installation, in my case c:\programs\arduino-1.6.5-r2. See the picture.

Image 9

Close the preferences window.


Now we are ready to create new project.

In Eclipse go to File menu > New > C++ Project.

Enter name of your project, for example, blink.
Select AVR Cross Target Application > Empty Project and click Next.

Image 10

In the next step leave everything as is and click Next.

In the next step select MCU Type: ATmega328P and MCU Frequency (Hz): 16000000. (that is 16 MHz).

Image 11

Click Finish.

We now have an empty project and will add links to the Arduino files into the project.

In the Project Explorer window in Eclipse right-click on the project and select New > Folder from the context menu.

Image 12

In the New Folder window expand the Advanced options in the lower part of the window and select the "Link to Alternate location (Linked folder)" option.

Then click the Variables... button and select our ArduinoPath variable from the list.

Then click Extend... button.

Image 13

In the Variable Extension window you should see the sub-folders of the Arduino folder.
Browse to hardware\arduino\avr\cores\arduino and click OK.

Image 14

Now you will be back in the New Folder window and the path to our linked folder should point to ArduinoPath/hardware/arduino/avr/cores/arduino; see the picture below.

Image 15

Click Finish to close the window.

Repeat the same procedure for another folder. The location of this folder will be:

ArduinoPath/hardware/arduino/avr/variants/standard.

Now you should see two (linked) sub-folders in your project: arduino and standard.

Image 16

Go to preferences for your project (Alt + Enter or right-click the project and select Properties).

Expand C/C++ Build > Settings category.

At the top of the window, in "Configuration" list select [All configurations].

Select the Additional Tools in Toolchain sub-category.

Check the box next to "Generate HEX file for Flash memory".
The HEX file is the output of the build process which is later uploaded to the microcontroller on your Arduino board.

Image 17

Now select the sub-category AVR C++ Compiler > Directories and add the two Arduino folders which we have added to our project into the list.
The easiest way is to copy-paste the following two paths, including the quotation marks (click the small Add button and then paste one path; repeat for the other):

"${workspace_loc:/${ProjName}/arduino}"
"${workspace_loc:/${ProjName}/standard}"

Alternatively, click the Add button, then Workspace button in the Add directory path window. In the Folder selection window browse to [your_project]/arduino and click OK. Click OK again to close the Add directory path window. Repeat the same for the “standard” folder.

Image 18
This tells the compiler to search for source code in these locations.


Repeat the same step also for the directories of the C compiler in AVR C Compiler > Directories.

Still in the Preferences window, select AVR > AVRDude and in the "Programmer Configuration" list select Arduino. This is the programmer we have created earlier in the Set up the AVR Eclipse plugin section. If you do not see any item in the list please go back to that section and make sure you have done all the steps.

Image 19
 

Now we will add source file with our setup() and loop() functions.

Right-click the project and select New > Source File.
Enter some name for the file with .cpp extension and click Finish. The name could be the same as the project’s name or sketch.cpp etc.

Image 20

Click Finish to create the file. It will appear under the project in Project Explorer. Double-click it to open it.

Paste the following code into the new file:

C++
#include "arduino.h"

void setup(void)
{
    pinMode(13, OUTPUT);
}

void loop(void)
{
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(13, LOW);
    delay(2500);
}

Save the file.

Note: In default configuration Eclipse does not save the files before build. I recommend enabling auto-save in Window > Preferences > General > Workspace:  "Save automatically before build”. Without this you will often forget to save the changes after modifying your code and will be actually building the old version, without these changes.


Build your project.

The project should built without errors, there will be some warnings about delay functions which have no effect on our program. The build may take little longer first time because all the Arduino core functions are built, but later, the compiler is smart enough to build only changed files, so it takes only few seconds to build the program.

Now upload your project to your Arduino board.

First, connect the Arduino board to your computer. Give the computer enough time recognize the board.

Now click the AVR icon in Eclipse toolbar.

Image 21

Your program will be uploaded to the board. Check the results in the Console window in lower part of Eclipse. There will be some messages from AVRDude. It should end with "avrdude done. Thank you."

Image 22

You should now see that the LED on your Arduino is blinking. This means our program is running.

If you get an error message "port is blocked" you probably have wrong COM port number in the AVR Eclipse plugin configuration. To fix this go to Window > Preferences > AVR > AVRDude. In the "Programmer configurations" list select your Arduino item and click Edit. Make sure the number of COM port (for example, //./COM15) is correct. You can find this number either in the Arduino IDE or in Hardware Manager of your computer.

You are now ready to develop your programs in Eclipse. Don't miss some of the cool features of a professional IDE, for example, you can look at the source code of the Arduino functions: just select the function name, right-click and select Open Declaration.

Image 23
 

Points of Interest

In most tutorials I have seen the Arduino core functions are pre-build into a static library first and then this library is added to your project(s). In this tutorial we link the Arduino files directly to the project and build them together with our code. In my opinion this is easier to set up, easier to update and the build does not take unbearably long, so I consider this a better option than using static library.

If you update to a new version of Arduino software, you just need to change the value for the ArduinoPath variable and all the projects should work with the new version.

 

History

2015-06-23 First version.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)