You are on page 1of 5

Colorado School of Mines

Department of Mechanical Engineering


MEGN 540: Mechatronics

Laboratory 1: Atmel Studio and Serial Communication


Objective: In this lab you will learn the basics of microcontroller programming and serial
communication using the Atmel Studio software development environment.
Equipment:
AVRISP mkII programmer
Arduino Uno (on your Haptic Paddle)
Atmel Studio 7 Software

USB Cable
Computer (with Windows OS)
Matlab

Background:
Arduino development boards have gained increasing popularity in recent years due to their low
cost, and ease of use. A typical Arduino board comes with a particular 8-bit Atmel
microcontroller, I/O ports, USB connectivity, and a DC power input. Arduino has also released
an associated development environment (with programs called sketches), which comes
bundled with pre-written functions for common microcontroller programming routines. However,
we will be bypassing the Arduino development environment and we will be programming the
microcontroller directly using Atmel Studio, an embedded C programming environment.
The Arduino Uno Board
One of the most popular Arduino boards, the Arduino Uno (Fig. 1) packs an Atmel AVR
ATmega328p microcontroller, digital input and output pins, analog input pins, a 16MHz master
clock crystal, a stand-alone power supply (if needed) and a plug and play (drivers installed) USB
port for communication.

Figure 1. The Arduino Uno


The USB port is used to upload software written in the Arduino development environment, but
also provides power to the board when plugged in to a computer. In order to communicate with
the microcontroller directly, we will be using the AVRISP mkII programmer, connecting to the six
pins that we extended from our Arduino Uno board through the Ardumoto board. The USB port
can still be used to send serial data from the ATmega328p to a host computer, with no

additional drivers necessary, as they are installed from a small auxiliary microcontroller on the
Uno board.
Atmel Studio 7
We will be programming the microcontroller directly rather than going through the Arduino
programming environment. This allows us lower level (more sophisticated) control, faster
operation/loop rates, and access to additional functions. Atmel Studio is the integrated
development platform for developing and debugging Atmel AVR microcontroller applications.
Applications can be written in C/C++ or assembly code. Atmel has a helpful FAQ section
available on their website, www.atmel.com, called their Knowledge Base accessible under the
Design Support tab. In addition, they provide tutorial videos on youtube at:
http://www.youtube.com/user/AtmelCorporation.
All applications developed in Atmel Studio begin as a project. When starting a new project, you
are prompted to specify which programming language you will be using and the type of project,
and you will need to select which microcontroller you will be programming for.

Getting Atmel Studio set up


1. You can download and install Atmel Studio 7 from the following address (warning: it is a
pretty big download (>700MB) and installation, it will require some .NET components to be
installed as well): http://www.softpedia.com/get/Science-CAD/Atmel-Studio.shtml
(choose the offline installer.) Atmel Studio is a free software.
You may need to also separately install the Arduino drivers as well. This is required for your
computer to automatically configure the USB connection to Arduino as a COM port. These
drivers are available online.
2. Connect your Arduino to the computer via the AVRISP mkII. You will know the AVRISP is
connected properly if both lights on the AVRISP are green. If the light on the face of the
programmer is red, the board is unpowered, and if the light is flashing orange, the programmer
is incorrectly attached to the board. Because AVRISP mkII connections does not power the
Arduino, you need to make a second direct USB connection between Arduino and the computer
for power, as well as for serial communication.
3. From the opening menu, choose to begin a new project.
- The first prompt will ask you to choose a template. Choose GCC C Executable Project.
You will also be able to name your project here.
- Next you will be prompted to select your board. Arduino Uno uses the ATmega328p.
- In the upper left, under the ASF Wizard tab, use the Project: dropdown menu to
select your project. You will not need to install any additional modules.
4. Now you are ready to write your first application. Before you do so, there is additional things
to learn about, summarized in the next section.

Tutorials and Other Resources:


There are many tutorials and web sites that focus on microcontroller programming. The
following are ones that were found (by the instructor and students who took this class) to be
particularly helpful and to follow a clear presentation. You should plan to go through these
tutorials before you start working on the assignment. Additional references at the and are not
required and you can check them if you are interested.
As you work on the lab assignments, if you come across additional resources/sites which you
find to be particularly helpful, please email them to the instructor so that they can be shared with
the others in the class in the current semester or future semesters.
An Overview of Microcontrollers and Microcontroller Programming
The following provide a brief introduction to microcontrollers, steps involved in developing code
for a microcontroller and embedding/writing it on to the microcontroller, and AVR
microcontrollers and their peripherals.
"Basics of Microcontrollers"
http://maxembedded.com/2011/06/basics-of-microcontrollers/
" AVR Basics"
http://maxembedded.com/2011/06/avr-basics/
Serial communication and USART of AVR microcontrollers
The following sequence of three tutorials provide a comprehensive introduction to serial
communication and how to do it using AVR microcontrollers.
"Serial Communication Introduction"
http://maxembedded.com/2013/09/serial-communication-introduction/
" Serial Communication RS232 Basics"
http://maxembedded.com/2013/09/serial-communication-rs232-basics/
"The USART of the AVR"
http://maxembedded.com/2013/09/the-usart-of-the-avr/
Additional References:
As the name implies, the following is a more compact treatment of how to do serial
communication with an AVR microcontroller.
"Simple Serial Communications With AVR Libc
http://www.appelsiini.net/2011/simple-usart-with-avr-libc

