Using Arduino in Xcode

While the Arduino IDE (Integrated Development Environment) is relatively simple way to program the Arduino hardware, I found myself getting frustrated by it’s limited features after spending time using Xcode and Eclipse. There are some simple tutorials available online which demonstrate how to set up Xcode for programming the Arduino, however these didn’t “just work” for me, and were targeting arduino-0.10.

I made a few (simple in hindsight) changes to the project which has worked well for me so far.

Bill of Materials

  • Xcode – provided with the Apple Developer Tools
  • Arduino IDE – needed for the core libraries for programming the Arduino
  • AVR programming tools. Easiest solution is to install the AVRMacPack.
    • You can alternatively install avr-gcc, avr-libc, avrdude and libusb via MacPorts or Fink.
  • Project Template for Arduino on Xcode.

Set-up

  1. Install Xcode. Typically at /Developer
  2. Install Arduino. Typically at /Applications/arduino-x.xx
    Update: Arduino 017+ on OS X is now provided as an application bundle. See below.
  3. Install AVRMacPack. The tools are located at /usr/local/AVRMacPack
  4. Uncompress the ArduinoOnXcode project template. Move it to /Developer/Library/Xcode/Project Templates/Other/
  5. Open the Makefile in the ArduinoOnXcode folder. Edit the ARDUINO and AVRDUDE_DIR lines to reflect your system. The default values should work “as is” with this tutorial. Note: for Arduino 017+ change the ARDUINO path to:
    /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino

Use

Create a new project in Xcode and select Other>ArduinoOnXcode. You’ll be prompted for a save location for the new project. Select a location and name and click “Save”.

The code for your sketch is written in “main.cpp”. Any custom functions you write will need to be either prototyped at the top of the file where indicated, or will have to be declared before they are used in setup() or loop(). A prototype just indicates the return value and any arguments a function requires. The blink function returns nothing (void) and expects two integer arguments, which is prototyped as: void blink(int n, int d); Again, look at the example code in the template for guidance.

Compiling/Uploading to Arduino

activetarget

Connect the Arduino via USB. Change the Active Target to “Upload”. The select Build>Clean All Targets. This will compile the code and upload it to the Arduino. The template contains a simple blink sketch to confirm that the process worked.

Serial Terminal

I recall a neat trick to enable easy access to opening a serial terminal through Xcode, but can’t find it now. In the meantime you can use the Terminal.app to open a serial connection to the Arduino:

  1. Open /Applications/Utilities/Terminal.app
  2. The the command prompt in the new terminal window type (without the quote marks),
    • “screen /dev/tty.usbserial”
    • Hit the Tab key. The rest of the serial port’s name should appear. If it doesn’t, verify that the Arduino is plugged in via serial and is powered on.
    • For example, the command for my Arduino looks like: “screen /dev/tty.usbserial-A4001Jdl”
  3. Press return to execute the screen command. The Arduino will reset and should open the serial port.
  4. You can type to send characters, but there is no local echo (ie. you won’t see the typed characters)
  5. To exit and close the serial port
    • Press Control-A then ‘k’ to “kill” the window. This just stops the screen program and returns to the command prompt.

Of course, you could use ZTerm instead…
Good luck!


Posted

in

by

Comments

