Controlling Arduino With Windows Form Application

Introduction

 
As per Wikipedia "Arduino is a hardware and software company, project, and user community that designs and manufactures computer open-source hardware, open-source software, and micro-controller-based kits for building digital devices and interactive objects that can sense and control physical devices".
 
For programming the micro-controllers in Arduino Kit, the Arduino project provides an integrated development environment (IDE), based on a programming language, named Processing, which also supports the languages C and C++.
 
You can download the IDE from the given Link.
 
The Arduino kit looks as shown below:
 
 
This Board consists of the following elements:
 
 
Now, from here, I will write the code in the Arduino IDE as follows:
  1. void setup()    
  2. {    
  3.       
  4.   Serial.begin(9600);    
  5.   pinMode(13, OUTPUT);    
  6. }    
  7.     
  8. void loop()    
  9. {    
  10.   if(Serial.available())    
  11.   {    
  12.  char  data=Serial.read();    
  13.   Serial.println(data);    
  14.   switch(data)    
  15.   {    
  16.     case 'O':digitalWrite(13, HIGH);    
  17.     break;    
  18.     case 'F':digitalWrite(13, LOW);    
  19.     break;    
  20.        
  21.   }    
  22.   }    
  23. }   
 
Now, connect Arduino with the computer, fix the LED to the PIN NO-13(Positive) and to Gnd(Negative). The Negative and Positive of the LED  is shown below:
 
 
It should look as shown below:
 
 
Now, change the port to "COM5" as follows:
 
 
Afterward, upload the code, using the upload button on the IDE.
 
 
Here, the code written is very simple.
 
First of all, it will require the input from Serial Monitor or from any other thing. It will read the data and print the data. If the data is "O" then  LED will be switched on and if the data received is "F", it will switch off the LED. 
 
This will complete the work from the Arduino board and IDE. To control this from Windows Form Application Open Visual Studio, create a new Windows Form Project and design the form with the two buttons and Serial port as follows:
 
Now, afterward, go to .cs page and write the following code:
 
First, use the following namespace.
  1. using System.IO.Ports;   
Now, declare a serial port as follows.
  1. public partial class Form1: Form    
  2.     {    
  3.         public SerialPort myport;    
  4.              
  5.         public Form1()    
  6.         {    
  7.             InitializeComponent();    
  8.         }   
Now, under "ON" button, write the following:
  1. private void button1_Click(object sender, EventArgs e)    
  2.        {    
  3.            myport = new SerialPort();    
  4.            myport.BaudRate = 9600;    
  5.            myport.PortName = "COM5";    
  6.            myport.Open();    
  7.            myport.WriteLine("O");    
  8.            myport.Close();    
  9.        }   
Here, as expected, I am passing "O" for ON the LED in the Serial Port.
 
Now Just send "F" for OFF, the LED will become OFF at the click of a button.
  1. private void button2_Click(object sender, EventArgs e)    
  2. {    
  3.     myport = new SerialPort();    
  4.     myport.BaudRate = 9600;    
  5.     myport.PortName = "COM5";    
  6.     myport.Open();    
  7.     myport.WriteLine("F");    
  8.     myport.Close();    
  9.     
  10. }  
The complete code is here and you can check it as follows:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Drawing;    
  6. using System.Linq;    
  7. using System.Text;    
  8. using System.Threading.Tasks;    
  9. using System.Windows.Forms;    
  10. using System.IO.Ports;    
  11.     
  12. namespace Ardeno    
  13. {    
  14.     public partial class Form1 : Form    
  15.     {    
  16.         public SerialPort myport;    
  17.              
  18.         public Form1()    
  19.         {    
  20.             InitializeComponent();    
  21.         }    
  22.     
  23.         private void Form1_Load(object sender, EventArgs e)    
  24.         {    
  25.     
  26.         }    
  27.     
  28.         private void button1_Click(object sender, EventArgs e)    
  29.         {    
  30.             myport = new SerialPort();    
  31.             myport.BaudRate = 9600;    
  32.             myport.PortName = "COM5";    
  33.             myport.Open();    
  34.             myport.WriteLine("O");    
  35.             myport.Close();    
  36.         }    
  37.     
  38.         private void button2_Click(object sender, EventArgs e)    
  39.         {    
  40.             myport = new SerialPort();    
  41.             myport.BaudRate = 9600;    
  42.             myport.PortName = "COM5";    
  43.             myport.Open();    
  44.             myport.WriteLine("F");    
  45.             myport.Close();    
  46.     
  47.         }    
  48.     }    
  49. }   
After that, just run the project.
 
 
Now, click the ON Button and check the result.
 
 
Click the OFF button to OFF the LED.
 
 
Hence, in this way, we can control Arduino, using the C# Windows Application. I hope this article will help the new developers, who are coming to learn this technology.