Setting up Xcode to Compile & Upload to an Arduino ATMega328 (Duemilanove)

This morning Joshua Noble tweeted a link to this excellent post that details how to setup your Xcode environment to compile & upload to an older Arduino168 like a Diecimila. This is awesome for anyone who would rather code in a more feature rich environment than the Processing / Arduino IDE such as myself. However, while trying to get this working with my Duemilanove, I ran into a few problems compiling and uploading code to the board.

After sifting through the comments and making some tweaks to the Makefile I finally got this working and so without further ado, here are the steps to get Xcode talking to your ATMega328 (Duemilanove).

1. Install Xcode obviously if you haven’t already.
2. Download & Install the Arduino IDE as you still need the core Arduino libraries to compile against.
3. Download & Install the AVR Programming Tools. The most recent version worked fine for me.

CrossPack-AVR-20100115.dmg

4. Download this Project Template to compile & upload the “Blink” sketch to confirm everything is working.
5. Unzip the template and put it in /Developer/Library/Xcode/Project Templates/Other/
6. Now fire up Xcode and create a new project from the template found in Other -> ArduinoOnXcode

Arduino-Template

Now take a look at the Makefile in your new project. This file defines all your compiler arguments and tells Xcode where to find the dependency libraries it needs on your system.

Arduino-Makefile

The AVR Programming tools should have installed to /usr/local/CrossPack-AVR

If for some reason this is not the case be sure to set the correct path so Xcode can find your CrossPack in the AVRDUDE_DIR on line 65. Note also that /usr/local/CrossPack-AVR is a symlink so if you are using a newer version of the CrossPack this path should still work fine.

At this point connect your Arduino to one of your USB ports, change the Active Target to “Upload” and select “Clean All Targets” from the Build menu and you should see the the TX/RX lights flash rapidly as the code is compiled and uploaded to the board.

Now go ahead and make some changes to the main.cpp file in your project, recompile and you should see these changes reflected on your board.

