- 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.
- int led = 3;
- void setup()
- {
- Serial.begin(9600);
- pinMode(led, OUTPUT);
- }
- void loop()
- {
- char data = Serial.read();
- switch (data)
- {
- case 'ON':
- digitalWrite(led, HIGH);
- break;
- case 'OFF':
- digitalWrite(led, LOW);
- break;
- }
- }
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.
- using System;
- using System.Windows.Forms;
- using System.IO.Ports;
-
-
- namespace ArduinoConnection
- {
- public partial class Form1 : Form
- {
- private SerialPort newport;
- public Form1()
- {
- InitializeComponent();
- Code();
- }
-
- private void Code()
- {
- newport = new SerialPort();
- newport.BaudRate = 9600;
- newport.PortName = "COM4";
- newport.Open();
-
- button1.Enabled = true;
- button2.Enabled = false;
-
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- newport.WriteLine("ON");
-
- button1.Enabled = false;
- button2.Enabled = true;
-
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- newport.WriteLine("OFF");
-
- button1.Enabled = true;
- button2.Enabled = false;
-
- }
- }
- }
Explanation
- using System.IO.Ports is a Namespace
- Create the object named as newport
- newport = new SerialPort(); //Intialize the new instance of Serial Port Class
- newport.BaudRate = 9600; //Set the Serial Baud Rate for transforming the data
- newport.PortName = "COM4"; //Set the COM Port
- newport.Open(); //Port Open
- button1.Enabled = true; // Conditions that true means Ledon
- button2.Enabled = false; // Ledoff
Conclusion
We saw working with Arduino using Windows Form Application