Windows Form Application Using Arduino Uno

Introduction

 
In my previous article, I explained about Bluetooth Connection using Arduino and in this article, I'll show you working with Arduino Uno using Windows Form Application.
 
Requirements
  • Arduino Uno
  • Led
  • Arduino IDE
  • Visual Studio IDE 
Connection
 
Anode Pin(+) to 3
Cathode Pin(-) to Gnd 
 
 
Programming
 
Arduino: You can refer my first article for explanation.
  1. int led = 3;      
  2. void setup()      
  3. {      
  4.     Serial.begin(9600); //Baud Rate      
  5.     pinMode(led, OUTPUT);      
  6. }      
  7. void loop()      
  8. {      
  9.     char data = Serial.read();      
  10.     switch (data) //Selection Control Statement      
  11.     {      
  12.         case 'ON':      
  13.             digitalWrite(led, HIGH); // Sets the led ON      
  14.             break;      
  15.         case 'OFF':      
  16.             digitalWrite(led, LOW); //Sets the led OFF      
  17.             break;      
  18.     }      
  19. }     
Windows Form:
 
Step 1: Once Visual Studio Community 2015 and select FILE, then New, Project… from the Menu.
 
 
Step 2: From the New Project window select Visual C# from Installed, Templates, then select Windows Form Application.
 
 
Step 3: Drag and drop the buttons in the designer window named ONLED and OFFLED.
 
 
Step 4: And drag and drop the SerialPort Tool in the designer window and it will hide one.
 
 
Step 5: Start coding.
  1. using System;    
  2. using System.Windows.Forms;    
  3. using System.IO.Ports;    
  4.     
  5.     
  6. namespace ArduinoConnection    
  7. {    
  8.     public partial class Form1 : Form    
  9.     {    
  10.         private SerialPort newport;    
  11.         public Form1()    
  12.         {    
  13.             InitializeComponent();    
  14.             Code();    
  15.         }    
  16.     
  17.         private void Code()    
  18.         {    
  19.             newport = new SerialPort();    
  20.             newport.BaudRate = 9600;    
  21.             newport.PortName = "COM4";    
  22.             newport.Open();    
  23.     
  24.             button1.Enabled = true;    
  25.             button2.Enabled = false;    
  26.     
  27.     
  28.         }    
  29.     
  30.         private void button1_Click(object sender, EventArgs e)  //Click Event For LEDON  
  31.         {    
  32.             newport.WriteLine("ON");  // LED ON  
  33.     
  34.             button1.Enabled = false;    
  35.             button2.Enabled = true;    
  36.     
  37.         }    
  38.     
  39.         private void button2_Click(object sender, EventArgs e)  //Click Event For LEDOFF  
  40.         {    
  41.             newport.WriteLine("OFF");  // LED OFF  
  42.     
  43.             button1.Enabled = true;    
  44.             button2.Enabled = false;    
  45.     
  46.         }    
  47.     }    
  48. }    
Explanation
  1. using System.IO.Ports is a Namespace
  2. Create the object named as newport 
  3. newport = new SerialPort(); //Intialize the new instance of Serial Port Class
  4. newport.BaudRate = 9600; //Set the Serial Baud Rate for transforming the data
  5. newport.PortName = "COM4"; //Set the COM Port
  6. newport.Open(); //Port Open
  7. button1.Enabled = true; // Conditions that true means Ledon
  8. button2.Enabled = false; // Ledoff

Conclusion

 
We saw working with Arduino using Windows Form Application