Using Netbeans for Arduino Software Development

Synopsis: This is about setting up a Netbeans environment for Arduino programming. No plugins, no external Makefiles required.

The Arduino environment is great for getting started without requiring any knowledge about compiler, linker and all the stuff 'under the hood'. For that reason the Arduino IDE is kept somewhat simple and lacks features that are essential when 'quick hacking' or 'rapid prototyping' turn into software development.

Approach

There are approaches that are using custom Makefiles that have to be adapted and copied in your Netbeans project directory. One good example is Kerry Wongs Makefile and Netbeans Plugin. It works but lacks basic dependency checking. I did not like the fact that the core lib files were compiled every time (same with the Arduino IDE, btw).

So I wanted to see if Netbeans can be configured for Arduino software development using just the means that are provided by Netbeans.

This was done on Mac OS X 10.7 using Arduino 1.0 and Netbeans 7.0.1. It should work as well on any other system.

It looks really complicated but it isn't. ;-)

Requirements

Cross Compiler Toolchain

There are a couple of options here:

Using the Arduino Toolchain (on a Mac located at /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin)

Arduino core lib and other libraries

The nice thing about Arduino is that it comes with a nice core library and a truckload of additional libraries and examples for SPI, LCD, SD cards, OneWire protocol etc. We need these files.
I copied them to /usr/local/arduino1.0 because in case of an update of the arduino software my stuff will stil compile.

copy everything from /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino to /usr/local/arduino1.0/

copy everything from /Applications/Arduino.app/Contents/Resources/Java/libraries to /usr/local/arduino1.0/

Compiling the Arduino core lib

We want to create a static core lib library that we can link our projects against later:

Create a new project: C/C++ static library

Name it "arduino_corelib"

Select Arduino toolchain

Click "Finish"

Right-click on the newly created project "arduino_corelib" and select "Add existing Items from Folders..."

Select /usr/local/arduino1.0/cores/arduino

Click "Add"

Set project configuration to DEBUG (Menu: Run, Set Project Configuration)

Now we have to adjust some compiler settings for this project.

Breaking down the compiler options that we are going to use:

-Os: Optimizing size

-Wall: Print all warnings

-ffunction-sections -fdata-sections: Arduino IDE does this, so I copied it: "Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the section's name in the output file."

-mmcu=atmega328p -DF_CPU=16000000L: Type and speed of the microcontroller

You should find a nice corelib under dist/Debug/Arduino-MacOSX/libarduino_corelib.a in your Netbeans project directory.

Compiling a simple program

We are going to create a simple main.cpp that toggles the onboard LED connected to port 13.

Create a new C/C++ application, name it e.g. "blink", set toolchain to "Arduino"

Edit main.cpp:

#include <Arduino.h>
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
int main(void)
{
init();
setup();
for (;;)
loop();
return 0; // must NEVER be reached
}
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}