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.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.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 } }
nice tute
ReplyDeleteSo good! Made me do it! Thank you!
ReplyDeleteHi,
ReplyDeleteReally it's very easy to understand, Thankn you for sharing this information it's very useful for beginers like me.I con't understand how to glow & off LED in window form image please explain it.
Thank you advance for ur replay.
Thank you. This is fantastic, excellent explanation. I have one problem I can not solve - the LEDPin turns on when I turn the arduino on. Can I make it not turn on until I press the button?
ReplyDeleteThanks again!
Nice explanation, so if I want to transform it using Ethernet shield, so what's can be?
ReplyDeletethere is an updated code , with dynamic port selection refer this page arduino vb port selection
ReplyDeleteNusbioMCU is Micro-Controller for .NET to control
ReplyDeletemulti-color LED strip, ring or matrix (WS2812 LED).
www.NusbioMCU.com
Hi, how to add the LED image and make it ON and OFF
ReplyDeleteyou should select item " picture box" but you must to do( LED ON.JPEG) and (LED OFF.JPEG) before done.
DeleteGreat. I also have same project to communicate Arduino with Visual studio, please come here:
ReplyDeletehttp://engineer2you.blogspot.com/2016/12/arduino-serial-communication-visual.html
you should publish these at computer aided automation
ReplyDeleteThis information is really awesome thanks for sharing most valuable information.
ReplyDelete3D Scanning Services Near Me
3D Scanning Reverse Engineering
Reverse Engineering Services
Plant Engineering Services
Offshore Engineering Services India