Note, I still get a few warnings on compilation that I think are related to properties in the Makefile.
Any suggestions to remedy these warnings or any other improvements are most welcome in the comments below.

  • http://www.wrinklyfilm.com Mike Naylor

    Followed your instructions and it worked first time – with no compilation warnings. It would be great to see the “Hello World” message somewhere. Is there an easy way to do that?

  • Pingback: Track Hacker

  • Paul Buxton

    The dependancies don’t seem to pick up changes c-files as requiring a re-upload so need to run clean manually, other than that works very nicely, thanks for this! I may poke around and see if I can get the dependancies working a bit better, if I do I will post the info here.

    Mike, use a serial terminal application. Cornflake.app seems to work pretty well.
    http://tomgerhardt.com/Cornflake/

  • Ric Turley

    I like this idea, I so miss drag and drop in the Arduino IDE.

    Everything goes great until it tries to upload:

    /usr/local/CrossPack-AVR/bin/avrdude -F -p atmega328p -P /dev/tty.usbserial* -c stk500v2 -b 57600 -D -U flash:w:main.hex
    avrdude: no programmer has been specified on the command line or the config file
    Specify a programmer using the -c option and try again
    make: *** [upload] Error 1
    Command make failed with exit code 2

    Same error specifying a specific USB and with stk500v1 (I found stk500v2 in Arduino 0022

    Any idea how to solve this?

  • TCB13

    Hi, this is just great, I’m used to program for PIC microcontrollers at work (always under XCode), but now I decided to try Arduino… well it’s very nice, but I hate their IDE!

    Anyway, I’ve had the same problem as RIC TYRLEY, to solve it is easy, just replace:

    AVRDUDE_PROGRAMMER = stk500v1

    With:

    AVRDUDE_PROGRAMMER = stk500v2

    And donne! It will upload even faster than Arduino IDE!

    I love XCode, code sense etc… ;)

    And what I love most: Don’t have to call .pde to my cpp files anymore!

    Hope I helped!, Have fun!

  • Davide

    I have corrected
    ARDUINO = /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/
    AVRDUDE_DIR = /usr/local/CrossPack-AVR-20100115/bin/
    but still gives me errors and warnings:

    Print.cpp:(.text+0xa6): undefined reference to `String::operator[](unsigned int) const’
    core.a(Print.o):(.data+0×6): undefined reference to `__cxa_pure_virtual’
    make: *** [main.elf] Error 1

    there is someone who does the script ArduinoOnXcode?
    thanks

  • David

    Freak’n fantastic. A real editor at last. Genius!
    And Serial I/O is still available through the Arduino App.
    Now if we had hardware debugging!

  • Brady

    Hey So I can make the code run the blink once then it stops, any ideas?

  • avenue33

    Hi!

    Yes, very exciting!

    The basic blinking LED works!

    But when I tried a more elaborate project with Wire.h for the i2c BlinkM RGB LED, I’ve got an awful list of errors!

    Among others:

    cc1plus: warning: command line option “-Wstrict-prototypes” is valid for C/ObjC but not for C++
    cc1plus: warning: command line option “-std=gnu99″ is valid for C/ObjC but not for C++

    Any idea?

  • Mike

    Need some help for installing on xcode 4
    The template doesn’t get picked up in the tabs of x code (other)
    Anything has to be done different then the instructions above?
    Thanks in advanced

  • http://phy5ics.com phy5ics

    I ran into the same issue as Ric using Xcode 4 with an Arduino Uno. After scratching my head for a while and trying what others suggested to no avail, I thought I’d try changing the upload rate to see if that might work:

    UPLOAD_RATE = 115200

    Amazingly, it did. However, I still haven’t figured out how to get the template picked up by Xcode 4. Oh well, a minor inconvenience at this point. Thanks for the instructions.

  • Joopmicroop

    TKX worked great now I can start prototyping my own controllers for my games

  • Ronan

    I have followed the tutorial and the comments about UPLOAD_RATE and AVRDUDE_PROGRAMMER. Unfortunately. I am getting a timeout message from the avrdude program.

    In the Build Results view in Xcode I see the following:

    /usr/local/CrossPack-AVR/bin/avr-objcopy -O ihex -R .eeprom main.elf main.hex
    /usr/local/CrossPack-AVR/bin/avrdude -F -p atmega328p -P /dev/tty.usbmodemfa141 -c stk500v2 -b 57600 -D -U flash:w:main.hex
    avrdude: stk500_2_ReceiveMessage(): timeout
    avrdude: stk500_2_ReceiveMessage(): timeout

    It seems that the program never gets uploaded. When I run the make script I don’t see TX/RX on the Arduino flashing.

    My environment is an iMac running 10.6.7, XCode 3.2.5 and an Arduino Uno (Jan 2011 build).

    I’ve changed the makefile to read as follows;

    PORT = /dev/tty.usbmodemfa141 // Yes, this is the usb port and it works fine from within the Arduino IDE

    UPLOAD_RATE = 57600 // Also tried 115200

    Am I missing something fundamental here? Any help would be greatly appreciated.

  • http://sites.google.com/site/vilorei/arduino/20--arduino-makefile-for-xcode Rei Vilo

    Hi!

    Please find on my website a Xcode 4 project for Arduino UNO and chipKIT UNO32, with code.

    • Xcode 4 on Mac OS X.7
    • MPIDE 0022 based on Arduino IDE 0022

    I tested it successfully with the Arduino UNO and the chipKIT UNO32 platforms.

    The Xcode 3 template is no longer valid because Xcode 4 has changed everything about the templates.

    website page : http://sites.google.com/site/vilorei/arduino/20–arduino-makefile-for-xcode
    GitHub repository : http://github.com/rei-vilo/Xcode-for-MPIDE-Arduino

    I’d greatly appreciate some help on Xcode 4 about templates and libraries linking.

  • tim

    I have create XCode 3.2 templates that work with linking libraries, to be found here: https://github.com/timknapen/Arduino-With-XCode

  • Redsharko

    I have the same problem as Ronan… Any idea?

  • Jan

    Hi all,

    i did all the steps described above up to steps 5 & 6 but xcode simply does not see the ArduinoOnXcode template. I have put it in /Developer/Library/Xcode/Templates/Project Templates/Mac/Other on my mac.

    thanks for any help

  • http://sites.google.com/site/vilorei/ Rei VILO

    I managed to develop a template running on Xcode 4.3 for Arduino and chipKIT boards.

    See https://github.com/rei-vilo/embedXcode and http://sites.google.com/site/vilorei/

  • http://sites.google.com/site/vilorei/ Rei VILO

    I’ve developed an Xcode 4.3 template for Arduino and chipKIT boards.

    Code is here https://github.com/rei-vilo/embedXcode
    And explanations are there http://bit.ly/xy0UQU

    Enjoy and participate!

  • John Paterson

    I copied the template to the “other” folder inside the xcode application, but xcode does not see it. what am i doing wrong?