Controlling an Arduino With Cocoa (Mac OS X) or C# (Windows)




Introduction: Controlling an Arduino With Cocoa (Mac OS X) or C# (Windows)

 No I'm not talking about controlling an Arduino with a chocolate bar or cocoa powder. I'm talking about the programming language, Objective-C. So what is cocoa? Cocoa is a set of frameworks written in the programming language of Objective-C for Mac OS X. No there isn't a test at the steps so you don't have to worry about remembering all of this. Just know that when I talk about cocoa I'm not trying to make you hungry.

 Now I understand some people are probably wondering why you would want to use Cocoa instead of Processing, the reason I have is: if you use Cocoa over Processing you can have a full Computer Application(e.g. Safari, Mail, iCal) that can have a physical output. (e.g. Lights, motors, servos)

So Have Fun, Work hard, & Play nice! Please ask any questions you have!

- This will only work for Mac OS X & Windows
- The source code is attached below.
- If you don't want to code the app, or you don't have xcode, you can find the app for OS X in the source below under: 'Build -> Debug'
- The instructions for Windows are in step 7

Step 1: Stuff You Need:

- Arduino
- A-B USB Cable
- Computer with Mac OS X

- Xcode
- Arduino IDE

Step 2: Coding the App

Hopefully this makes sense. If you need help please ask any questions you have.
Note: If you don't want to code the app, or you don't have xcode, you can find the app in the source code download under: 'Build -> Debug'

Create the project:
 1) Open Xcode
 2) Press 'Command + shift + n' This will open the new project dialog
 3) Select 'Cocoa Application' under "Mac OS X -> Applications" Press Next
 4) Call the app what you want. I'm calling mine "Arduino Controller"

Add your class:

 5) Once the project is created, Press 'Command + n'
 6) Select the Objective-C Class option from the Classes option under the Mac OS X section
 7) Call it MainController. Then press finish

Add the code:

 8) Open MainController.h
 9) After the '}' & before the '@end' add the code:



 10) Open MainController.m
 11) After the code ''@implementation MainController" & before the '@end' add the code:


popen("echo i > /dev/tty.usbserial-A6006hmi", "r");



popen("echo o > /dev/tty.usbserial-A6006hmi", "r");


Finishing The Coding:

12) Press 'Command + b' & press save all
13) Double click on MainWindow.xib. It is located under Resources

14) Interface Builder will open

Step 3: Designing the GUI (Graphic User Interface)

If you have questions please ask them!

Creating the interface:
1) With interface builder open, find the buttons. Pick a style you like & drag 2 of them over to the window
2) Double click on them to rename one "On" & the other "Off"
3) Resize the window if you want
4) In the library find the thing that looks like a blue cube (NSObject) & drag it to the window with the list of files
5) Click on the object you just added to the window
6) Press 'Command + 6' to open the Identity Inspector
7) Under the section 'Class' type MainController
8) Press 'Command + 5' to open the Connections Inspector
9) Click & drag the little circle to each of the respective buttons. (e.g. ledOn to the On button)
10) Save & close Interface builder
11) Press "Build & Go" In Xcode to run the cocoa app

Step 4: Programming the Arduino

Uploading the sketch to your arduino:
1) Open the .pde file, in the source download, in the Arduino IDE
2) Connect your arduino & press Upload to I/O Board
3) Once the file is uploaded open the serial monitor
4) Go to the Arduino Controller program. If it's not running open it.
5) You can now use the On & Off buttons to control the led built in on pin 13 the arduino

Step 5: Using, Notes, & More Ideas

To use the controller, make sure the serial monitor in the Arduino IDE is running. This is important!
To use the app you can either press build & go in xcode or find the app in the xcode project under:
'Build -> Debug'

- This will only work on Mac OS X
- The Source code is attached on the first step.
- If you don't want to code the app, or you don't have xcode, you can find the app in the source code under: 'Build -> Debug'

- Make it so there are two buttons that control a servo's rotation
- Make a small piano
- Make a physical output for your mac
- Make a pan & tilt webcam

Step 6: How This Works

In the cocoa application there are 2 methods, ledOn & ledOff, that are activated by the pushing of the buttons. Within each of those methods there is a line of code that looks like this:
popen("echo i > /dev/tty.usbserial-A6006hmi", "r");
This code sends the letter i to the Serial port: /dev/tty.usbserial-A6006hmi which is received by the arduino

With in the arduino program there is the code:
if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte =;
    if(incomingByte == 105){
      digitalWrite(ledPin, HIGH);
    else if(incomingByte == 111){
      digitalWrite(ledPin, LOW);
This takes the decimal value from the sent character on the serial port connection. If it is a i then it turns the led on if it is a o it turns it off. 

For the Arduino to receive the signal from the cocoa app the serial monitor in the Arduino IDE needs to be open.

If this doesn't make sense it's fine. This is kind of a reference if you want to expand the functionality of the program.

Step 7: Coding in C# (Windows)

 Well I finally got the windows part done!! It turns out you don't have to leave the Adruino IDE open when you use C# for programming. You need either Visual Studio or Visual C# express (Free). If you don't want to write the app the setup is included in the source code file at the intro. I would NOT recommend trying to write this program if you don't have experience writing code in C#.

Here are the steps:

Picture 1) Create a new project
Picture 2) Choose "Windows Form Application" & Name it what you want
Picture 3)
This is what you should have
Picture 4)
Find the button in the Toolbox
Picture 5) 
Get 2 buttons & drag them to the window
Picture 6)
Left click on the first one. Under properties, go to title & rename it "On"
Picture 7)
Rename the second button & resize the window
Picture 8)
Select both of the buttons & double click them to create methods for them
Picture 9)
Paste in the code attached below

