HowTo: Write your own interface application for Arduino

The Arduino is a great platform for a lot of projects in electronics since you do not have to start from scratch – not in hardware nor in software, since it comes with a nice framework including a bunch of useful libraries. This makes it possible to control an Arduino board over USB with a few lines of code.

In this article I want to summarize how to do this and how to write your own interface application.

Firmware

The USB communication both on the computer and the Arduino microcontroller is done over a virtual serial port. So what you have to do in firmware (i.e. your “sketch”) is to set up the serial interface and implement your own protocol. This could look like this:

void setup() {
  [...]
  Serial.begin(9600);
}

void loop() {
  [...]
  usb_comm();
}

void usb_comm() {
  if (Serial && Serial.available()) {
    switch (Serial.read()) {
      case ';': // Command end
        break;

      case 'a': // Command group 'a'
        switch (Serial.read()) {
          case 'a': // Command 'aa'
            cmd_a(Serial.parseInt());
            break;
          case 'b': // Command 'ab'
            cmd_b();
            break;
        }
        break;

      case 'b': // Command group 'b'
        switch (Serial.read()) {
          [...]
        }
        break;

      [...]
    }
  }
}

The “protocol” in this example is very simple. Commands consist of two chars: the first is determining the command group and the second specifies the command itself. Every command is terminated with a semicolon.

When you want to say “stepper left: speed 100; stepper right: speed 80;” for example, you open the Serial Monitor in the Arduino IDE and type the following commands:

sl100;sr80;

To send back information from your Arduino to the interface you can write:

Serial.Print("ab");
Serial.Print(value);
Serial.Print(";")

 

Software

Controlling your Arduino by typing commands is not very comfortable. So sooner or later you want to create your own customized interface application. And there are dozens of possibilities how to do this.

One way is to do it in C#. All you need is Visual C# 2010 Express which can be downloaded here for free. After opening a new project (I usually choose Windows Forms), you open the Toolbox and place three controls on the main form:

  • SerialPort, name it comPort
  • ComboBox, name it cbxPort
  • Button, name it btnConnect

The SerialPort should be configured to the same baud rate as selected in firmware, and for some Arduinos (i.e. Arduino Leonardo) it is necessary to set DtrEnable and RtsEnable to True. Other properties are usually okay (no parity, no handshake, 8 data bits, 1 stop bit).

A good practice is to use a ComboBox to select the correct COM port. Double-click on the main form to jump into the code of the Load event and type the following. This fills the ComboBox with all available COM ports and selects the first one.

cbxPort.Items.Clear();
string[] ports = System.IO.Ports.SerialPort.GetPortNames();

foreach (string port in ports)
  cbxPort.Items.Add(port);

if (cbxPort.Items.Count > 0)
  cbxPort.SelectedIndex = 0;

Go back to the form designer, double-click on the Connect button and type the following:

if (!comPort.IsOpen)
{
  comPort.PortName = cbxPort.Text;
  comPort.Open();
  btnConnect.Text = "Disonnect";
}
else
{
  comPort.Close();
  btnConnect.Text = "Connect";
}

Now you can start creating your custom interface by placing more controls and typing some code.

Examples for sending a command are:

comPort.Write("ab;");
comPort.Write(String.Format("sl{0:D};", NumericUpDown1.Value);

Handling data received from the Arduino is done in the DataReceived event of the SerialPort control.

string cmd;
while (comPort.BytesToRead > 2)
{
  cmd = comPort.ReadTo(";");
  [...]
}

 

Troubleshooting

USB communication with my Arduino does work with the Arduino Serial Monitor but not with my interface program.

  • Check the serial port configuration in your interface software (see above). Especially RtsEnable and DtrEnable have to be active.

USB communication with my Arduino is very slow or delayed.

  • Check if you are using Serial.parseInt() in firmware. Always terminate numbers with a non-numeric char (i.e. “;”) as this command will wait about a second when no character is following the number.
  • Check your baud rate. 9600 baud are only 960 bytes per second (8 data bits, 1 start bit, 1 stop bit). Use a higher baud rate if necessary.
  • There always are some milliseconds latency. More information available here.

 

Conclusion

Controlling an Arduino over USB is simple and easy to implement. Of course the examples above don’t allow complex communication and should be considered as a starting point.

I would be happy to receive your feedback and ideas!

Advertisements

3 thoughts on “HowTo: Write your own interface application for Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s