[Project 21] – Arduino & C# Communication Turn ON-OFF LED

In this project we will provide C# communication with the Arduino. We build C# interface with using 4 different object from the visual studio. You must set the port according to your Arduino. I used Arduino Mega 2560.

Circuit :


Lesson 1-Blinking LED

 

Materials:


Arduino Uno/Mega  x1
Breadboard  x1
Red LED  x1
220 Ohm  x2
Jumper Cable

 

Arduino Program :


 
	
//  letsarduino.com
//  [Project 21] - Arduino & C# Communication Turn ON-OFF LED

int led = 13;  
void setup() {              
    pinMode(led, OUTPUT); 
  Serial.begin(9600);  
}
void loop() {
  if(Serial.available())   
  {int x=Serial.read();   
  if(x =='1')
 { digitalWrite(led, HIGH);} 
  
  else if (x == '0')         
  {digitalWrite(led, LOW);}
  }

}

 

C# Program:


 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace ArduinoSerialCommunication

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = comboBox1.Text;            
            serialPort1.Open();
            
        }       

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = SerialPort.GetPortNames();
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Write("1");
            label1.Text = "LED ON";      
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0");
            label1.Text = "LED OFF";
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }           

     }
}
	

 

C# Interface Design :


c#-arduino-communication


c#-arduino-comarduino-C#-comm



 

You may also like...