Arduino Contest

Participated in the
Arduino Contest

Be the First to Share


    • Big and Small Contest

      Big and Small Contest
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Make It Bridge

      Make It Bridge



    4 years ago

    Very interesting post! I have some problems:

    1) When i open the "Serial monitoring" in Arduino IDE, the Cocoa application blocks as the port resource is busy

    2) When i click on one of the two buttons, tx/rx lights flicker on the Uno card and the LED_BUILTIN goes on for less than a second, then goes off but never stabilises

    3) In the Arduino IDE serial monitor, when i send "i" or "o", the LED_BUILTIN led switches on and off correctly

    I'm running MAC OSX Sierra and get a warning saying that my target OS is lower than the deployment target (which i had to change in the project properties)

    Thank you for your help



    Reply 6 months ago

    After 4 years I repeated the test and added 3 instructions in the setup() of the Arduino sketch, to switch the led ON for 2 seconds. I noticed that this happens each time i click on one of the two buttons in the Cocoa program when the Monitor window is NOT open in the Arduino IDE, meaning that the Arduino is reset each time a character is sent through the port. When the monitor window is open, no character can be sent because the port is "busy" (message in the Xcode console). So i have 2 questions:
    1) How can i avoid resetting the Arduino when sending a charater to it? (Monitor window not open)
    2) How can the Xcode program access the port when the Monitor window is open?
    Thank you,


    6 years ago

    I had some problems here: the arduino resets it self everytime it receives a serial signal. Is that a software or hardware problem?


    9 years ago on Step 5

    sh: /dev/tty.usbmodemfd131: Permission denied appear in Xcode 5.0.2
    Can't turn on the LED. Can anyone help ? Please


    10 years ago on Step 2

    Hello, nice tutorial!

    I have a problem here, when building the code. When I press command + b, i get the "Expected specifier-qualifier-list before '-' token" error on the lines


    of manicontroller.h file.

    Im using Xcode 3.2.6 on a SLeopard 10.6.8

    Do you people have a clue on this problem??

    Thank you very much


    10 years ago on Introduction

    Hello, well i have a problem, when I click in the button ON it doesnt turn on the led, so i think the problem is in the Baud because it need to be in 9600, so I want to know if you know how to change the Baud in the popen() function.


    11 years ago on Introduction

    I'm trying to modify this for picaxe (using the same code for cocoa), and I keep getting an error message in Xcode: sh: /dev/tty.usbserial-000013FD: Resource busy. Any idea what I should do?

    Very informative. I find "Processing" very interesting in interfacing and controlling the serial port of an Arduino by Mac.


    12 years ago on Introduction

    I pass through your article when I was seeking a simple way to communicate with my Arduino from the Internet. After many days of unsecessful reseach, I decided to make one application of my own.

    So I made up a Mac application that can relay strings send from the Internet to a connected serial device and vice versa. I now think I must share because this app is very useful for me. So maybe you'll find that too!


    Reply 11 years ago on Introduction

    interesting, I'm sure I'll find a use for this. Thanks


    11 years ago on Introduction

    Greatly appreciated.
    My Arduino is in the post, can't wait to get started.


    12 years ago on Step 4

    Thanks! It worked great but I had to change the serial port number from the one you gave (A6006hmi) to my Arduino's own serial port number. It took my a while so maybe you could write in that you have to use your serial port number and not the one you give. Thanks again!


    12 years ago on Step 1

     does it matter what version OS X i have


    13 years ago on Introduction

    Thanks for the great info! Is it just as easy to get info back from the Arduino to display on the screen? I want to make a bicycle computer that displays all of its info in Mac OS X... does that seem like something that would be relatively easy using this method?


    13 years ago on Step 6

    I wrote up a wiki page about how to do this a while ago located here:
    It looks like its been changed a little since I wrote it, but I wrote up the part on using Popen().

    Also, you have to change the part in the code after the tty.usbserial to whatever port your Arduino is on. So you have to look in the Arduino program under Tools/Serial Port then replace the "/dev/tty.usbserial-A6006hmi" thats in this code to the selected serial port in the Arduino program. Read my post for more info about this.


    13 years ago on Introduction

    cool code, only thing is, when i use the serial monitor to send an i or o to the arduino it works, but the program wont...

    i needed to change the com port to com4 because thats what my arduino uses, and when i press on or off the RX light goes on for a short time but it doesnt trigger...

    any ideas?


    Reply 13 years ago on Introduction

    oh, nevermind, after a quick arduino reset it recognizes it. nice :)