Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Using VS to communicate with MicroController(Arduino)

Hey guys, I decided to make this tutorial because I often see people on Youtube asking how exactly this is done. Alot of people enjoy using the Arduino but one problem is some people want to be able to control the Arduino rather then have it run through endless loops.

This is VERY easy and all it is, is pretty much communicating through serial ports. This can be used for things such as creating a program control panel to turn lights on/off, change a motor speed or simply control a robot you made.

Before we get into visual studio we must set up the Arduino. So First in the Arduino program, you are going to want to create a Int value for each byte you will be receiving.

So at the top before setup create your integer
Code
int incomingbyte;

Next you are going to want to start your Serial in the setup void, So create the void and then code in the following.

Code
void setup()
{
  pinMode(speakerPin, OUTPUT); // This can be ignored
  Serial.begin(9600); // This is what you want
}

Once that is done you are almost finished allowing your Arduino to receive data. Next in you're void loop you are going to want this.

Code
void loop()
{

  if(Serial.available() > 0) // This checks if it is receiving any bytes
  {
  incomingbyte = Serial.read(); // This is creating our integer to obtain the bytes value.

  if(incomingbyte == '1') // In VB I sent the [u]STRING[/u] 1, This is NOT a integer value, hence the reason of the semi quote.
  {
  playkey(note1); // This can be ignored because it is playing a void in my source that plays an audio frequency. but here is where you will put what will happen if the 'value' is true.
  }
  }
}

Now that that is all setup, we are going to move onto Visual Studio... I will be using C# in this example, but i assume it wont be hard to figure out if you are using a different language.

First we are going to want to create our Windows Form program and name it what ever you want, I will name mine "ArduinoApp".

We are going to want to double click on the Form and add some things to get it set up to our Serial port.

We are then going to add the some references at the top so we can access our Serial port.

Code
using System.IO.Ports;
*If you are using normal .Net language then you will most likely need to do 'Import' rather then 'using'.

Once that is done we are now going to want to declare our Serial port as 'com3'(Or what ever name you want it to have) in our class.

Code
public partial class Form1 : Form
    {
  [u]SerialPort com3 = new SerialPort();[/u]

Now in our Form load we want to connect and open our Port for Communiction.

Code
private void Form1_Load(object sender, EventArgs e)
  {
    /* This is all Optional for people who don't want to get confused.
    com3.BaudRate = 9600;
    com3.DataBits = 8;
    com3.Parity = Parity.None;
    com3.StopBits = StopBits.One;
    com3.Handshake = Handshake.None;
  */

    com3.PortName = "COM3"; // This is my port, you may have a different one. You can check this by going to your Arduino program clicking 'Tools' on the tool bar and then going to 'Serial Port'.
    com3.Encoding = System.Text.Encoding.Default; //This is important for clear communication.
    com3.Open(); // This opens the port for the program.
    com3.RtsEnable = true; // this allows RTS signal to be sent.

  }

Now that we are connected to our Port we are going to want to send it data correct? Well here is our last step!

Now you can create a button or anything really, and write the signal to your port. I chose to use a button.

Code
private void button1_Click(object sender, EventArgs e)
  {
    com3.Write("1");

  }

So now, when i open my program I can send the byte "1" to my Arduino and we have our Arduino coded to act upon receiving a byte valued as the string "1".

I will give you my Source codes I am using for anyone interested in seeing it in more detail. this source controls Audio frequency's to play tunes, as well as a "Funky Town" beat with VB controls.

Download: http://www.mediafire.com/?sjr9z6fpgxzv8zv

I know i already posted this earlier, but I figured it would be better off here then in gadgets section.

Μouse Wrote:  That's wrong. I don't have proof for that, it should just be common knowledge that it's wrong.

LOL ! My brother just got one of these "chips" for christmas and is testing different things with it :D
Nice tutorial ;)
(Dec 26, 2011 06:28 PM)pr0t0typ3 Wrote:  LOL ! My brother just got one of these "chips" for christmas and is testing different things with it :D
Nice tutorial ;)

Yeah, Micro-controllers are very fun to mess around with... You can also do ALOT with them.. I have friends who have made Paint Ball turrets with cameras that detect movement and shoot at them, all using the Arduino platform.

But I hope he might find use of this Tut :D

Μouse Wrote:  That's wrong. I don't have proof for that, it should just be common knowledge that it's wrong.

Are you sure that is Visual Basic?

Looks like C# to me :D

[Image: tkg318I.png]
Yea, that really does look like C#.
(Dec 27, 2011 09:09 PM)Exemplification Wrote:  Are you sure that is Visual Basic?

Looks like C# to me :D

My apologies, I'm used to referring to any language in visual studio as VB.

I meant, this is C# in Visual Studio :P

Μouse Wrote:  That's wrong. I don't have proof for that, it should just be common knowledge that it's wrong.



Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Brotherhood] by itcorpJul 30, 2026 01:52 AM 4 181 Jul 30, 2026 01:52 AM
Last Post: HerrWinston
[.NET] by SIM-2362662Apr 12, 2020 09:06 PM 9 200 Apr 12, 2020 09:06 PM
Last Post: 0xDEAD10CC
[Java] by Nemo2478Oct 28, 2019 01:42 AM 0 176 Oct 28, 2019 01:42 AM
Last Post: Nemo2478