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.
- For reference: Original source of the ArduinoXcode project template.
- Install Xcode. Typically at /Developer
- Install Arduino. Typically at /Applications/arduino-x.xx
Update: Arduino 017+ on OS X is now provided as an application bundle. See below.
- Install AVRMacPack. The tools are located at /usr/local/AVRMacPack
- Uncompress the ArduinoOnXcode project template. Move it to /Developer/Library/Xcode/Project Templates/Other/
- 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:
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
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.
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:
- Open /Applications/Utilities/Terminal.app
- 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”
- Press return to execute the screen command. The Arduino will reset and should open the serial port.
- You can type to send characters, but there is no local echo (ie. you won’t see the typed characters)
- 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…