Click here to Skip to main content
Click here to Skip to main content

Arduino Unleashed

By , 18 Sep 2010
 

What is this Arduino Thing?

Straight from the horse’s mouth

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

Actually Arduino is a lot of things. Given a specific context, it may mean any of the following…

  1. Arduino – The hardware platform (“the board”)
  2. Arduino – The abstraction library over WinAVR. WinAVR is the GNU tool chain (Compiler + C Runtime library etc.) for the AVR microcontroller used in the Arduino boards (“the software”)
  3. Arduino – “The IDE” (We’re using Arduino 19)

Here is a picture of the specific variant of Arduino I am using (It’s called the Freeduino serial board):

http://www.freeduino.org/bf-analyze/images/freeduino_maxserial_500.jpg

Figure 1 Freeduino-serial board (with ATMega328)

Show Me Something Interesting

Here you go, but remember to come back!

  1. Makezine Arduino archive
  2. Electronics lab Arduino archive

My Hands Are All Itchy, Where Do I Start?

First of all, you need to familiarize yourself with the Arduino platform and play with the Arduino IDE which is freely downloadable from the Arduino website.

Being the expert programmer that you are, you’ll soon realize that your hands are itchier but the Arduino IDE is getting in your way of alleviating that itch. What you need is a more powerful set of tools. I recommend using the combination of WinAVR + Arduino Library + Eclipse and this is what we will explore in the rest of this article.

So What’s This Article About, Again?

This article is all about empowering you, the expert programmer (for almost any definition of expert ;)), with more powerful tools that let you unleash your creativity without being limited by the default IDE.

With Eclipse, you will have code completion, better code navigation, syntax highlighting and the whole shebang!

So from here onwards I am assuming that you have already familiarized yourself with regular Arduino development. If not, then go ahead and do it (should not take too long) then come back for a better deal or just stay the course if you don’t mind a little suspense.

Show Me the Way

Alright then, here are the steps to Arduino nirvana…

  1. Download Eclipse (about 90 MBs). Unzip it to C:\Misc\Eclipse
  2. Download the Arduino IDE. Unzip to C:\Misc\arduino-0019
  3. Download the latest version of WinAVR. Unzip to C:\Misc\WinAVR
  4. Download the AVR Eclipse plug-in and install it (follow the instructions on their website)

Starting Eclipse and Trying on a Sample Program for Size…

  1. Start Eclipse

  2. Dismiss the welcome page by clicking the cross on the tab to reveal the actual work environment…

  3. Start a new project by clicking on “File->New->C++ Project”. Choose the “AVR Cross Target Static Library” project type and set the project name to Arduino. We are now going to compile the Arduino source files into a static Library for later use. Finally click on finish.

  4. Use Windows Explorer to go to C:\Misc\arduino-0019\hardware\arduino\cores\arduino, select all the code files and drag them onto the Arduino project already open in Eclipse. Note only include files with the extensions .c, .cpp and .h

    sshot1.jpg

  5. Next click ok on the following dialog (accept the default copy option)…

  6. Now build the project by right clicking on it and choosing “Build” from the context menu. Horror of horrors we have 10 build errors…

  7. But don’t worry, we’ll fix them in a moment. Right click on the project and choose “Properties” and go to the “C/C++ Build -> Settings->AVR Compiler” option. Click on the “+” icon (after selecting the “Directories” option).

  8. Click on the “Workspace…” button and add the project directory “${workspace_loc:/${ProjName}}” as an include directory once for the “AVR Compiler” and again for the “AVR C++ Compiler”

  9. Right click on the project and choose “Properties”, then go to “AVR->Target Hardware” and then set the MCU Type option to the Microprocessor being used on your Arduino board along with the frequency of the crystal supplied with it. For me, it’s ATmega328P and 16,000,000 respectively. Then click on ok.

  10. Now build your project again and the build should succeed this time. Though you might get a bunch of warnings (21 in my case), just ignore them for now.

