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. namespace ArduinoConnection
  5. {
  6. public partial class Form1 : Form
  7. {
  8. private SerialPort newport;
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. Code();
  13. }
  14. private void Code()
  15. {
  16. newport = new SerialPort();
  17. newport.BaudRate = 9600;
  18. newport.PortName = "COM4";
  19. newport.Open();
  20. button1.Enabled = true;
  21. button2.Enabled = false;
  22. }
  23. private void button1_Click(object sender, EventArgs e) //Click Event For LEDON
  24. {
  25. newport.WriteLine("ON"); // LED ON
  26. button1.Enabled = false;
  27. button2.Enabled = true;
  28. }
  29. private void button2_Click(object sender, EventArgs e) //Click Event For LEDOFF
  30. {
  31. newport.WriteLine("OFF"); // LED OFF
  32. button1.Enabled = true;
  33. button2.Enabled = false;
  34. }
  35. }
  36. }
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