BURST Microcontroller Lab

BURST Microcontroller Lab

Microcontroller Introduction (Arduino)

Introduction

A microcontroller is a small, typically single chip, computer with the capability to read external signals and respond to them. The usual way a microcontroller interacts with the outside world is through input/output pins. Microcontrollers must also have some interface for programming them as well as memory to store the programs and to provide temporary memory space for program execution.

Microcontrollers are extremely common nowadays. They are used in most household appliances, in thermostats, in microwaves, in furnaces and ventilation systems.Dozens of them are found in cars, hundreds in airplanes. Very powerful microcontrollers are now built into cell phones. They are extensively used in all forms of robotics and autonomous control.

In this lab, you are going to learn about microcontrollers in a hands-on fashion by actually programming one to do various tasks.

ArduinoMicrocontroller

For this lab, we will use a very easy to use microcontroller called Arduino. Arduino is an open source project based in Italy. ( The programming language is a variant of C which is straightforward, and the system library is very rich. With little fuss you will be able to learn a great deal about how microcontrollers work and how to program them.(The actual microprocessor chip the Arduino uses is made by Atmel, which is based in San Jose, California.)

Below is an enlarged photograph of the Arduinomicrocontroller module (technically the Uno R3 module):

style

The Arduino has 14 input/output pins on one side (labeled 0 through 13) and 6 input/output pins on the other (labeled A0 through A5). It is these pins that allow external information flow in and out of the microcontroller.
|

In this lab, you will primarily be concerned with these input/output pins. The six pins labeled A0 through A5 have the analog to digital conversion (ADC) capability. This means that you can measure an analog voltage (between 0 and 5V) on these pinsto a certain degree of precision. In the case of the Arduino, the precision is 5/1023 or about 5 mV.

Programming the microcontroller

You will be shown how to program the microcontroller, but the process is very simple and isdescribed below.

  1. Open the Arduino Programand plug the USB cable into the computer and into the microcontroller module.
  2. Make sure the Arduino environment knows the version of the board you are using by clicking on Tools->Board and choosing the right board. We are using Arduino Uno.

style

  1. Very important step: Make sure the program knows which USB port the Arduino is using.

Select the serial device of theArduino board from the Tools->Serial Port menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). On a Mac, the name will start with tty. To find out, you can disconnect your Arduino board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.

  1. Save your file using File->Save. This will create a new folder in the Sketchbooks folder with the same name.For your first program, you can use the name “Blink.”
  2. Type you code in the module. For example, for a first program you can type:

6. Finally, click on the “Compile and Run” button:

Another useful button is the compile button.

The compile button allows you to check if the syntax of your program is correct.

  1. The led on the board (connected to pin 13) will turn on and off.

Programming Language and System Library

You can find reasonably good documentation on the programming language and system library in Help->Reference docs.

The programming language is a variant of C. This means, among other things, that each line ends with a semicolon, and all variables must be declared. C code is case sensitive. Here are some of the most important types:

Type / Storage / Range
boolean / 8 bits / true, false
byte / 8 bits / 0 to 255
char / 8 bits / -128 to 127 (ascii) or use notation 'a', 'b', etc.
int / 16 bits / -32768 to 32767
unsigned int / 16 bits / 0 to 63535
long / 32 bits / -2,147,483,648 to 2,147,483,647
float(same asdouble) / 32 bits / -3.402823 E+38 to -3.402823 E+38
string / variable / character array

Variables are declared in typical C syntax:

bytePinNumber;

unsignedintPinReading;

longEventCount;

Very helpful are the type conversion functions:

Type Conversion Functions
char() / Convert to char
byte() / Convert to byte
int() / Convert to int
long() / Convert to long
float() / Convert to float

As in all C languages, code comments are inserted using a double forward slash: //. All characters to the right of the double forward slash are ignored.

//This is a full line comment.

I = 0; // This is an inline comment.

Structure of programs.

