If you can't read please download the document
Upload
trandien
View
219
Download
0
Embed Size (px)
Citation preview
PH-315 Portland State University
MICROCONTROLLERS
BASIC INPUTS and OUTPUTS (I/O)
ABSTRACT
A microcontroller is an integrated circuit containing a processor and programmable
read-only memory,1 which is widely used as an interface between hardware and
software systems. We will use the Arduino microcontroller. Arduino is a tool for making
computers that can sense and control more of the physical world than your desktop
computer. Its an open-source physical computing platform based on a simple
microcontroller board, and a development environment for writing software for the
board. 2 This laboratory session pursues obtaining familiarity with the Arduino
microcontroller operation, namely installation of communication with your computer,
downloading the proper software and programming code, and gaining familiarity with
the Arduino boards to implement basic I/O tasks.
1. INTRODUCTION
About the board.
Microcontrollers are small computers designed to do real time controls. A microcontroller
is essentially a small programmable computer contained on a single integrated circuit,
consisting of a processor, read-only memory (ROM) used to store the program instructions,
and a set of input and output pins which can be used to interact with an external circuit.
The microcontroller IC is a digital device and, since the processor operates on transistor to
transistor logic (TTL), only two logic states are acceptable: HIGH (~ +5 V) and LOW (~0 V). The
microcontrollers come in all shapes, sizes, and layouts. Usually, they are quite small and use
less power than traditional computers. Microcontrollers are often deployed in appliances
and serve an unmodifiable dedicated purpose, such as keeping track of what spin cycle your
washing machine is on, or how much time is left before it should turn off your microwave
oven. Make no mistake, however, these are general purpose computers.
A major difference between a microcontroller and traditional computers is that they
come with an array of analog and digital inputs and outputs. These inputs and outputs
can be used to read environmental data from sensors, talk to other computers or devices
and electronically control other systems which provide environmental outputs such as a
LCD screens, mechanical switches or servo motors etc.
Getting started with microcontrollers can be a tedious process, as they can require a
number of supporting circuits, USB controllers, programmers, boot-loaders and power
supplies just to load your first program onto the microcontroller chip. Often times you will
start with a prototyping board which puts all of the necessary components in a
convenient and ready to use package.
Arduino Microcontroller and its advantages
This lab will be using the single board Arduino Leonardo Microcontroller.3 It is similar to
the Arduino Uno,4 with the major difference being that the latter uses Surface Mount
Technology (SMT)5 instead of the older thru-hole 6 technology. The Arduino drastically
lowers the difficulty of getting started with a microcontroller (compared to to plain
ATMEGA/PIC/ARM chips), as it provides all the necessary tools for making the
microcontroller do interesting things, which would be daunting if staring with just a plain
microcontroller chip.
The Arduino is based around an 8-bit Atmel AVR microcontroller, and has supporting
systems like a boot loader for uploading programs, a USB controller, as well as a barrel jack
for external power.
It is programmed using a language that is based off of C++.
It uses an integrated development environment (IDE) for writing, compiling and uploading
your programs to the board.
Figure 1: The Arduino Leonardo Microcontroller.2
2. GETTING STARTED. How to talk to the board This lab is based off of the Arduino software, which can be downloaded for free from the
Arduino website. 7 Unlike other embedded systems development environments Arduino software is quick to download and set up, and has zero cost associated with the software, which makes it a convenient to work with when the primary goal is to come up with a working
prototype quickly and cheaply. There is a large community of Arduino users and there exists a massive pool of example programs and libraries compared to other educational prototyping boards.
Whatever you do, DO NOT APPLY MORE THAN 5V TO ANY PIN ON THE ARDUINO. Otherwise
it will damage or destroy the microcontroller board. Also, avoid powering directly with the
Arduino devices that draw high current. Instead opt for a separate power source and an NPN
transistor or something similar.
Find a Computer
You are free to use one of the classroom computers or your own laptop. Plug your Arduino into
the computer using the micro USB cable. Please be careful with the delicate connectors.
A. Download and Launch the Arduino Software
Download the latest Arduino Integrated Development Environment (IDE)7 available
at the following site,
http://arduino.cc/en/Main/Software
The downloading process may take a few minutes.
Then go to the downloads folder in your computer (typically located in the favorites
section). Identify with your mouse the Arduino application file, and enter a left-click.
After a few minutes, the monitor screen should pop-up the following message: Do you
want to allow this program making change in your computer? Proceed accordingly.
(You may be prompted to add hardware if you are on windows. If it asks for a driver, tell
the windows driver wizard to look inside a folder called drivers inside the Arduino folder.
If the computer you are using already has the Arduino software downloaded, look inside
that folder usually found in Program Files, or wherever you copied it, or the shortcut on
the desktop leads too.)
B. Selecting the Board
The Arduino application software is now installed in your computer. It should be
available from the Start menu (lower left corner) of your computer
Click on the Arduino application to launch the Arduino Integrated Development
Environment (IDE).
It is time now to tell the Arduino IDE what particular Arduino board we will be using (i.e.
to tell the IDE what hardware it will be compiling). For that purpose, once the IDE is
open, navigate to the toolbar and select the Leonardo board,
Toolbar Tools Board Arduino Leonardo
If you are using a different board, select the one you have from this list instead.
This step may vary from system to system.
C. Selecting a Serial Port
This step varies from system to system. This step is to tell the computer which serial
port the Arduino chip can be reached at, for both programing the board as well as
talking to it during runtime.
Figure 2: Port selection in Windows and OS X.
C.1 Windows
Select Toolbar Tools Serial Port COM5
where COM5 is the serial port that has been assigned to your Arduino by windows. (The computer may have assigned another port number to your Arduino, like COM4, for example; check it out).
You may have more than one port in the list. To know which port is associated with the Arduino, you can check the list with the Arduino unplugged, and check it again with it plugged in. The extra port that appears in the list is the Arduinos port.
C.2 OS X
Usually the Arduino is the first item in the Serial Port list. Another way to tell is that it has tty in the name and does not have the world bluetooth in it.
NOTE: Sometimes, even if the port selection has been set through the Arduino program (as described above), the actual selection may not happens. It may be necessary then to specify the serial port though the following sequence:
Left-click the Start button; right click on Computer, select Properties >> Left-
click on Device Manager >> Ports
3. PROGRAMMING the ARDUINO
A. Compiling and uploading your first program. Example program: The Blink program
We have chosen the blink program available in the Arduino library. For that purpose, navigate to, Toolbar File Examples 01. Basics Blink
The program shown in Fig. 5 will appear in your screen. We proceed next to verify that it compiles, and then to upload it to your microcontroller board.
A.1 Compiling your program lets the compiler check the program for syntax and structure
errors.
Press the verify button, located at the top-left side of the screen (see Fig. 3).
It will take a few minutes to compile the sketch and then to return the Done compiling
message (see Figure 3). If you get an error, something went wrong.
Figure 3. Left: The Verify and Compile buttons. Right: Successful-compile message.
A.2 Once the compiling process is completed, go ahead and upload the program to the
micro-controller board by pressing the upload button (located right next to the verify
button).
This process should provide a similar completion message after a few seconds. The
LEDs on the Arduino will blink during the upload, but should settle down after a few
seco