You are on page 1of 9

Sending Data from a computer to a microcontroller

using a UART (Universal Asynchronous


Receiver/Transmitter)

Eric Bell
04/05/2013

Abstract:
Serial communication is the main method used for communication between external
microcontrollers and computers, and can be a challenge to set up correctly. This application
note will provide a walk-through example on how to set up a simple UART communication link
using Microsoft Visual Studio and the Arduino Leonardo microcontroller.

Keywords:
Arduino, Leonardo, Visual Studio, Microcontroller, UART

Page | 1

Table of Contents
Introduction...................................................................................................................................3
Arduino Software Installation........................................................................................................4
Hardware Setup.............................................................................................................................4
Setting up the Receiver.5
Setting up the Sender.6
Recommendations.9
References..9

Page | 2

Introduction:
This application note will provide instructions on how to obtain simple communications
between a computer and a microcontroller using UART (Universal Asynchronous
Receiver/Transmitter). UART is a commonly used piece of hardware which translates data
between parallel and serial communication mediums (See reference [6]). In this tutorial, you
will be taught how to set up a simple interface between a computer and a microcontroller by
utilizing Microsoft Visual Studio, and the Arduino Leonardo microcontroller.
The implementation described in this application note will provide a walk-through to set
up the Arduino Leonardo to receive communication from the computer and then respond
appropriately. In this case, the microcontroller will receive characters from the computer
consisting of either + or -. These characters will activate an LED by turning it on or off, by
interpreting a + as an on signal, and a - as an off signal.

Implementation:
Hardware:
-

Personal Computer running Microsoft Windows


Arduino Leonardo Microcontroller
Micro-USB Interconnect Cable
1 LED (any color)
1 220-Ohm resistor

Software:
-

Page | 3

Microsoft Visual Studio


Arduino IDE Software
Arduino Leonardo Drivers

Arduino Software Installation


Download and install the Arduino IDE Software and Arduino Leonardo Drivers by following the
set-up instructions provided with the software. Arduino IDE Software and Leonardo Drivers can
be obtained free of charge by download by visiting http://arduino.cc/en/Main/Software.

Hardware Setup
For this communications set-up, we will be physically connecting the Arduino Leonardo
microcontroller to the computer using a micro-USB interconnect cable, and setting up the
microcontroller to provide feedback through the lighting of an LED. The microcontroller can be
obtained by ordering, and is available at many different web sites on the internet. One major
distributor of Arduino products is Mouser, and this microcontroller can be ordered from them
by visiting www.mouser.com/Arduino-Leonardo.

Follow the steps outlined below for proper set-up:


1) Connect the 220-ohm resistor and LED to the Arduino Leonardo microcontroller by
following the diagram below. (See Reference [1])

2) Plug the Micro-USB cable in to the Arduino Leonardo in to the attached port, and in to
any available USB port on the computer.
Page | 4

Setting up the Receiver


The Arduino Leonardo microcontroller needs to be set up to receive a character from the USB
port, and also needs to be programmed to respond appropriately based upon the received
character. This will be accomplished by using the Arduino IDE Software and the following code
(See reference [1]):

int led = 13;


char c;
void setup()
{
pinMode(led, OUTPUT); // specify led port as output
Serial.begin(9600); // Specify serial communications speed
}
void loop()
{
if(Serial.available() > 0)
{
c = Serial.read(); // reads a character from serial interface
Serial.print(c);
if(c == '+')
{
digitalWrite(led, HIGH); // turn on LED
}
else if(c == '-')
{
digitalWrite(led, LOW); // turn off LED
}
}
}

The code above is written in C#, and will specify port 13 as the port for our LED, and will
determine whether the Arduino Leonardo will turn on or turn off the LED. Once this code has
been typed in to the Arduino IDE software, it can be uploaded by clicking on File-> Save, and
then clicking on the Upload button. The button is depicted as a small arrow in the upper left
corner of the program screen.

Page | 5

Setting up the Sender