Das blinkenlights

  1. It’s time now for the “Hello world” equivalent of the embedded systems world. Go to “File->New->C++ Project” as before but now choose “AVR Cross Target Application” as the project type and set the project name to “BlinkenLights

  2. Right click on the BlinkenLights project and choose “New->File”, set the file name to Main.cpp and click on Finish.

  3. Now add the following C++ code to Main.cpp:
    Text Box: #include "WProgram.h" //Include arduino headers
    
    ///CONSTANTS///
    //The onboard test LED is connected to pin number 13
    const int ledPin = 13;
    const int interval = 1000; //Milliseconds
    
    ///MAIN///
    int main()
    {
    	//Initialize the Arduino library.
    	//Not doing so will prevent the delay function
    	//from working. Calling this functions is a must
    	//for all arduino projects.
    	init();
    
    	//Initialize the serial port. This will be used
    	//to send diagnostic information in this project.
    	Serial.begin(9600);
    
    	//Configure ledPin as an output
    	pinMode(ledPin, OUTPUT);
    
    	//Announce the start of program. Usually a
    	//hyper-terminal is connected to the serial
    	//port on the PC so this message can be seen
    	//there
    	Serial.println("Ready.");
    
    	//Enter the infinite loop responsible for making
    	//the microcontroller do the same thing over and
    	//over again. (Almost every microcontroller has
    	//such a loop)
    	while(true)
    	{
    		//Turn on the LED
    		digitalWrite(ledPin, HIGH);
    		//Wait for half of "interval".
    		//The LED remains ON during this time.
    		delay(interval/2);
    		//Turn off the LED
    		digitalWrite(ledPin, LOW);
    		//Wait for half of "interval".
    		//The LED remains OFF during this time.
    		delay(interval/2);
    	}
    
    	//Unreachable code but it's required by
    	//the compiler
    	return 0;
    }
  4. Now compile the project. Whoa, a whole bunch of errors!

  5. Ok let’s fix them. Right click on the “BlinkenLights” project and choose “Properties” and go to the “C/C++ build->Settings->Avr Compiler->Directories” option and click on the “+” icon.

  6. Then click on the “workspace…” button on the subsequent dialog and then choose Arduino->Debug in the sub-subsequent dialog.

  7. Finally you should see the following. Click on ok after you have verified the path.

  8. Do the same for “C/C++ build->Settings->Avr C++ Compiler->Directories”
  9. The go to “C/C++ build->Settings->Avr C++ linker->Libraries” and set the following options.

  10. Now compile the BlinkenLights project again.

    Ok that’s much better now but we still have one error to go. This is a linker error. The linker is complaining here that it could not find an implementation for the function called __cxa_pure_virtual anywhere though it is being referred to/used in other parts of the code. Hmm I hear you say “It’s a lie the function name doesn’t even suit my taste, why would I use it?!” Well I hear you alright but the thing is you are barely getting started and you haven’t seen all the code being used behind the scenes yet.

    Suffice it to say the function is required by the C++ runtime to decide what needs to be done when someone calls a pure virtual function directly. As you’ll remember from your dealings with C++, a pure virtual function cannot be called directly without providing an implementation in some derived class. Well for that matter, a class with a pure virtual function cannot be instantiated at all but the C++ runtime likes to be prepared for any and all eventuality.

    Advanced compilers would have thrown an exception from this function to signal an illegal operation but since we do not have those on the WinAVR platform, all we can do is prevent further harm from being done by entering a never ending loop (assuming the program is running amok in the first place since it has done the impossible by calling a pure virtual function directly!)
    Note: This needs to be done only for the debug build. This function is apparently not used in the release build of Arduino. The debug builds are rigged to fail fast so that the point of failure is as close as possible to the place where the real problem is.

    So here’s how we get rid of the above error…

  11. Add a new .c file to the Arduino project (note it should be .c and not .cpp). Call it missing.c and paste the following code in it and build both projects again. The error should go away. Note you need to build Arduino before you can build BlinkenLights.
    Text Box: void __cxa_pure_virtual()
    {
    	while(1);
    }
  12. On switching to the “Console” pane, you can see the result of running the avr-size command on the generated .elf file…

    But it is not a pretty sight to behold. A meager led blinking program occupies about 62% of the whole of available program memory. But don’t be alarmed, we are in the debug build and such bloat is par for the course. You will get better results when you switch to release builds.

Switching to the Release Build

  1. Right click on a project and select “Build configurations->Set Active->2 Release”. Do this for all projects. Also please let me know if you find a way of doing this for all projects at once.
    Note: You need to re-specify all the settings for the release build like you did for the debug build in order to make it compile.

    Hmm, much better, but still not good enough 18% for BlinkenLights is still too much.

Premature Optimization Not the Root of All Evils?

  1. Here’s how we go from better to impressive. The following will ensure that you only pay for those functions and data-sections which are actually being used in the program. Add the following additional flags to both the C and C++ compilers in all projects
    -ffunction-sections -fdata-sections

    Add the following flag to the linker options in all the projects.

    Text Box: -Wl,--gc-sections

  2. Rebuild them (in the correct order Arduino first, BlinkenLights second.)

    What a relief!

Uploading your Program to the Arduino