All Arduino programs must have the following components. Note there can be muchmore in addition, but these two components are required:

// the setup routine runs once when you press reset:

void setup()

{

// Whatever code here occurs one time upon reset

}

// the loop routine runs over and over again forever:

void loop()

{

}

Lab Exercise 1. Control of an outputon a pin and delays

A very important system call is digitalWrite(pin_number, HIGH or LOW) which sets an output pin either high (5V) or low (0V).

Before you write to the pin, you need to declare the pin to be an input or output in the setup() routine:

pinMode(pin_number, OUTPUT);

For the pin numbers, use 0 through 13 for digital pins and A0 through A5 for the other pins.

Another very useful system call is delay( number_of_milliseconds)which causes the microcontroller to do nothing for a fixed number of milliseconds. The argument must be an unsigned integer or unsigned long integer.

So delay(2300) causes a delay of 2.3 seconds.

delay(2000.3) causes a compilation error.

Why does delay(2000.3)fail?

Your task in Lab Exercise 1:Install your own LED with a current limiting resistor in series on pin 9 and cause it to turn on for ½ second and turn off for ½ second, three times.

For those of you not so familiar with LEDs, the following diagram may help:

style

Lab Exercise 2. Loops

This exercise is a simple extension of the first one, however it introduces an extremely important programming concept: looping. In the first exercise you caused the LED to blink on and off three times. What if you wanted to cause it to blink 30 times or an indefinite number of times? For this, we need variables and for loops.

Example syntax for a loop in C is

for (inti=0;i<10;i++)

{

// code that gets repeated goes here

}

Your task in Lab Exercise 2: Declare an integer variable Number_of_Blinks, set it equal to 15, and cause the LED you installed in Exercise 1 to blink on for ½ second and off for ½ second 15 times.

Lab Exercise 3. Reading Pins and While Loops

So far, you have controlled the voltage on a pin. You can also read a voltage on a pin. Pins 0 through 13 are digital pins, meaning one can only detect whether the pin is high (above 3V or so) or low (below 1.5 volts or so). Pins A0 through A5 can be used eitheras digital pins or as analog pins. When the pins are used as analog pins, you can measure a voltage between 0 and 5V to a certain degree of precision.

The syntax for reading a voltage on a pin is as follows. To read a pin in digital mode use

VariableToStoreValue = digitalRead(PinNumber);

To read a pin in analog mode use

IntegerVariable = analogRead(PinNumber);

The output of analogReadis an integer between 0 and 1023, with 1023 corresponding to 5V.

As in the case of outputs, you need to declare the pin as being an input first:

pinMode(PinNumber, INPUT);

In this exercise we will only use the digital version, digitalRead.

A common use of input pins is to wait for and to react to button presses. In order to use a button switch, you need a pull-up resistor however.

In the diagram above, when Pin 5 is configured as an input pin, it is set to have very high impedance (as if it weren’t even connected). What is voltage at Pin 5 when the switch is closed? What is the voltage when is open?

It turns out that this situation is so common that the option for a pull-up resistor is built into the microprocessor: if you declare the input variable as

pinMode(PinNumber, INPUT_PULLUP);

the pull-up resistor is internally connected.

The other syntax you need is a while loop. Similar to a for loop, a while loop will cause code to be executed repeatedly while a certain condition is met. The syntax is

while( Boolean condition )

{

//Code that gets repeated goes here.

}

A typical usage is to cause the microprocessor to wait until a condition is met, e.g. until a button is pressed:

while (digitalRead(A0)==1) // wait for switch to bepressed

{

//do nothing

}

If you want to do something repeatedly forever, you can write

while (true) //

{

// whatever goes here gets repeated forever

}

Your task in Lab Exercise 3:

Using the above concepts, your task in this exercise is to write code to wait for a button press on Pin 5 and then blink the LED on pin 13 (½ second on, ½ second off) indefinitely.

1