For this portion of the set-up, we will be using Microsoft Visual Studio to create a program
which will send the + or - character to the Arduino Leonardo microcontroller through the USB
cable. This portion of the application note will provide a demonstration on how to write code
that will:
1)
2)
3)
4)

Determine if there is a serial port currently available for communication


Open the serial port for communications
Send data through the serial port
Close the serial port

Microsoft Visual Studio should be used to create a new project, in which you should create one
combo-box, and four buttons. This can be accomplished within the program by clicking on The
toolbox icon on the left side of the screen, and then double-clicking on the Button link. This will
allow you to place the button on your blank form. Once all buttons have been placed, doubleclick on the ComboBox link in order to place the drop-down menu as shown in the figure.
Complete instructions are included in the Microsoft documentation on their web site
(Reference [4]). When complete, your program GUI (Graphics User Interface) should look
similar to this:

The code on the following pages will then be used to control the functions of the combo-box
and buttons.

Page | 6

Example code to determine if there is a serial port currently available (See reference [2]):

private void Form1_Load(object sender, EventArgs e)


{
foreach (string port in SerialPort.GetPortNames()) // Adds all port names to
ports list
{
cbPorts.Items.Add(port); // adds ports to Ports combo-box
}
if (cbPorts.Items.Count > 0) // if there is at least one port available, select the
first one as default
{
cbPorts.SelectedIndex = 0;
}
else // no ports available
{
cbPorts.Enabled = false;
MessageBox.Show("Could not locate any available serial ports.", "No serial
ports", MessageBoxButtons
}
btnSendA.Enabled = false;
btnSendB.Enabled = false;
btnClose.Enabled = false;
}

Page | 7

Example code to open the serial port for communication (See reference [3]):

private void btnOpen_Click(object sender, EventArgs e)


{
_serialPort = new SerialPort(cbPorts.Text); // Store serial ports in combo-box
_serialPort.BaudRate = 9600; // Set Port speed
_serialPort.DataBits = 8; // Use 8 bits for data
_serialPort.Parity = Parity.None; // No Parity
_serialPort.StopBits = StopBits.One;
_serialPort.Open();
// Open the port
_serialPort.WriteTimeout = 5000; // set time-out time for write
// Change the Enabled flag for the buttons to prevent user error
cbPorts.Enabled = false;
btnOpen.Enabled = false;
btnSendPlus.Enabled = true;
btnSendMinus.Enabled = true;
btnClose.Enabled = true;
}

Example code to write a + to the open serial port (See reference [3]):
private void button1_Click(object sender, EventArgs e)
{
_serialPort.WriteLine("+"); // Writes a '+' to the open Serial Port
}

Example code to close the serial port (See reference [3]):


private void btnClose_Click(object sender, EventArgs e)
{
_serialPort.Close();
// Change the Enabled flag for the buttons to prevent user error
cbPorts.Enabled = true;
btnOpen.Enabled = true;
btnSendPlus.Enabled = false;
btnSendMinus.Enabled = false;
btnClose.Enabled = false;
}

Page | 8

In this application note, we have covered the set up of the hardware, the design and
programming of the user interface, as well as the programming of the Arduino Leonardo
firmware. By following these instructions, it should be a very straight-forward task to develop
communication between the computer and the Arduino Leonardo microcontroller. Once these
techniques have been practiced, they can be applied to more complex situations, as well as to
other microcontrollers.

Recommendations
-

The Arduino Leonardo can also be used to send date to the computer using the
serial interface. There are many examples and tutorials available for free by utilizing
the Arduino web site. (See reference [1])
The serial ports on the Arduino Leonardo can be used to communicate to more
complex devices, such as other microcontrollers
More complex Arduino Microcontrollers are capable of communication through a
much wider array of communication mediums, such as WiFi, rather than just serial
communication

References
[1] http://arduino.cc/en/Tutorial/HomePage
[2] http://msdn.microsoft.com/en-us/library/tf8zk72w.aspx
[3] http://msdn.microsoft.com/en-us/library/y2sxhat8.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
[4] http://msdn.microsoft.com/en-us/vstudio/aa718325.aspx
[5] http://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter

Page | 9

You might also like