The time has now come when you can upload your very first Arduino program built within the comfy confines of Eclipse.

  1. Right click on the BlinkenLights project and select “Properties”. Go to the AVRDude page and click on new.

  2. Fill the form as follows:
    • Configuration name: Arduino
    • Programmer Hardware: Arduino
    • Override default port: \\.\COM3. Replace COM3 to whatever port the Arduino hardware is connected to.
    • Override default baud rate: 19200. Replace 19200 to whatever baud rate was specified by your board manufacturer / the person who uploaded the Arduino boot loader onto your board.

  3. Select Arduino as the programmer in AVRDude settings and click OK.

  4. Click on the AVR toolbar button to upload your program to the board.

    You should see something like the following in the console window…

    Text Box: Launching C:\misc\WinAVR\bin\avrdude -pm328p 
    	carduino "-P\\.\COM3" -b19200 -Uflash:w:BlinkenLights.hex:a 
    Output:
    
    avrdude: AVR device initialized and ready to accept instructions
    
    Reading | ################################################## | 100% 0.02s
    
    avrdude: Device signature = 0x1e950f
    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 "BlinkenLights.hex"
    avrdude: input file BlinkenLights.hex auto detected as Intel Hex
    avrdude: writing flash (2828 bytes):
    
    Writing | ################################################## | 100% 2.16s
    
    avrdude: 2828 bytes of flash written
    avrdude: verifying flash memory against BlinkenLights.hex:
    avrdude: load data flash data from input file BlinkenLights.hex:
    avrdude: input file BlinkenLights.hex auto detected as Intel Hex
    avrdude: input file BlinkenLights.hex contains 2828 bytes
    avrdude: reading on-chip flash data:
    
    Reading | ################################################## | 100% 2.14s
    
    avrdude: verifying ...
    avrdude: 2828 bytes of flash verified
    
    avrdude done.  Thank you.
  5. If everything went right, the on-board test LED (usually red in color) should now start blinking at about 1 blink/sec. Congratulations!

In Conclusion

We have finally seen how to use Eclipse for AVR development. But this is only the beginning. I have shown you how to catch fish and also given you a small fish for today, but tomorrow you must catch your own! Well actually what I mean is… there’s still a lot to explore but I have shown you the basics and you should now be fine on your own. The examples and libraries supplied with Arduino should be a good place to start for further exploration.

History

  • 16th September, 2010: Initial version

License

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

About the Author

Sandeep Datta
Technical Lead KPIT Cummins Infosystems Ltd.
India India
Member
Author ID:{5A853460-2944-42f8-84B8-2432DE3657EF}

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberAbinash Bishoyi21 Feb '13 - 20:09 
Nice!!!
QuestionTutorial based on this post, for Arduino Mega 2560memberMember 247394519 Jan '13 - 4:19 
Hi,
I used your step-by-step guide to good effect to set up a system to program my Arduino Mega 2560. Many thanks!
However, I found a couple of platform/system specific issues. So I created a tutorial on exactly what I did and how it worked out.
The tutorial is available at: http://aethersdr.org/blogs/colin/arduino_sans_ide_tutorial
 
Colin
Generalusing simulation?memberMember 315136911 Jun '11 - 15:56 
first i want to thank you for this great tutorialSmile | :)
it really helps getting things started.
 
using eclipse with other platform in the past i know it can support:
simulation, step-by-step debugging and real-time hardware emulation.
 
Do you know of a way to use this features with arduino in eclipse?
GeneralRe: using simulation?memberSandeep Datta11 Jun '11 - 20:25 
Hi,
 
I am sorry I am not aware of any Eclipse plugin with support for the WinAVR Simulator (Simulavr)[^]. You will probably be able to use Eclipse's remote debugging abilities to debug your programs using the simulator (I know this is possible but I haven't tried it myself). Google is your friend for this.
 
Regards,
Sandeep Datta.
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

