Tutorial 6: Introduction to Arduino



Download 17.7 Kb.
Date13.06.2017
Size17.7 Kb.
#20398
Tutorial 6: Introduction to Arduino

Arduino is a microcontroller mounted on a small printed circuit board with several connection points that enable it to talk to the outside world. A microcontroller is really a small computer that can be programmed to communicate with the devices it is connected to, and to perform useful tasks. In the not-so-distant past, microcontrollers were programmed in assembly language, which required the user to be familiar with things like hexadecimal coding, shift registers and the like. Luckily for us, Arduino is programmed in a language that looks a great deal like C++.


There are several versions of Arduino available on the market, but we will use the Arduino UNO in ME Lab. The UNO has 14 digital input/output pins and 6 analog input pins. A digital pin can take one of two states: HI or LO, which corresponds to +5V or 0V. An analog pin, by contrast, can read in any voltage ranging between 0 and +5V. Please note that connecting Arduino to a voltage higher than +5V will likely damage the microcontroller, the board, or both.




Exercise 1: Turn on an LED


As a first exercise, we will program Arduino to turn on an LED, and make it blink. If you simply connect an LED to one of the digital outputs of Arduino, you might be in for a nasty surprise. An LED can only pass a certain amount of current before it burns out; usually this is around 10mA or so. To ensure that Arduino doesn’t supply too much current to the LED, we must include a current limiting resistor in our circuit, as shown below.

To determine the size of the resistor, we must do a simple calculation. The LEDs supplied for this lab have a “forward voltage drop” of 1.8V. This means that the voltage drops by 1.8V when the LED is turned on. Using Kirchhoff’s voltage law on this simple circuit gives

But we wish to provide 10mA to the LED, so that

Rearranging, and solving for R1 gives

The nearest standard resistor is 330Ω, so we choose that. Wire up the circuit shown above, with the resistor connected to digital pin 12. Next, start the Arduino software, connect the USB cable, and write the following program:
/*

Blink


Turns on an LED on for one second, then off for one second, repeatedly.

*/
const int led = 12; // Pin 12 is connected to the LED


// the setup routine runs once when you press reset:
void setup() {

pinMode(led, OUTPUT);// initialize the digital pin as an output.

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

void loop() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}
Let us take a look at some of the important parts of the program. The first line defines a constant integer called led and sets its value to 12. This variable stores the pin number for the LED that we wish to turn on and off. We use a constant integer because it reduces memory usage and (generally) makes the program run faster.
The next part is the setup() function, which is run once, when Arduino is turned on (or when the Reset button is pressed). For this simple program, we set the input/output mode of pin 12 to output. It is important to tell Arduino if a pin will be used for input or output, so that the microcontroller will know if it needs to supply current to the pin or not. In some cases, very mysterious behavior has been traced back to the fact that the input/output state of a pin was defined incorrectly (or not at all).
The final part of the program is the loop() function, which runs again and again until power is turned off. For this simple loop, we set pin 12 to high (+5V), wait for 1000ms, set it to low (0V), wait for 1000ms, and then repeat ad infinitum.

Challenge 1: KITT


Can you wire up 5 LEDs to the Arduino, and make the light pass back and forth, like KITT on Knight Rider?


Exercise 2: Reading an Analog Input


One of the most common tasks for Arduino is to read in an analog signal, usually for the purpose of taking a measurement. There are six analog inputs which can be used for this purpose. An easy way to get an analog signal into Arduino is to use a potentiometer, as shown in the circuit above. By turning the dial on the potentiometer, you can vary the voltage seen by Analog Pin 0 between 0V and +5V.
In order for Arduino to be able to make sense of an analog signal, it must first convert it to digital format. The Arduino microcontroller has six built-in analog-to-digital converters (ADCs), which accomplish this task. The job of an ADC is to transform a continuously variable signal (from 0 to +5V) into a binary number.

One measure of the quality of an ADC is its resolution; that is, how fine the steps are between the binary numbers that the ADC puts out. For example, a 2 bit ADC would have 4 steps (22 = 4) and an 8 bit converter would have 256 steps. The figure above shows the same sine wave being discretized with two different resolutions: 2-bit and 4-bit. The higher resolution is able to model the sine wave much more accurately. Note that the figure on the right actually has 17 steps – this was an error in drawing the figure. It turns out that the converters on Arduino have 10 bits, which means that the 0-5V signal will be converted to a number between 0 and 1023.


Of course, reading an analog signal with Arduino is useless if we don’t do something with the information. It is quite simple to send the analog readings to a window on the computer using the Serial Monitor. If you click on the button in the upper right of the Arduino window (it looks like a magnifying glass) it will open up the serial monitor.
For this exercise we will combine the two concepts above into a single program. This program will instruct Arduino to read in an analog signal from analog pin 0, and display the result as a number on the Serial Monitor.
/*

AnalogReadSerial

Reads an analog input on pin 0, prints the result to the serial monitor.

Attach the center pin of a potentiometer to pin A0, and the outside pins to

+5V and ground.

*/
void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

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

void loop() {

int sensorValue = analogRead(A0); // read the input on analog pin 0:

Serial.println(sensorValue); // print out the value you read:

delay(1); // delay in between reads for stability

}
The Serial.begin command instructs Arduino to prepare to write to the Serial Monitor. The analogRead command reads the analog signal present on pin 0, and converts it to a number between 0 and 1023. It stores this value in the variable sensorValue. It takes a finite (but very short) amount of time to perform the analog to digital conversion, so the program stops execution for 1ms using the delay command. Type the program in the Arduino window and watch the results!



Challenge 2:


Write a program that does one of three things - your choice!
1. Read the analog signal from the potentiometer, and change the rate of blinking of one or more LEDs accordingly.

2. Read the analog signal from the potentiometer, and turn on between 0 and 5 LEDs depending on the reading (i.e. make a voltmeter!)



3. Read the analog signal from the potentiometer, and change the rate of the “KITT effect” accordingly.




Download 17.7 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page