Tutorial: C# Communicating with Arduino over Serial


Category : Arduino, Electronics, Windows

In this tutorial I’ll try to show you how to easily tell an arduino what to do from a C# program using serial communication. The only thing the C# program does is send a byte to the arduino, and the arduino program will then set pin 13 to HIGH or LOW.

Lets start with the C# sourcecode, it’s written in C# Express 2010 which you can download for free here. Get the C# project files:

Download CSharpEasySerialSource.zip

I tried to comment the code so that it should explain most of the stuff going on. But it’s pretty basic, you select the current comport then hit Connect. If you selected the correct port that the arduino is using it should now be connected. The textbox only takes numbers, writing text there will only result in it being converted to bytes.

When you click Send it will try to send the contents of the textbox to the arduino. In my example code I used “0” and “1” for turning off an on the LED. Lets look at the Ardunio example:

What the program does it’s listening on the serial port. And when it receives a “1” pin 13 goes HIGH, and if it receives “0” it goes LOW. Pin 13 is in most cases a LED thats on the arduino board, so you only need a bare arduino to test it. And if you don’t know any C# and just want to use the program to control a arduino I’ve created a zip file containing only the .exe file for the program:

Download CSharpEasySerial.zip

Comments (11)

Can you show how to communicate with arduino via string messages, like “turn left,90”, or “drive 1,500” and maybe just “start”, “stop”?
It’s seems to me really difficult.

Yeah, it’s a bit harder. I don’t use string messages, but I send a number first, for example 1 which tells the arduino it’s the servo thats should be moved and right after one I send for example 90.

The hard part is the arduino code. I’ll try to put that into another tutorial. For now I only have this arduino example you can have a look at. Get it here:


to do this you should learn
string first.

good programmer never ask such questions.

OK. But read status led with c# how???

Your thinking about reading from a arduino to C#? Thats actually very simple, I’ll make another tutorial for that.

Thanks for your comment =)

You can have a lot of functions. In my case i work via strings and 3 arguments. But i have 1 little problem. i read serial in main while and haven’t buffer. I want to control steppers (generate impulses) and read serial simultaneously.
Thank you for respond.

I just would like to say that this was very helpful. 🙂

How can I print the status of the LED on the Windows form??? for example if the LED is off, on the Windows Form a text will appear and prints “LED is off” ?? in other words, how can I send the arduino status to the computer as a feedback.??

Great tutorial :D.
I would also like to know how to read a led’s status, or especially a pushbutton’s status in C#.

hi!! i would like to know on how to interface lcd with arduino when it receives data from visual studio/basic 🙂 tq