QuestionUsing #include "LiquidCrystal.h"memberr0oland11 Feb '11 - 11:06 
When I am trying to talk to a LCD display by using the #include "LiquidCrystal.h" and the LiquidCrystal lcd(7, 5, 4, 3, 2, 1); command, it wont event start to do anything but stops with the following error:
undefined reference to `LiquidCrystal::LiquidCrystal...
 
I copied the LiquidCrystal.h and LiquidCrystal.cpp files to the Arduino folder so they should get included. What am I doing wrong? Any help would be more than appreciated.
 
Best regards, Jo
AnswerRe: Using #include "LiquidCrystal.h"memberSandeep Datta17 May '11 - 0:40 
I know it's kind of late (I somehow missed the email notification for your post) ... r0oland you must include those files in the eclipse project in the same way the other files were added. Simply copying them to the same folder on disk will not work. Hope I have understood your problem correctly.
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

General23. errormemberpiranha8018 Dec '10 - 14:29 
Great article!
 
I am getting an error message after
23. C/C++ build-Settings-Avr C++ linker-Libraries
 
"cannot find -lArduino"
 
What am I doing wrong?
 
Thanks!
 
Michael
GeneralRe: 23. error [modified]memberSandeep Datta18 Dec '10 - 20:09 
Thanks.
 
This could be happening for various reasons...
- You have not created the Arduino project as mentioned in the article yet
- You have a typo in the name of the Arduino project
- You are trying to add apply this step in the creation of the Arduino project instead of the blinkenLights project.
 
...possibly few more. Just get back to us with the exact steps of what you have done so far.
 
Regards,
Sandeep Datta.
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo
 
modified on Sunday, December 19, 2010 2:18 AM

GeneralRe: 23. errormemberfiveohhh3 Jan '11 - 19:03 
I get the exact same error. I've triple checked the naming and the Arduino project is there and builds. Any other ideas?
GeneralRe: 23. errormemberSandeep Datta3 Jan '11 - 21:48 
Can you check if you have a file called "libArduino.a" in your build directory (usually a directory called Debug/Release in the Arduino folder)? If yes then can you verify that you have set the right path to the library i.e. the BlinkenLights project is looking at Arduino/Debug (or Arduino/Release) and not just Arduino for the library file?
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

GeneralRe: 23. error [modified]memberfiveohhh4 Jan '11 - 17:59 
K I'm not sure what I did, but I got rid of the previous errors, but now I can't get rid of the errors found in step 18. I'm going to work on this a bit more, but if you have any ideas I'd appreciate the input. Thanks for your help so far!
 
one of the errors I get is
"WProgram.h: No such file or directory main.cpp /blinky line 1"
modified on Wednesday, January 5, 2011 12:25 AM

GeneralRe: 23. errormemberSandeep Datta4 Jan '11 - 18:31 
Hi, please check that the "compiler directories" have been set correctly the header files can be found in the "Arduino" folder. So just check if you have got the right path.
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

GeneralRe: 23. error [modified]memberfiveohhh4 Jan '11 - 18:32 
OK, I got it to build. I had to add both Arduino and Arduino/Debug to the directory lists in the blink project.
 
Maybe I only needed one or the other, but with both in there it builds.
 
Thanks again!
 
edit: I did some tracing and the C/C++->settings Directories needed to have the root arduino set, and the linker->libraries needed to have Arduino/Debug set. Once I did this it built. This appears to be be opposite of what you have...
modified on Wednesday, January 5, 2011 12:40 AM

GeneralRe: 23. error [modified]memberSandeep Datta4 Jan '11 - 19:15 
Yeah, I was probably a bit drowsy while writing this tutorial...mixed up the paths...sorry about that...also I have (re)discovered ARM lately (it has much more to offer at the same price point) and I don't have the AVR environment on my machine anymore so fixing the screenshots won't be easy for me. But thanks for your interest anyways. I think you should also take a look at MHVLib (mentioned somewhere in the comments below).
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo
 
modified on Sunday, January 16, 2011 9:06 AM

GeneralRe: 23. error [modified]memberC Blair15 Jan '11 - 22:52 
Great tutorial!
 
[Edit] Aha! wrong click on BlinkenLights -> Proerties -> AVR -> Target Hardware -> put in your arduino settings; the linker was detecting that libArduino.a was compiled for the default microcontroller, and I want to compile it for an ATmega2560
 
The linker code for optimizing (had trouble reading it) is "-Wl -gc-sections"
 
Error descriptions (these are fixed, above)
==========================================
Unfortunately, I also get three errors, as you've described (there are no warnings):
 
Description	Resource	Path	Location	Type
cannot find -lArduino	BlinkenLights		line 0	C/C++ Problem
 
make: *** [BlinkenLights.elf] Error 1	BlinkenLights		line 0	C/C++ Problem
 
skipping incompatible /home/XXXXX/Arduino/Debug/libArduino.a when searching for -lArduino	BlinkenLights		line 0	C/C++ Problem
 

I have fixed the compiler locations and verified that libArduino.a is in the /Debug folder. I have built the Arduino project (right click -> Build Configurations -> Build -> Select -> Debug; and then right click -> Build Project) first, and then BlinkenLights. I tried adding the /Arduino and /Arduino/Debug folders to all compiler and linker directory lists, just to see. I'm running Fedora 13, not windows; perhaps this is the difference?
 

Sandeep I'd be happy to send you a few screenshots to fix the /Arduino vs /Arduino/Debug directory screenshots?

modified on Sunday, January 16, 2011 5:28 AM

GeneralRe: 23. error [modified]memberSandeep Datta16 Jan '11 - 0:29 
C Blair wrote:
the linker was detecting that libArduino.a was compiled for the default microcontroller, and I want to compile it for an ATmega2560

 
C Blair please recompile the Arduino project for your processor (take a look at step 13) and try again. Also please make sure the BlinkenLights project is targeting the right processor (it should be the same as the Arduino project).
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo
 
modified on Sunday, January 16, 2011 6:35 AM

GeneralRe: 23. errormemberC Blair16 Jan '11 - 14:47 
Thanks! That was indeed the fix. Good luck to everyone using this tutorial!
GeneralLEDsmemberDennis Meade15 Nov '10 - 14:16 
Sandeep
 
Great article. You might want to mention that with some Arduinos like the Duemilanove, there is no test LED. You have to plug one into Pin 13.
Dennis

GeneralRe: LEDsmemberSandeep Datta3 Jan '11 - 21:35 
Yup, good point again.
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

GeneralMy vote of 5 [modified]memberDennis Meade15 Nov '10 - 14:11 
Something I've been looking for a long time. Among other things this will make it much easier to code in C++. Also solves one of the major problems with the Arduino IDE, the awfule error messages.
modified on Monday, November 15, 2010 8:17 PM

GeneralRe: My vote of 5memberSandeep Datta3 Jan '11 - 21:33 
Thanks! And yes in my opinion Eclipse has a much better experience to offer.
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

QuestionArduino UNO questionmemberdrjoju3 Nov '10 - 3:00 
Hi, great article. Have a 5.
 
Only one queston. I have an Arduino UNO and the upload process fail. Here is my Console output. Any idea about this error? The COM port is COM17, but I don't know if the rest of the configuration it's ok.
 
Launching d:\proyectos\arduino\WinAVR-20100110\bin\avrdude -pm328p -carduino "-P\\.\COM17" -b57600 -Uflash:w:BlinkenLights.hex:a
Output:
avrdude: stk500_getsync(): not in sync: resp=0x00
 
avrdude done. Thank you.
 
avrdude finished
 
Thanks
drJoju

AnswerRe: Arduino UNO questionmemberSandeep Datta3 Nov '10 - 6:28 
Edit2: First try changing the baud to 115200 (from http://arduino.cc/en/Guide/Environment)
 
Hi,
 
I suspect you are facing a synchronization issue with the Arduino boot-loader.
 
Before uploading the firmware to Arduino AVRDUDE pulls the DTR pin (or RTS or CTS I forget which) on the serial port to auto-reset the board (this pin is wired to the RESET mechanism). The boot loader listens to the serial port for a short while for commands from AVRDUDE before continuing with the boot and transferring control to the Application code. This small window of opportunity when missed by AVRDUDE usually results in the above problem.
 
Sol: I am afraid the solution to this problem involves a fair amount of trial and error. You need to hit reset on the board manually just before AVRDUDE is invoked. You should be able to get the timing right after a few trials.
 
Edit: I remember that the LED on my board blinks thrice before resuming boot you could try and synchronize the manual reset with this signal.
 

HTH
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

GeneralRe: Arduino UNO questionmemberdrjoju3 Nov '10 - 12:12 
Thank you!! I will try it.
 
By the way I think there are 2 bugs in steps 20, 21, 22 and 23.
 
I think the Compiler directories for C and C++ must be Arduino and not Arduino->Debug because the .h files are in the root directory so images 20 and 21 are incorrect.
 
In the linker library the correct directory must be Arduino->Debug and not root directory so image in 23 is incorrect.
 
What do you think?
 
Regards
drJoju

GeneralRe: Arduino UNO questionmemberSandeep Datta4 Nov '10 - 4:08 
drjoju wrote:
I think the Compiler directories for C and C++ must be Arduino and not Arduino->Debug because the .h files are in the root directory so images 20 and 21 are incorrect.
 
In the linker library the correct directory must be Arduino->Debug and not root directory so image in 23 is incorrect.

 
Yup good catch, somehow I mixed them up in the article, will fix as soon as possible!
 
Also thanks for the feedback and rating, much appreciated!
The best way to accelerate a Macintosh is at 9.8m/sec-sec - Marcus Dolengo

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 18 Sep 2010
Article Copyright 2010 by Sandeep Datta
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid