sending data from a computer to a microcontroller

Upload: stephanie-campbell

Post on 01-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Sending Data from a computer to a microcontroller

    1/9

    Page | 1

    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

  • 8/9/2019 Sending Data from a computer to a microcontroller

    2/9

  • 8/9/2019 Sending Data from a computer to a microcontroller

    3/9

    Page | 3

    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:

    Microsoft Visual Studio

    -  Arduino IDE Software

    -  Arduino Leonardo Drivers

  • 8/9/2019 Sending Data from a computer to a microcontroller

    4/9

    Page | 4

    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.

  • 8/9/2019 Sending Data from a computer to a microcontroller

    5/9

    Page | 5

    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 receivedcharacter. 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 outputSerial.begin(9600); // Specify serial communications speed}

    void loop(){

    if(Serial.available() >0){

    c =Serial.read(); // reads a character from serial interfaceSerial.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.

  • 8/9/2019 Sending Data from a computer to a microcontroller

    6/9

    Page | 6

    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)  Determine if there is a serial port currently available for communication

    2)  Open the serial port for communications

    3) 

    Send data through the serial port

    4)  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, double-

    click 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.

  • 8/9/2019 Sending Data from a computer to a microcontroller

    7/9

    Page | 7

    Example code to determine if t here is a serial por t current ly available (See reference [2]) :

    private void Form1_Load(object sender, EventArgs e){

    foreach (string port in SerialPort.GetPortNames()) // Adds all port names toports 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 thefirst 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;

    }

  • 8/9/2019 Sending Data from a computer to a microcontroller

    8/9

  • 8/9/2019 Sending Data from a computer to a microcontroller

    9/9

    Page | 9

    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