Introduction: Interfacing Your Arduino With a C# Program

About: I love writing computer programs and pulling stuff apart to "see how it works", then putting it back together again.

Ever wanted to make your own application (*.exe) to work with your arduino (or other serial communicating device)?

This instructable requires:

  - Visual Studio 2008 or later* (I am using 2010 RC, some options may differ between versions)
                       OR
  - Visual C# Express Edition 2008 or later*


  - An Arduino (Any type) or other kind of serial communicating device
  - A basic knowledge of the C# Language


* An earlier edition of VS may work, but I am not sure if it has the SerialPort library.

Step 1: Create a New Application

Open Visual Studio and create a new Windows Forms Application. Then when form 1 comes up, add as many controls as you would like, starting with the SerialPort class.

If you are adding the Arduino support to your pre-made program, then just add the SerialPort class. If you are more advanced, you may want to make a plain code file with just the SerialPort library, so that you don't keep defining it.

Step 2: Configure Your Serial Port

The only things that ever need to be changed are the
   -BaudRate (Change this to match the Arduino code (Serial.begin(this is your baud rate))
   -Port Name (When compiling and uploading you need to select a port, usually starts with COM)
   -Maybe Read buffer size, and write buffer size, only if you intend on reading/writing more data than 4096Bytes reading or 2048Bytes writing to/from the arduino. Usually this setting can stay the same.

Step 3: Using the Serial Port in Code.

On a button, or any control that has an 'event' when clicked, just double click on the control, and it will come on to the code window. Here are some codes that you can use there. IF STATEMENTS ARE THE SAME!
The majority of the code is similar to the Arduino code, however;

Arduino Code     C# Code
Boolean               bool
unsigned any     uany

random                (new System.Random()).Next()
There is no time options for C#, such as delay() delayMicroseconds().


Other Stuff (at the top?!)

serialPort1.Open();  - Opens the serial port for you to use. There will be a big nasty error if the port is already opened, or if the port is not there.

serialPort1.BytesToRead - use an if statement to compare to 0. If the result is false, then there is serial data available (if(serialPort1.BytesToRead == 0) is the same as for arduino if(Serial.available))

Talking to the Arduino

serialPort1.Write(arg); - Tells the arduino something, where arg is what you want it to say. There will be a big nasty error if the port is not opened.

serialPort1.WriteLine(arg); - same as serialPort1.Write(arg); but always adds "\n".

Reading from the Arduino

string read = serialPort1.ReadTo(arg); - Reads the serial data, until the text in arg is found, then is returned as read. Also will have an error if the port is not opened.

string read = serialPort1.ReadLine(); - Same as serialPort1.ReadTo("\n");

string read  = serialPort1.ReadToEnd(); - Keeps reading until there is no more data to read, then is returned as string read.

Step 4: Example Part 1 - C# Part

On both of the Example pages, I have attatched the source code files. To open, extract the files on to where-ever you want them to be, and open the CS folder, and double click the .csproj file.

I want to have a program that changes an RGB LED's color, each time I click a button, so I am going to need a button on my control, and a serialPort.

I added the button and serial port to my form, and then resized the button to fit.
I then added a serialport, and changed the PortName to COM4. This may be different for your computer.

I then double-clicked the button, and it changed to the code view. In button1_Click(object sender, EventArgs e) I added code (There is some error handling code in this.) :

if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
serialPort1.Write("T");
serialPort1.Close();
}
catch
{
MessageBox.Show("There was an error. Please make sure that the correct port was selected, and the device, plugged in.");
}
}

Step 5: Example Part 2 - Arduino Part

On both of the Example pages, I have attatched the source code files. To open, extract the files on to where-ever you want them to be, and open your Arduino programming environment, and then open the file found under the Arduino folder, and subfolders.

An RGB LED was connected to

R  Co  G   B  LED Pin
13 12  11 10 Ardunino Digital Pin

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(11, LOW);
digitalWrite(13, HIGH);
}

int led = 1;

void loop()
{
if(Serial.available())
{
Serial.read();
switch(led)
{
case 1:
led = 2;
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
break;
case 2:
led = 3;
digitalWrite(12, LOW);
digitalWrite(10, HIGH);
break;
case 3:
led = 1;
digitalWrite(10, LOW);
digitalWrite(13, HIGH);
}
}
}

USB Contest

Participated in the
USB Contest