Instructables

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.

 
Remove these adsRemove these ads by Signing Up

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;


cpadilla109 months ago
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.
the seaker (author)  cpadilla109 months ago
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.
Thank you the seaker, I am still browsing on the internet to find out a way to do that.
cpadilla109 months ago
Hello!, nice tutorial, what changes are necessary in the VB code to control leds with keyboard instead of buttons?
the seaker (author)  cpadilla109 months ago
I don't entirely understand what you mean? Could you elaborate please and maybe I could help you out
jborgne2 years ago
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.
the seaker (author)  jborgne2 years ago
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!
Lyron3 years ago
How do you show the serial feedback in visual studio?
Use the Serial Monitor in Arduino.

I only wish it did hex.
the seaker (author) 3 years ago
im sorry, but that means..?
ggghhhggg3 years ago
I think that this program is break
lele_163 years ago
yay, go Seaker!!! sorry, just here to show some support for my bestie..hope that's okay...from the very little that i know, it looks good!!! :)
the seaker (author)  lele_163 years ago
haha its good to "see" a frendly face showing some support:) thanks bestie! i never new you had an account here!! instructables is my favorite website ever!
i just made it!! i finally remembered to click the link you sent me, and i wanted to show some support so i made an account!!! keep up the good work bestie!!!!
doby1623 years ago
If you could modify this to work with java that would be so awesome. In fact, let me know if you ever do something like that. Their is a lot of potential in somthing like this, now you just need to think of something good to do with it. good ible!
the seaker (author)  doby1623 years ago
thank you. i will definitely look into using java, i didn't even think of that. Thanks for the idea!!! and this is my first ible so i will be sure to review and post updates!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!