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:
- void setup()
- {
-
- Serial.begin(9600);
- pinMode(13, OUTPUT);
- }
-
-
-
- void loop()
- {
- if(Serial.available())
- {
- char data=Serial.read();
- Serial.println(data);
- switch(data)
- {
- case 'O':digitalWrite(13, HIGH);
- break;
- case 'F':digitalWrite(13, LOW);
- break;
-
- }
- }
- }
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:
Afterwards, 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 recieved 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, afterwards, go to .cs page and write the following code:
First, use the following namespace.
Now, declare a serial port as follows.
- public partial class Form1 : Form
- {
- public SerialPort myport;
-
- public Form1()
- {
- InitializeComponent();
- }
Now, under "ON" button, write the following:
- private void button1_Click(object sender, EventArgs e)
- {
- myport = new SerialPort();
- myport.BaudRate = 9600;
- myport.PortName = "COM5";
- myport.Open();
- myport.WriteLine("O");
- myport.Close();
- }
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.
- private void button2_Click(object sender, EventArgs e)
- {
- myport = new SerialPort();
- myport.BaudRate = 9600;
- myport.PortName = "COM5";
- myport.Open();
- myport.WriteLine("F");
- myport.Close();
-
- }
The complete code is here and you can check it as follows:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO.Ports;
-
- namespace Ardeno
- {
- public partial class Form1 : Form
- {
- public SerialPort myport;
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- myport = new SerialPort();
- myport.BaudRate = 9600;
- myport.PortName = "COM5";
- myport.Open();
- myport.WriteLine("O");
- myport.Close();
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- myport = new SerialPort();
- myport.BaudRate = 9600;
- myport.PortName = "COM5";
- myport.Open();
- myport.WriteLine("F");
- myport.Close();
-
- }
- }
- }
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 C# Windows Application. I hope this article will help the new developers, who are coming to learn this technology.