Introduction: Controll Leds With an Arduino From a C# Program
Have you ever wanted to control some LEDs with your Arduino from a program you have written? Well I'm here to help you with just that. This is my First 'able so please help me out if i mess up along the way. And feel free to add on and change any of the code to fit your needs.
Step 1: Open a New Project
Open up Microsoft Visual Studio 2010 or Microsoft Visual C# 2010 and make a open a new C# Project and name it what ever you want. I will call mine "Arduino Led Controller". After you name it, click okay.
Step 2: Add Your Controlls
For this example, i will do 2 simple buttons to turn an led connected to pin 13 on and off. First Open the toolbox and click and drag the button onto the screen. Do this twice. Then add two pannels and resize the form, view the pics to see this.
Also add a serial port from the toolbox
Step 3: Rename Your Buttons
You can now rename your buttons or keep them the same. i will keep them the same, but i will change the text. do this by clicking on the button and going to the properties on the right side of the screen and changing the text section. shown below. Make sure you have the correct button selected when you are trying to change its text.
Step 4: Add the Code
Double click on the On button, and you should get a screen that looks like the first picture. Then in the block of the code add this code:
serialPort1.Open();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("A");
}
serialPort1.Close();
panel1.BackColor = Color.Lime;
panel2.BackColor = Color.Transparent;
then go up to the code:
public Form1()
{
}
and add this:
InitializeComponent();
serialPort1.PortName = " ";
serialPort1.BaudRate = 9600;
in between the brackets.
we will add the PortName value in a later step.
Now go back to the design page and double click on the off button and add this code:
serialPort1.Open();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("a");
}
serialPort1.Close();
panel2.BackColor = Color.Red;
panel1.BackColor = Color.Transparent;
Step 5: Arduino Code
okay for the arduino code you have to receive messages from a serial port. so this is the only way i know how to do this. Please forgive me if there is a different way for i have just started programming with the arduino.
int message = 0; // This will hold one byte of the serial message
int LEDPin = 13; // This is the pin that the led is conected to
int LED = 0; // The value or brightness of the LED, can be 0-255
void setup() {
Serial.begin(9600); //set serial to 9600 baud rate
}
void loop(){
if (Serial.available() > 0) { // Check to see if there is a new message
message = Serial.read(); // Put the serial input into the message
if (message == 'A'){ // If a capitol A is received
LED = 255; // Set LED to 255 (on)
Serial.println("LED on"); // Send back LED on
}
if (message == 'a'){ // If a lowercase a is received
LED = 0; // Set LED to 0 (off)
Serial.println("LED off"); // Send back LED off
}
}
}
Attachments
Step 6: The Setup
First you need to take an led and connect it to your arduino like the photo below. make sure the anode(+) is in digital pin 13 and the cathode(-) is in ground.
Then you need to open up the arduino ide and add the code attached below. Then go to the menu bar of the program and go to tools/board and choose the correct board that you are using. i am using the Dueminelove. then go to tools/serial port and note the name of yours. in my case it is COM4.
sorry for the poor photo quality, i had to use my phone. My camera broke this morning.
Attachments
Step 7: Adding the Serial Port Number to the C# Program
Go back to The C# project and find this code:
serialPort1.PortName = " ";
in the blank quotes change it to whatever your serial port is.
then you're going to have to go to the design screen and click the serial port at the bottom left of the screen. Go to the properties section and change the PortName to the same thing you did as the code. in my case it is COM4.
Step 8: Test Your Program
Upload your sketch to the arduino, and test it with the serial monitor. Type a capitol A for on and a lowercase a for off. then go to the C# program and click the start debugging button. its the play button on the top tool bar. (or click the button f5) and click the on button, the led should go on. click the off button and the led should go off. Voila! you just made a program that talks to an arduino!
Now you can add more leds and more functions! have fun!! below i have attached pics of my controller! have fun playing with this! i will attach the code and what not below for the whole project.

Participated in the
LED Contest
27 Comments
6 years ago
I use the NusbioMCU device which designed to control multiple shape of RGB LED
WS2812 from C#, VB.NET or Powershell. It is just plug and play.
7 years ago
worked like a charm. I used it to controll a servo based on date in a sql database. THANKS!
7 years ago
I have tried this code for com3 on my pc but it says access denied kindly help me
8 years ago
Which ARDUINO model do we need for this tutorial? Can you please share the product link.
Thank you
Reply 8 years ago
The arduino I used in this instructable is the Duemilinove however i would assume you could use any board just edit the code accordingly.
10 years ago on Introduction
Hello!, nice tutorial, what changes are necessary in the VB code to control leds with keyboard instead of buttons?
Reply 8 years ago on Introduction
We can Do this but not through VB...This can be done using a Processing sketch.
I press 1,2,34, keys on the keyboard and accordingly led's turn on......
I can make an instructable on this if you want.
Reply 10 years ago on Introduction
I don't entirely understand what you mean? Could you elaborate please and maybe I could help you out
9 years ago on Introduction
Hi , i try this code.... in my PC my arduino was in COM11... but when i use that port in C# program then the program failed. it was showing "Access is denied" . how can i access COM11 in C# program
Reply 8 years ago on Introduction
You mustn't to access COM11 in C# program. If you have linux, you can unlock that port from one command (for Ubuntu):
sudo chmod a+rw /dev/COM11
8 years ago on Introduction
did you solved the connection lost problem if your arduino disconect the program will crash imidietli
9 years ago on Introduction
Hi the seaker. Awesome thread. I'm a complete noob with Arduino and Visual Studio but your 'how-to' has helped me understand how the two communicate.
One problem though... I am receiving this error message....
"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.dll
Additional information: Access to the port 'COM4' is denied."
Now COM4 is definitely where my Arduino is plugged in, could there be something up with the code? Looks as though the port is opened already?
You probably wont read this but if anybody else has encountered this problem and overcome it, I would be very grateful to know how.
Thanks guys!
Frankie
9 years ago on Introduction
Hey dude nic work....i have a small problem...im using arduino mega...i wnt to control 40 LEDs.plz can put me the C# code please...thnx :)
10 years ago on Introduction
Hello the seaker, I want to control 4 LED but I want to use my laptop keyboard.
I need to create a C# program but I dont Know how to write the VB code so that the program communicates with arduino each time I press some key (A,S,D,F, or direction keys), each key turns on a different LED.
Thanks for the help.
Reply 10 years ago on Introduction
Hmm I don't know exactly how to do it but I have a few ideas. You can use a textbox that waits for updates,when a letter is typed in have it send a serial signal to the arduino that the arduino will process and then turn the led on or off depending on the state. It shouldn't be that heard I think, but I'm sure we can find you some more detailed help somewhere here.
Reply 10 years ago on Introduction
Thank you the seaker, I am still browsing on the internet to find out a way to do that.
11 years ago on Introduction
nice instruct,
Im new into the microconrollers but have mess with programming in vb6, asp3.0, js ect and last year got into c#. this is perfect for many things, I plan to adopt it to change eeprom values from the c# prog. Maybe to store a patterns/modes for some leds or the likes.
But untill my shift reg's get here i may just make a simple prog like you have.
regards.
Reply 11 years ago on Introduction
Thanks for the feed back! And if you come up with a updated program let me know, id love to see what you can come up with!
12 years ago on Introduction
How do you show the serial feedback in visual studio?
Reply 12 years ago on Introduction
Use the Serial Monitor in Arduino.
I only wish it did hex.