32 responses to “Using Arduino in Xcode”

  1. Stefan Avatar

    Dear Robert,

    I really like your tutorial on using the arduino togehter with Xcode.

    Unfortunately, I could not get it to work with the Arduino Duemilanove (atmega328p). Any hints will be appreciated.

    Regards,
    Stefan

  2. Robert Avatar

    I don’t have a Duemilanove to test, but offhand I’d look at the Makefile. Perhaps change the MCU variable to atmega328 (or whatever value avrdude would be looking for). Do you get a specific error message?

    I do have a couple of 328’s lying around…I’ll try it out when I get a chance, but it might not be for a bit.

  3. GlynC Avatar

    Hi,

    This looks great, will be trying it out tonight when I get home.

    Did you/last poster have any success with the Duemilanove with a 328?

    Thanks,

    Glyn

  4. GlynC Avatar

    Oh and another question… by using XCode do you still have access to the Arduino ‘API’ (i.e. DigitalWrite())? I’m currently assuming that only access to avr-libc is available for development.

    Thanks,

    Glyn

  5. Robert Avatar

    I haven’t had time to try a 328, but I can’t imagine that there will be problems if you’re using an up-to-date version of the Arduino IDE (for the core files).

    I don’t seem to have CodeSense working correctly for the Arduino API, but it definitely does highlight syntax errors. However, all the Arduino language (provided by Wiring.h) is still available when editing in Xcode.

    FWIW, I’m migrating over to Eclipse for Arduino development. This is borne out of my tendency lately to simultaneously develop firmware and visualization/communication software (mostly in Processing/Java). Being able to switch between tabs in Eclipse to work on both rather than having Eclipse and Xcode open has been nice for me.

  6. GlynC Avatar
    GlynC

    Another quick question (seeing as I’m new to Mac), one of the tasks in your tutorial states:

    Open the Makefile in the ArduinoOnXcode folder. Edit the ARDUINO and AVRDUDE_DIR lines to reflect your system. The default values should work “as is” with this tutorial.

    the Makefile currently says:

    ARDUINO = /Applications/arduino-0013/hardware/cores/arduino/
    AVRDUDE_DIR = /usr/local/AVRMacPack/bin/

    I have 0017 version of Arduino, is it simply a case of adjusting the value above? If I enter /Applications/arduino-0017/ in finder it says that the folder doesn’t exist (similarly for /Applications/arduino-0013/)

    Also, the second directory I’m guessing should be:

    /usr/local/CrossPack-AVR-20090415/bin/

    ??

  7. Robert Avatar

    Precisely…you need to update these lines to point to your specific installation of the tools.
    For the Arduino IDE, look in /Applications for the installation folder…just navigate using the mouse to locate it. If all else fails try a Spotlight search (magnifying glass in the upper right corner of the menu bar)

    You’ve got the idea for the avr tools directory…I don’t know your particular installation, but point that line to wherever your tools have been installed.

    You could also refer to more documentation I put elsewhere:
    http://itp.nyu.edu/oldITPedia/Arduino_in_Xcode

  8. glync Avatar
    glync

    Hi,

    Thank you so much for your help. I’m still having trouble with the Arduino path, the latest version installs the following:

    /Applications/arduino.app

    there is no Folder. Your make file seems to want to point at source code. Is this correct? Or does it just need the arduino.app? If source is required, what do I need to do to get the source in a Mac ‘format’ ?

    Thanks again

    Glyn

  9. Robert Avatar

    Ok, it seems as though Arduino has now updated itself for parity with Processing 1.0.3…which means that the application is provided as an app bundle (which is just a special, extra-fancy folder in OS X). Right-click on the Arduino.app bundle and select Show Package Contents.

    The appropriate directories should be within it somewhere. I’m not certain what the path will be (away from my personal computer)…but it will likely be something similar to:
    /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino

    Please let us know what it turns out to actually be!

  10. GlynC Avatar

    Hi I’m back at work now but will try tonight. I did try a ‘show contents’ but couldn’t find anything specific, will look again tonight for /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino

    Thanks,

    Glyn

  11. GlynC Avatar
    GlynC

    Hi,

    So I found the folder and have updated the makefile but nothing seems to transfer to the device when I select Build > Clean All Targets.

    After some amount of trying it seems there is support issues in avr-libc and avrdude for the ATmega328.

    After even more web searching it seems that no-one is successfully programming their Duemilanove 328 with XCode.

  12. GlynC Avatar
    GlynC

    So I’ve got a little bit further, I only have 1 error (plus a whole load of warnings!). The error is:

    make: *** No rule to make target `/Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino/wiring_serial.o’, needed by `core.a’. Stop.
    Command make failed with exit code 2

    I’ve had a look and cannot find a wiring_serial.c in the Arduino.app folder.

    My Makefile settings are (note I’ve pointed AVRDUDE_DIR to the avrdude within the Arduino.app folder):

    ARDUINO = /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino
    AVRDUDE_DIR = /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin

    PORT = /dev/tty.usbserial*
    TARGET = main
    SRC = $(ARDUINO)/pins_arduino.c $(ARDUINO)/wiring.c \
    $(ARDUINO)/wiring_analog.c $(ARDUINO)/wiring_digital.c \
    $(ARDUINO)/wiring_pulse.c $(ARDUINO)/wiring_serial.c \
    $(ARDUINO)/wiring_shift.c $(ARDUINO)/WInterrupts.c
    CXXSRC = $(ARDUINO)/HardwareSerial.cpp $(ARDUINO)/WMath.cpp $(ARDUINO)/Print.cpp
    MCU = atmega328p
    F_CPU = 16000000
    FORMAT = ihex
    UPLOAD_RATE = 19200

  13. Robert Avatar

    It looks like they’ve removed wiring_serial.c in Arduino016….if you’re targeting a 328, what happens when you remove the missing file from the Makefile? i.e.

    SRC = $(ARDUINO)/pins_arduino.c $(ARDUINO)/wiring.c \
    $(ARDUINO)/wiring_analog.c $(ARDUINO)/wiring_digital.c \
    $(ARDUINO)/wiring_pulse.c \
    $(ARDUINO)/wiring_shift.c $(ARDUINO)/WInterrupts.c
    CXXSRC = $(ARDUINO)/HardwareSerial.cpp $(ARDUINO)/WMath.cpp $(ARDUINO)/Print.cpp

  14. Nick Avatar

    @Robert,
    Tried your revision with 0017 and an atmega 168 (decemilia) with no luck. Here’s the last errors:

    In file included from /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino/wiring_private.h
    warning: #warning “This file has been moved to .”
    warning: command line option “-Wstrict-prototypes” is valid for C/ObjC but not for C++
    warning: command line option “-std=gnu99” is valid for C/ObjC but not for C++
    Command make failed with exit code 2

    any thoughts?

  15. Robert Avatar

    What is the full build log? Those last two warnings shouldn’t kill the compile. Where’s the error message?

    I was getting this error when I switched to 0017:
    core.a(Print.o):(.data+0x4): undefined reference to `__cxa_pure_virtual’

    Workaround is to place this somewhere in main.cpp {I put it above setup()}:
    // this is for a bug in Arduino 017:
    extern “C” void __cxa_pure_virtual() { while (1); }

  16. James Avatar
    James

    This works great with the ATMega328 on the Duemilanove. Modify the makefile as follows:

    1. Remove the reference to wiring_serial.c from line 71 or so, leaving the reference to wiring_pulse.c alone
    2. MCU = m328p
    3. UPLOAD_RATE = 57600
    4. add to the end of AVRDUDE_FLAGS: -D
    5. Make sure AVRDUDE_DIR is right, ie these days it might be /usr/local/CrossPack-AVR/bin/ due to renamed package
    6. make sure PORT is a valid name in /dev/ (wildcards ok)

    works!!

    Thanks
    jt

  17. Robert Avatar

    Thanks for the config options for the 328!
    -r

  18. GlynC Avatar
    GlynC

    Hi,

    I’m still having issues.

    Have reinstalled everything, followed the instructions but I’m getting a whole load of errors relating to the pin mappings seemingly:

    error: MCU ‘m328p’ supported for assembler only

    warning: #warning “device type not defined”

    /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino/pins_arduino.c:365: error: ‘DDRB’ undeclared here (not in a function)

    and so on…

    Any suggestions?

    I’ve tried both Arduino 0017 and 0018, I’m using the latest CrossPack-AVR package (2010)

  19. GlynC Avatar
    GlynC

    if I change the MCU to atmega328p I get 17 warnings plus one error:

    Command make failed with exit code 2

  20. GlynC Avatar
    GlynC

    Bit more detail…

    If I try to ‘make’ with atmega328p or atmega328 I get 17 warnings of the type:

    /usr/local/CrossPack-AVR-20100115/lib/gcc/avr/4.3.3/../../../../avr/include/avr/delay.h:36:2: warning: #warning “This file has been moved to .”

    /Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino/pins_arduino.c:365: warning: initialization makes integer from pointer without a cast

    And the following Error:

    /usr/local/CrossPack-AVR/bin/avr-gcc -mmcu=atmega328 -I. -gstabs -DF_CPU=16000000 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino -Os -Wall -Wstrict-prototypes -std=gnu99 -o main.elf main.cpp -L. core.a -lm
    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++
    core.a(Print.o):(.data+0x6): undefined reference to `__cxa_pure_virtual’
    make: *** [main.elf] Error 1
    Command make failed with exit code 2

  21. george Avatar
    george

    I had the same problem. Changing the compile and linker flags in the makefile to the following fixed the error for me.

    CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -Wl,–gc-sections

    CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CTUNING)
    CXXFLAGS = $(CDEFS) $(CINCS) -O$(OPT) $(CTUNING)
    LDFLAGS = -lm -Wl,-gc-sections -mmcu=$(MCU)

  22. Ken Rogers Avatar
    Ken Rogers

    Hey fellas. This last fix was what i needed. Unfortunately there is a odd character paste problem that left me scratching my head for like 3 hours. It ends up that George had the right answer but the character RIGHT before “gc-sections” is coming across as the wrong character and not a true hyphen on OS X. If you keep getting errors just select the “-” and type hyphen on yer keyboard to correct it.

    After that all my errors go away! Great post, tyvm..

  23. Lawrence Johnston Avatar

    First let me say that this is definitely the best tutorial for setting up the Arduino in Xcode I found.

    However, using my Duemilanove with ATMEGA328-PU chip from Adafruit I initially received continual stk500_2_ReceiveMessage(): timeout messages whenever I tried to upload a program via Xcode. I had no issues uploading via Arduino.

    By enabling verbose mode in Arduino I was able to determine that line ~112 needed to be changed to AVRDUDE_PROGRAMMER = stk500v1 (from stk500 with no version number). After that everything worked great.

  24. SpacyRicochet Avatar
    SpacyRicochet

    I’m currently trying to get this to work on my Mac. I’m currently using a Arduino Mega to test it with.

    I’ve followed the tutorial, followed James suggestion except for the following:

    MCU = atmega1280

    I should not that I have no idea how to make sure if PORT is something valid, so I left it default. After that I also george’s suggestion, including Ken’s remark about the faulty hyphen.

    Right now I still 41 warnings and an error. Full build log is here: http://pastie.org/991666
    However, the relevant part is probably:

    /usr/local/CrossPack-AVR/bin//avr-gcc -mmcu=atmega1280 -I. -gstabs -DF_CPU=16000000 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino -Os -Wall -Wstrict-prototypes -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -Wl,\320gc-sections -o main.elf main.cpp -L. core.a -lm -Wl,-gc-sections -mmcu=atmega1280
    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++
    /usr/local/CrossPack-AVR-20100115/lib/gcc/avr/4.3.3/../../../../avr/bin/ld: \320gc-sections: No such file: No such file or directory
    make: *** [main.elf] Error 1

    I tried searching the makefile for the apparantly guilty \320gc-sections, but I can’t seem to find it anywhere, so I’m at a loss. Any ideas?

  25. SpacyRicochet Avatar
    SpacyRicochet

    Hah! Shows me not to check things more thoroughly.

    gc-sections appears TWICE in the makefile, so I need to replace both hyphens with an actual hyphen, as per Ken Roger’s suggestion. After that I got the same timeout issues that Lawrence Johnston had, but I followed his suggestion and now it works fine.

    Thanks for this post and thanks to all the great commenters as well.

  26. Stephen Braitsch Avatar

    Excellent post. Had some trouble myself getting this to work with my ATMega328 Duemilanove but finally did after stitching together the fixes posted in the various comments. I zipped up an updated project template & Makefile w/ my changes here:
    http://quietless.com/zip/ArduinoOnXcode.zip

    More details that expand on this post can be found here:
    http://quietless.com/kitchen/setting-up-xcode-to-compile-upload-to-an-arduino-atmega328-duemilanove/

  27. […] shuffled around breaking peoples xcode and arduino setup. This tutorial is a modified version of Robert Carlsen’s. I found I had to make several modifications to get it working for […]

  28. Robert Atkins Avatar
    Robert Atkins

    I’m trying to get this working on a Uno, which I believe
    has different USB/serial hardware than previous Arduino models?
    Anyhow, I’ve changed the makefile to say: PORT = /dev/tty.usbmodem*
    … but I’m getting a whole series of error messages: avrdude:
    stk500_2_ReceiveMessage(): timeout And it doesn’t look like the
    sketch is uploading to the board (rx/tx LEDs aren’t flashing.)
    Anybody got any hints?

  29. Davide Avatar
    Davide

    Also I am trying to work with Xcode ArduinoUno but gives me
    this error —- “rm -f main.hex main.eep main.cof main.elf \
    main.map main.sym main.lss core.a \ make: *** No rule to make
    target
    `/Applications/Arduino.app/Contents/Resources/Java/hardware/cores/arduino//pins_arduino.o’,
    needed by `core.a’. Stop. — someone managed to make it work
    ?

  30. avenue33 Avatar
    avenue33

    Hello,

    I tried Nick’s modified version of ArduinoOnXcode template plus all the hints

    • values for the MCU, upload speed and the protocol according to file at
    Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/boards.txt

    • on Makefile, add $(ARDUINO)/WString.cpp to line 73 before $(ARDUINO)/Print.cpp

    • on Makefile, change line 67 from PORT = /dev/tty.usbserial* to PORT = /dev/tty.usbmodem* # works

    Check at http://www.makesomecode.com/2010/07/30/programming-arduino-with-xcode/

    Enjoy!

  31. avenue33 Avatar
    avenue33

    Hi!

    I can’t manage compiling projects involving a library. After some research, it seems to be related to the link options.

    Full report at http://arduino.cc/forum/index.php/topic,49956.0.html

    Any idea?

    Thank you and best regards,

  32. mark windrim Avatar
    mark windrim

    Anyone have a version of this template for xCode 4.2 ?

Leave a Reply