Suggested reading on this page can be helpful with serial communication concepts
https://learn.sparkfun.com/tutorials/terminal-basics/serial-terminal-overviewPretty exhaustive one hour lecture on serial communications and USART
https://www.youtube.com/watch?v=W5xt6w5EMP0
Several brief video tutorials by Atmel on USART in Atmel in this playlist
https://www.youtube.com/watch?v=1su5uSRHkpk&index=16&list=PLtQdQmNK_0DRhBWYZ32
BEILOykXLpJ8tP

Additional Notes on Atmel Studio and Matlab:


Moving/Writing your application/code onto the board in Atmel Studio
For moving your code to the board, you need to first compile the code, and if it is without error,
you can build your solution. If there are no errors, you should see several files appear under the
Output Files folder in the far-right Solution Explorer window. The files of interest are the
.hex or .elf files. To write your code onto the microcontroller, you will use the AVRISP mkII
programmer.
1. Under Tools, select Device Programming
2. If the programmer is connected properly, you should be able to select it under the Tool
dropdown menu. You should also specify the device here and press Apply
3. Select Memories and then under Flash select the appropriate .hex or .elf file. Be certain to
erase the device before programming.
Serial port communication in Matlab
Typing help serial will get you started in defining a serial port and provides basic examples in
how to communicate via the serial port. In order to communicate with Matlab, you will need to
have a direct USB connection between the computer and your Arduino Uno board. You can
check the COM port number for the Arduino from Device Manager on your computer. Your
basic Matlab code should:
1. Open a serial port.
2. Read the value you send from the microcontroller (or send a value to the microcontroller.)
3. Close and delete the port.
Modify this basic operation to suit the requirements in different parts of the assignment. The way
Matlab handles serial ports is not perfect. If something goes wrong, it may not be possible to
close and delete an opened port and you will keep getting errors. Sometimes you may have to
terminate Matlab and restart it to eliminate such problems.

Assignment:
The main assignment in this lab is to build a program/application that will set up the
microcontroller for serial communication and transmit and receive some values via the serial
port (a USB port set up to operate as a serial COM port) to be viewed in a serial communication
program you will write in Matlab.
1. Create a function that sends a single character (type char) of your choosing from the
microcontroller to the computer via serial communication (such as letter "A"). Make sure you
can receive and display this character in Matlab (also develop the Matlab code to do this).
2. Create a function that sends a string (composed of several characters, such as a word or a
sentence) from the microcontroller to the computer.
3. Create a function that sends an 8-bit numerical value, of type uint8_t from the
microcontroller to the computer. You will need to convert the number to a character for this.
4. Create a function that sends a 32-bit numerical value (type float), from the microcontroller to
the computer. You will need to split it into 8-bit parts and send them in correct order, and
also receive and combine them in the correct order at the Matlab end. Call this function
print_float.
5. Create a program that makes use of your print_float function, to receive a 32-bit number
from the computer, multiplies it by 5, and sends it back to the computer. When tested from
the Matlab screen, you should input a float type number, then the microcontroller will send
you back 5 times that number, which should be displayed.

Deliverables:
1. Demo: Show your properly commented code in Atmel Studio, explain how it works, and
demonstrate that it compiles correctly and can be written to the microcontroller.
2. Demo: Show your properly commented code in Matlab, explain how it works, and
demonstrate that you can read a value from your Arduino, send a value to the Arduino, and
receive the correct output. Show that all assignment parts (1 to 5) are functional.
3. Write-up: Prepare a summary of what you have completed for this assignment. It does not
need to be in a formal report format, it should mainly be notes for yourself, organizing the
information that you found important and critical while completing this lab. Basically, these are
laboratory notebook type of notes you would need to replicate this lab in the future.
4. Write-up: Using what you have learned thus far, explain how you might send a continuous
stream of float type data from the microcontroller to Matlab, instead of individual
values/numbers. What kind of challenges do you anticipate in such scenario?

You might also like