Requirements
- Visual Studio.
- Arduino IDE.
- LED Light (1 number).
- Aurduino Board.
Step 1- Programming Arduino
- Open Arduino IDE, if already installed or download and install it from this link.
- Now, open the IDE and enter the code, given below in it.
- char incomingOption;
- void setup()
- {
- pinMode(13, OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- incomingOption = Serial.read();
- if (incomingOption=='1')
- {
- digitalWrite(13, HIGH);
- }
- else if (incomingOption=='0')
- {
- digitalWrite(13, LOW);
- }
- }
- Save this sketch in any location and then verify the code.
Step 2- Designing in Visual Studio
- Open Visual Studio and create new Windows Form Application with any name, you want.
- Once you have created a new form Application, open the designer Window of Form1.
- Now, from the tools box, drag and drop the SerialPort tool into Form 1.
- This will help you to communicate your program with the Arduino board.
- Now, add four buttons to Form 1.
- Name the buttons as ON, OFF, Port Open and Port Close.
- It will appear as the screenshot, given below-
Step 3 - Writing C# code in Visual Studio
You have four buttons. Hence, you need to write a separate code for each button.
- Double click on "ON button". You will get a coding page. Paste the code, given below, there-
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- SerialPort1.Write("1");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- Double click on "OFF button" and paste the code, given below, in it-
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- SerialPort1.Write("0");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- Now, double click Form1.
- You will get a coding page for it.
- Paste the code, given below, in it-
- private void Form1_Load(object sender, EventArgs e)
- {
- SerialPort1.Open();
- }
- Now, double click on the Port Close button and paste the code, given below, in it.
- private void button3_Click(object sender, EventArgs e)
- {
- SerialPort1.Close();
- }
- Now, double click on the Port Open button and paste the code, given below, in it-
- private void button4_Click(object sender, EventArgs e)
- {
- SerialPort1.Open();
- }
- Once you finish all the code, the total view will be like the screen shot, shown below-
- The complete set of code is given below. It will show some errors, if you copy and paste the code without double clicking the buttons and the Form. It is good to follow the steps correctly.
- using System;
- using System.Windows.Forms;
- namespace LED
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- SerialPort1.Write("1");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- SerialPort1.Write("0");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- SerialPort1.Open();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- SerialPort1.Close();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- SerialPort1.Open();
- }
- }
- }
Step 4 - Connecting the LED to the Arduino and uploading program
- Take the positive side of the LED, connect it to the pin 13 and negative side to the GND.
- Now, simply verify whether the COM ports are same in the Arduino IDE and the properties of the Form in Visual Studio.
- After you verify it, upload the sketch into Arduino board. The code, which you wrote in IDE in the beginning is called as sketch.
- Now, you are completely ready with the whole setup.
Final step - Working of the project
- Deploy the whole setup.
- Now, click on "ON button". The LED will glow.
- Now, click on the "OFF button". The LED will turn off.
- Now; click on the Port Close button and try to turn on the LED. It will show a message, which port is closed.
- Now, click on the Port Open button and try to turn on the LED. It will now glow.
- Thus you have made your own LED lighting program using the C# language in visual studio.
Note: For more projects, based on the IOT, go through the links, given below-