Click here to Skip to main content
12,759,636 members (33,687 online)
Click here to Skip to main content
Add your own
alternative version


166 bookmarked
Posted 21 Apr 2014

Arduino, C#, and Serial COM Ports

, 10 Oct 2012 GPL3
Rate this:
Please Sign up or sign in to vote.
How to send a command to an Arduino board using C# and a COM port.
This is an old version of the currently published article.


The Arduino boards provide an easy-to-use environment to control electrical components such as LEDs and motors. I have done this project with an Arduino Mega 2560 microcontroller board, but you can use nearly any official Arduino board.

What do you need for the tutorial:

  • An Arduino board, connected to your computer by USB
  • The official Arduino IDE (download from here)
  • The Arduino board driver must be installed as described here


For a project I had to learn how to deal with COM ports and C/C++. However, to bring some fun into the whole story, I decided to buy an Arduino Mega 2560 board. It has an USB hardware plug to upload the software. The USB plug can be used to send and receive commands on the board by using a COM Port.

About the board

The Arduino Mega 2560 board has 54 I/O pins. If you are interested in more details, you can find them at the Arduino Product Page.

Using the code

The Arduino source code

Please note: I wrote the source code with the official Arduino IDE (download from here). When you use the official Arduino IDE, every Arduino program must have at least two methods: setup() and loop().

Code in the setup() method is processed right after connecting the board to power, or updating the software on the board. After the code in the setup() method is processed, the board starts processing the code in the loop method. Code in the loop method processed continously forever (yes, something like a while(true)-loop) until you plug out the power.

First, we are going to define our constants:

/*   CONSTANTS                                                   */
#define LED_TURN_ON_TIMEOUT  1000 //defines how long the LED stays powered on
#define LED_PIN 13                //Pin number on which the LED is connected
#define SERIAL_BAUDRATE 9600      //Baud-Rate of the serial Port

Afterwards, we have to write our setup()-method:

The setup method is executed once after the bootloader is done
with his job.
void setup(){
  //setup the LED pin for output
  pinMode(LED_PIN, OUTPUT);
  //setup serial pin

This method defines that the pin number 13 (constant LED_PIN has the value 13) on the Arduino board is used for output ( though in our case it is an LED, but it could be for example a relais too).


This method initalizes the serial USB port on the board to listen for incoming information.

So far so good. But now, we need a method which is listening for the information from the USB serial device.

Because I don't want to get this whole thing more complicated as it already is, I write the whole code into the loop()-method:

The loop method is executed forever right after the setup method
is finished.
void loop(){
  int incomingByte = 0;
  if (Serial.available() > 0) {//Only when serial port is avaible
     // read the incoming byte:
     incomingByte =; //read incoming serial data
     //Light up LED for specified amount of milliseconds
     digitalWrite(LED_PIN, HIGH); 
     digitalWrite(LED_PIN, LOW);
incomingByte =; //read incoming serial data

incomingByte =; //read incoming serial data

This command reads the serial input. Maybe I am going to add an explanation about how to process different commands.

At this point, the execution of the sketch code is stopped until the board receives any information from the serial interface.

After the board has received information, we can continue with turning the LED on and off:

//Light up LED for specified amount of milliseconds     

digitalWrite(LED_PIN, HIGH); 
digitalWrite(LED_PIN, LOW);

Afterwards, the board starts the execution of the loop()-function again.

The C#/WPF source code

The C# code is a simple thing, you can send commands to the board by using the SerialPort class which is avaible in the namespace System.IO.Ports of the .NET Framework. More information about the port object is avaible on the corresponding MSDN Page.

Points of Interest

Arduino provides a huge amount of possibilities on working with hardware and connection to a computer. Last but not least, at the official Arduino homepage is a large community support avaible.


  • 10-October-2012: Created the article.
  • 10-October-2012: Updated (thanks to Mike Hankey and OriginalGriff).


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Marco Bertschi (SFC)
Software Developer (Junior)
Switzerland Switzerland
Software Developer (Swiss Federal VET Diploma), experienced with Qt C++, Arduino Boards, multiple communication technologies (the usual Webservices, TCP/IP bunch as well as serial port communication), and C# including WPF, Entity Framework and COM libraries built using the .Net Framework.
Music enthusiast, runner, part-time psychologist for friends, awesome guy and Air Force Sergeant First Class.

You may also be interested in...

Comments and Discussions

Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
QuestionGood Work. Pin
ArchieF16-Mar-16 23:49
memberArchieF16-Mar-16 23:49 
AnswerRe: Good Work. Pin
Marco Bertschi17-Mar-16 4:20
protectorMarco Bertschi17-Mar-16 4:20 
QuestionMessage Removed Pin
Member 112468614-Dec-14 6:09
memberMember 112468614-Dec-14 6:09 
GeneralMy vote of 5 Pin
Member 1102292519-Aug-14 13:00
memberMember 1102292519-Aug-14 13:00 
GeneralRe: My vote of 5 Pin
Marco Bertschi24-Aug-14 7:52
protectorMarco Bertschi24-Aug-14 7:52 
Question3DChart missing? Pin
Reactory2-Jun-14 14:39
professionalReactory2-Jun-14 14:39 
AnswerRe: 3DChart missing? Pin
Marco Bertschi3-Jun-14 2:03
protectorMarco Bertschi3-Jun-14 2:03 
GeneralRe: 3DChart missing? Pin
Reactory3-Jun-14 7:33
professionalReactory3-Jun-14 7:33 
GeneralRe: 3DChart missing? Pin
Marco Bertschi3-Jun-14 8:37
protectorMarco Bertschi3-Jun-14 8:37 
QuestionEOT... standard? Pin
Member 1084688827-May-14 8:50
memberMember 1084688827-May-14 8:50 
AnswerRe: EOT... standard? Pin
Marco Bertschi27-May-14 9:02
protectorMarco Bertschi27-May-14 9:02 
QuestionMessage Automatically Removed Pin
23-Apr-14 10:06
memberISpliter23-Apr-14 10:06 
GeneralMy vote of 5 Pin
Volynsky Alex21-Apr-14 22:49
professionalVolynsky Alex21-Apr-14 22:49 
André Pereira25-Jan-14 3:38
memberAndré Pereira25-Jan-14 3:38 
Marco Bertschi25-Jan-14 4:28
professionalMarco Bertschi25-Jan-14 4:28 
Newsexactly what i was looking for... Pin
~dgn1274-Dec-13 19:11
professional~dgn1274-Dec-13 19:11 
GeneralRe: exactly what i was looking for... Pin
Marco Bertschi4-Dec-13 19:47
professionalMarco Bertschi4-Dec-13 19:47 
QuestionArduino hardware Pin
Plamen Kovandjiev28-Nov-13 2:43
memberPlamen Kovandjiev28-Nov-13 2:43 
AnswerRe: Arduino hardware Pin
Marco Bertschi28-Nov-13 4:32
professionalMarco Bertschi28-Nov-13 4:32 
QuestionYou mention my Article!! Pin
glennPattonWork26-Nov-13 23:58
professionalglennPattonWork26-Nov-13 23:58 
AnswerRe: You mention my Article!! Pin
Marco Bertschi27-Nov-13 2:58
professionalMarco Bertschi27-Nov-13 2:58 
GeneralMy vote of 5 Pin
Abinash Bishoyi9-Apr-13 7:01
memberAbinash Bishoyi9-Apr-13 7:01 
QuestionCommands? Pin
kovacsgellert6-Jan-13 5:14
memberkovacsgellert6-Jan-13 5:14 
AnswerRe: Commands? Pin
glennPattonWork27-Nov-13 4:00
professionalglennPattonWork27-Nov-13 4:00 
QuestionRe:Arduino, C#, and serial interface Pin
iykedapotential19-Nov-12 2:55
memberiykedapotential19-Nov-12 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 10 Oct 2012
Article Copyright 2014 by Marco Bertschi (SFC)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid