14 Haziran 2015 Pazar

Turn ON/OFF LED in Arduino Using Visual Studio

1. Problem Statement

In this tutorial we will learn how to Turn ON and Turn OFF a LED in arduino, but this time using Visual Studio using the serial Port and C# programming language.

2. What is a serial interface?

A serial interface is used for information exchange between computers and peripheral devices. When using a serial communication, the information is sent bit by bit (serial) over a cable. Modern serial interfaces are Ethernet, Firewire, USB, RS-485, etc. For our project we will use the USB port.
Visual Studio provide a serialPort control which we will use for this project.

3. Sketch for this project

We are going to use the sketch used in our first tutorial, which just need a LED, 100ohm resistors and wires to connect them with Arduino board, the next picture show the connections.
Turn ON OFF Led arduino Using C#

4. Source Code for Arduino

The source code for this project is simple to understand, but in this case we will get the information from the serial port dispatched by C# program and then we will verify to turn ON(1) or turn OFF(0), Type the following code in the text area of arduino the code is explained line by line.
 
#define BaudRate 9600
#define LEDPin    10
char incomingOption;

void setup()
{
  pinMode(LEDPin, OUTPUT);
  // serial communication
  Serial.begin(BaudRate);
}
void loop()
{
     //read from serial port getting information from VS 2013
     incomingOption = Serial.read();
     //verify incomingOption
     switch(incomingOption){
        case '1':
          // Turn ON LED
          digitalWrite(LEDPin, HIGH);
          break;
        case '0':
          // Turn OFF LED
          digitalWrite(LEDPin, LOW);
          break;
     }
}

5. Design the Interface in Visual Studio for communicate with Arduino

Design a interface with 3 buttons, the first one will turn ON the LED, the second one will turn OFF the LED and finally the third button will close the serial port.

Turn ON OFF LED arduino with C#

You need to drag and drop a serialPort control to the design area and set properties BaudRate to 9600 and PortName to COM4 and that's all for the design part.
Next we need to write the code for turning ON and Turning OFF.
 
public partial class frmTurnONTurnOFFLED : Form
{
   public frmTurnONTurnOFFLED()
   {
      InitializeComponent();
   }
   private void btnTurnON_Click(object sender, EventArgs e)
   {
      try
      {
         serialPort1.Write("1"); //send 1 to Arduino
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }
   private void btnTurnOFF_Click(object sender, EventArgs e)
   {
      try
      {
         serialPort1.Write("0"); //send 0 to Arduino
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }
   private void frmTurnONTurnOFFLED_Load(object sender, EventArgs e)
   {
      serialPort1.Open(); //open serialPort
   }
   private void btnClosePort_Click(object sender, EventArgs e)
   {
      serialPort1.Close(); //close serialPort
   }        
}
 
 

Hiç yorum yok:

Yorum Gönder