C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Windows Form Application Using Arduino Uno
WhatsApp
Kumaresh Rajalingam
5y
27k
0
4
100
Article
Arduino.rar
|
VS.rar
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.
int
led = 3;
void
setup()
{
Serial.begin(9600);
//Baud Rate
pinMode(led, OUTPUT);
}
void
loop()
{
char
data = Serial.read();
switch
(data)
//Selection Control Statement
{
case
'ON'
:
digitalWrite(led, HIGH);
// Sets the led ON
break
;
case
'OFF'
:
digitalWrite(led, LOW);
//Sets the led OFF
break
;
}
}
Windows Form:
S
tep 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)
//Click Event For LEDON
{
newport.WriteLine(
"ON"
);
// LED ON
button1.Enabled =
false
;
button2.Enabled =
true
;
}
private
void
button2_Click(object sender, EventArgs e)
//Click Event For LEDOFF
{
newport.WriteLine(
"OFF"
);
// LED 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
Arduino
OT
Visual Studio IDE
Windows Form
Up Next
Ebook Download
View all
Raspberry Pi -Sensorial Symphony of Connectivity
Read by 712 people
Download Now!
Learn
View all
Membership not found