Requirements
- Visual Studio.
- Arduino IDE.
- LED Light (1 number).
- Arduino Board.
Step 1- Programming Arduino
- Open Arduino IDE, if already installed or download and install it from this link.
- Now, open the IDE and enter the code, given below in it.
- char incomingOption;
- void setup()
- {
- pinMode(13, OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- incomingOption = Serial.read();
- if (incomingOption=='1')
- {
- digitalWrite(13, HIGH);
- }
- else if (incomingOption=='0')
- {
- digitalWrite(13, LOW);
- }
- }
- Save this sketch in any location and then verify the code.
- Open Visual Studio and create a new Windows Form Application with any name, you want.
- Once you have created a new form Application, open the designer Window of Form1.
- Now, from the tools box, drag and drop the SerialPort tool into Form 1.
- This will help you to communicate your program with the Arduino board.
- Now, add four buttons to Form 1.
- Name the buttons as ON, OFF, Port Open, and Port Close.
- It will appear like the screenshot, given below-

Step 3 - Writing C# code in Visual Studio
You have four buttons. Hence, you need to write a separate code for each button.
You have four buttons. Hence, you need to write a separate code for each button.
- Double click on "ON button". You will get a coding page. Paste the code, given below, there-
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- SerialPort1.Write("1"); //send 1 to Arduino
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- Double click on "OFF button" and paste the code, given below, in it-
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- SerialPort1.Write("0"); //send 0 to Arduino
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- Now, double click Form1.
- You will get a coding page for it.
- Paste the code, given below, in it-
- private void Form1_Load(object sender, EventArgs e)
- {
- SerialPort1.Open(); //open serialPort
- }
- Now, double click on the Port Close button and paste the code, given below, in it.
- private void button3_Click(object sender, EventArgs e)
- {
- SerialPort1.Close(); //close serialPort
- }
- Now, double click on the Port Open button and paste the code, given below, in it-
- private void button4_Click(object sender, EventArgs e)
- {
- SerialPort1.Open();
- }
- Once you finish all the code, the total view will be like the screen shot, shown below-


Gouri ThampiPosted Jan 18, 2018, 3:41 AM
I cant find serialPort in the tootbox.Please help
Munish APosted Oct 18, 2017, 7:41 AM
Nice article...........
Muthuramalingam DuraipandiPosted Oct 4, 2016, 9:27 AM
Good
Hussain PatelPosted Oct 4, 2016, 12:11 AM
Nice one.. I liked it..
Sr KarthigaPosted Oct 3, 2016, 9:55 AM
Good start
Humayun Kabir MamunPosted Oct 3, 2016, 4:30 AM
Nice...
Vijayaragavan SPosted Oct 3, 2016, 2:54 AM
Good
Vignesh ManiPosted Oct 2, 2016, 3:31 PM
Nice one