Hello I am new to controlling hardware with a USB connection. I have an Arduino UNO Microcontroller and was searching for resources to get me started. I program in C# (Visual Studio 2010) and was wondering if there were some basics I could use to setting up/testing my connection. I'm looking for something as simple as a check box in my WinForm toggling the Digital I/O pin on the Arduino between high and low. Haven't been able to find much to start with.
Thanks in advance.
Arduino uses its own language to program Arduino boards,Because its programming language is easy to understand. But it can also be programmed in C language.
C and C++ are among the most powerful languages you can find. Those are quite fast and stable, and thus are a good choice for microcontrollers. The compiler used to transform code into object files is avr-g++. Then, a program called avrdude is used to upload the program into the Arduino microcontroller.
If you spend some time on Google searching for Arduino Tutorials you can find all kinds of examples. The code in arduino is C++, not C. So if you want to understand how to program arduino the way people does, you need C++.
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online.
There are many ways to send a command from the pc to an arduino. Sandeep Bansil provides a good example of connecting and reading a serial port.
Below is a working example of how to write to a serial port based on the state of a checkbox on a windows form amd how to process the request from the pc on the arduino.
This is a verbose example, there are cleaner solutions but this is clearer.
In the example the arduino waits for either an 'a' or a 'b' from the pc. the pc sends an 'a' when a checkbox is checked and sends a 'b' when a checkbox is unchecked. The example assumes digital pin 4 on the arduino.
Arduino code
#define DIGI_PIN_SOMETHING 4
unit8_t commandIn;
void setup()
{
//create a serial connection at 57500 baud
Serial.begin(57600);
}
void loop()
{
//if we have some incomming serial data then..
if (Serial.available() > 0)
{
//read 1 byte from the data sent by the pc
commandIn = serial.read();
//test if the pc sent an 'a' or 'b'
switch (commandIn)
{
case 'a':
{
//we got an 'a' from the pc so turn on the digital pin
digitalWrite(DIGI_PIN_SOMETHING,HIGH);
break;
}
case 'b':
{
//we got an 'b' from the pc so turn off the digital pin
digitalWrite(DIGI_PIN_SOMETHING,LOW);
break;
}
}
}
}
Windows C#
This code will reside in your form .cs file. The example assumes that you have attached form events for OnOpenForm, OnCloseForm and the OnClick event to the checkbox. From each of the events you can call the respective methods below....
using System;
using System.IO.Ports;
class fooForm and normal stuff
{
SerialPort port;
private myFormClose()
{
if (port != null)
port.close();
}
private myFormOpen()
{
port = new SerialPort("COM4", 57600);
try
{
//un-comment this line to cause the arduino to re-boot when the serial connects
//port.DtrEnabled = true;
port.Open();
}
catch (Exception ex)
{
//alert the user that we could not connect to the serial port
}
}
private void myCheckboxClicked()
{
if (myCheckbox.checked)
{
port.Write("a");
}
else
{
port.Write("b");
}
}
}
Tip:
If you want to read a message from the arduino then add a timer to your form with an interval of 50
or 100
milliseconds.
In the OnTick
event of the Timer you should check for data using the following code:
//this test is used to see if the arduino has sent any data
if ( port.BytesToRead > 0 )
//On the arduino you can send data like this
Serial.println("Hellow World")
//Then in C# you can use
String myVar = port.ReadLine();
The result of readLine()
will be that myVar
contains Hello World
.
I'm sure you know that Arduino has a few samples that you can use with C#
Here's their C# page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With