University of Portland EE 271 Electrical Circuits Laboratory Experiment: Arduino I. Objective The objective of this experiment is to learn how to use the Arduino microcontroller to monitor switches and sensors and to activate devices such as LEDs or a speaker when the sensor output is a given value. II. List of Needed Components This experiment requires the following components: Two 1 kω resistors One 100 kω potentiometer One Arduino Uno One USB cable III. Background The Arduino is a microcontroller board that can be programmed to read switches and sensors and activate lights, speakers, motors, and other devices. Microcontrollers are used in a wide variety of devices such as cars, cell phones, cameras, appliances, printers, etc. Among their many uses, microcontrollers can be programmed to monitor a sensor and to activate some kind of output device when a given condition occurs. In a motion controlled light, for example, a microcontroller might turn a light on after a motion detector senses movement, and turn the light off after a given amount of time. Microcontrollers are often used to monitor sensors and control motors or other actuators in robots, quadcopters, etc. The Arduino website (www.arduino.cc) has details of what an Arduino Uno is and how to program it. In particular, if you want to download the Arduino software for free, click on Software. For technical details for the Arduino Uno board, click on Products, Arduino, and then click on Uno. To learn more about programming the Arduino, click on Learning, Reference. There are also tutorials, a forum, a list of Arduino projects, etc. No prior programming experience is necessary to complete this lab. University of Portland - p. 1 of 9 - Exp - Arduino.docx
IV. Prelab Assignment 1. What does the Arduino command pinmode do? 2. What does the Arduino command digitalwrite do? 3. What does the Arduino command digitalread do? 4. What does the Arduino command analogread do? V. Procedure Part 1: Arduino Set Up In this part of the experiment, we will use an existing sketch to blink an LED on and off. The Arduino microcontroller is programmed using free Arduino software which is a text editor that allows you to write programs, which are called sketches (see Figure 1). Figure 1: Arduino Software Connect the Arduino board to the computer using the USB cable first, and then open the Arduino software. The Arduino software comes with several example sketches. Open the example sketch called Blink by clicking on File, Examples, 01.Basics, Blink. The Blink sketch will open in a new Arduino window. University of Portland - p. 2 of 9 - Exp - Arduino.docx
Configure the software for the Arduino Uno board by clicking on Tools, Board, and select Arduino/Genuino Uno. Also click on Tools, Port, and select the port with Arduino/Genuino Uno. If there are not any ports labeled Arduino/Genuino Uno, try unplugging the Arduino from the computer and plugging it in again. Click on the Upload Button (see Figure 1), which will compile, upload, and run the sketch. When the process is complete, the LED near pin 13 should repeatedly turn on for 1 second, then off for 1 second. If you get an error such as Problem uploading to board, click on Tools, Port, and select another port. Part 2: Blink Sketch Let s examine the Blink sketch and modify it to blink in a pattern of your choice. The Arduino board is programmed using the computer language C++, but the Arduino software includes several features that make it possible to write sketches without knowing C++ in detail. The text at the top of the Blink sketch is a comment. Comments are used to document the sketch for the benefit of people who read or modify it, but comments are ignored by the Arduino software. There are two ways to mark text as a comment. If a line contains the characters //, then the rest of the line is considered a comment. Also, all the text between the characters /* and */ is a comment. The setup function is used to initialize the sketch. It is executed only once when the Arduino board is first powered up or when it is reset using the reset button. The Arduino Uno has 14 general purpose input/output (I/0) pins which can be configured to be either inputs or outputs. Pin 13 is connected to an LED on the Arduino board, and the command pinmode(led_builtin, OUTPUT) sets pin 13 so that it functions as an output because the value of LED_BUILTIN is set to 13 by the Arduino software. Any pin can be set to an output by replacing LED_BUILTIN with the pin number. Any pin can be set to an input by replacing OUTPUT with INPUT. Note that there needs to be a semicolon ; after each command, and the commands inside a function are enclosed in curvy brackets and. After the setup function is complete, the loop function is called repeatedly until the Arduino is either powered down or reset. In this sketch, the command digitalwrite(led_builtin, HIGH) is used to set Pin 13 to HIGH, which causes the pin to output +5V and turn on the LED. Any pin can be set to HIGH by replacing LED_BUILTIN with the pin number. The delay(1000) command causes a 1000 ms (or 1 second) pause. The command digitalwrite(led_builtin, LOW) sets Pin 13 to 0V, which turns the LED off. Then, after another 1000 ms pause, the loop function is called again, repeating the process. University of Portland - p. 3 of 9 - Exp - Arduino.docx
Part 3: Blink LEDs Construct the circuit shown in Figure 2. Make sure you connect the center connector (Pin 2) of the potentiometer to Arduino Pin A0 (see Figures 3 and 4). The Debounced Pushbutton is connected to ground internally, so you do NOT need to make that connection yourself. Also, note that the Arduino pin marked 5V is a power supply generated on the Arduino board that we are using to supply power for this experiment, so do NOT connect the bench supply or the Proto-board supply to the Arduino! Figure 2: Arduino Circuit University of Portland - p. 4 of 9 - Exp - Arduino.docx
Figure 3: Arduino Pins Figure 4: Pinout of Potentiometer Turn on the power to the Proto-board (otherwise the Debounce Pushbutton circuit won t work). In the Logic Indicator section on the Proto-board, there is a switch labeled 5V/+V; set this switch to 5V. There is also a switch labeled TTL/CMOS; set this switch to TTL. Test your circuit by using the DMM to measure the voltage that is connected to Arduino Pin 2. It should be 0 V when the button is not pressed, and 5V when the button is pressed. The voltage that is connected to Arduino Pin A0 should vary between 0 and 5V as the knob on the potentiometer is turned. Note that the Arduino input pins should never be connected to a voltage that is larger than 5V or less than 0V, because that could damage the Arduino. Save a copy of the Blink sketch in your P: drive by clicking on File, Save As. Then modify the sketch to make the four Logic Indicator LEDs blink in a pattern of your choice. Save your sketch to your P: drive. Part 4: Read a Digital Input In this part, we will examine an example sketch that reads a digital input. This sketch could also be used to monitor any sensor, such as a motion detector, that has a digital output. Open the example sketch called DigitalReadSerial by clicking on File, Examples, 01.Basics, DigitalReadSerial. Compile, upload, and run the sketch by clicking on the Upload button. Then click on the Serial Monitor button (see Figure 1). The sketch should print 0 to the Serial Monitor when the button is not pressed, and 1 when the button is pressed. Let s examine the DigitalReadSerial sketch. The command int pushbutton = 2 creates a variable called pushbutton and sets its value to 2. This variable is type int, which means that it can only store integer values. Variables that are defined outside of a function are global, which means that they can be used in any of the functions. The University of Portland - p. 5 of 9 - Exp - Arduino.docx
purpose of this variable is to store the number of the input pin to which the pushbutton switch is connected. In the setup function, the command Serial.begin(9600) initializes a serial connection (one bit at a time) with the computer that will transfer data at 9600 bits per second. This command is necessary if the sketch prints out any values. The pinmode(pushbutton, INPUT) command initializes Pin 2 as an input. (When the Arduino powers up, it automatically sets the I/O pins to inputs, so this command is not actually necessary.) In the loop function, the command int buttonstate = digitalread(pushbutton) creates an integer variable called buttonstate. Variables that are defined inside a function are private to that function, which means that the variable is only available to commands that are also inside that function. The function digitalread reads the value from Pin 2 and stores the result in the variable buttonstate. If the voltage on the input pin is near 5V, then the function digitalread returns the value 1, otherwise it returns 0. The command Serial.println(buttonState) prints the value of the variable buttonstate to the Serial Monitor. The delay(1) command causes a 1 ms delay. The loop function is repeatedly called until power is shut off or the Arduino is reset. Save a copy of the DigitalReadSerial sketch in your P: drive by clicking on File, Save As. Let s modify the sketch so that it does something different depending if the switch is pressed or not. Add the following lines inside the loop function after the line with digitalread : if (buttonstate == 0) // if switch is not pressed (note there are two equal signs) digitalwrite(13, LOW); // turn LED off else digitalwrite(13, HIGH); // turn LED on Try out the above modification. The LED connected to Pin 13 should be off when the switch is not pressed and on when the switch is pressed. Modify the sketch so that it blinks the LEDs in two different patterns depending on if the switch is pressed or not. Save your sketch to your P: drive. Checkpoint 1: Demonstrate your sketch to the instructor. University of Portland - p. 6 of 9 - Exp - Arduino.docx
Part 5: Read an Analog Input In this part, we will examine a sketch that reads an analog input voltage from a potentiometer. This sketch could also be used to monitor any sensor, such as an RTD, photosensor, etc., that has an analog output. Open the example sketch called AnalogReadSerial by clicking on File, Examples, 01.Basics, AnalogReadSerial. Click on the Upload button and then click on the Serial Monitor button (see Figure 1). As you turn the potentiometer knob, the voltage Vi in Figure 2 should vary between 0 and 5V, and the printed values should also vary. Determine the relationship between the voltage Vi and the values printed in the Serial Monitor. The AnalogReadSerial sketch initializes the serial connection to the computer to 9600 bits per second in the setup function. In the loop function, the sketch uses the analogread command to read the analog voltage from pin A0, which means that it converts the voltage on pin A0 to an integer. The integer value is stored in the variable sensorvalue. The value of the variable sensorvalue is printed to the Serial Monitor followed by a 1 ms delay. Save a copy of the AnalogReadSerial sketch in your P: drive by clicking on File, Save As. Then modify the sketch to convert the value that is returned from the analogread function to volts, and then print out the value in volts. In order to store the value in volts, you will need an additional variable that can store floating point numbers, so instead of using type int, use type float. Also, when you compute the voltage, you will need to tell the Arduino to use floating point arithmetic by using constants with decimal points like 5.0 and 1023.0 instead of 5 and 1023. Run your sketch and record the voltage Vi and the voltage computed by your program (see Table 1). Also compute the percent error. Save your sketch to your P: drive. Table 1: Potentiometer Voltage Versus Computed Voltage Potentiometer Voltage Vo Computed Voltage (from Serial Monitor) % Error University of Portland - p. 7 of 9 - Exp - Arduino.docx
Part 6: Making Tones In this part, we will write a sketch that generates tones with a speaker. Start a new sketch by clicking on File, New. Then type (or paste) the following sketch into the Arduino software. /* * Generate Tones */ int speakerpin = 3; // pin connected to the speaker void setup() // put your setup code here, to run once: pinmode(speakerpin, OUTPUT); // set speakerpin to output void loop() // put your main code here, to run repeatedly: int i; // loop counter int frequency = 500; // frequency in Hz int duration = 250; // duration in ms int shortdelay = 500; // short delay time in ms for (i = 0; i < 3; i++) // generate tone tone(speakerpin, frequency, duration); delay(shortdelay); // short delay delay(5000); // long delay Run the sketch. If the tones are too quiet, change the 1 kω resistor that is in series with the speaker to 150 Ω. Do NOT decrease the resistance below 150Ω because that could damage the Arduino. The first argument to the tone function is the pin number that is connected to the speaker, the second is the frequency of the tone in Hz, and the third is the duration of the tone in ms. The for command, which is also called a for loop, causes the tone and delay functions to be repeated three times. The for command has three has three commands in parentheses. The first command i = 0 is executed only once when the loop begins. It is used to initialize the loop counter. The second command i < 3 is a test, and if the test is true, then the statements enclosed in the parentheses are executed. Then the third command i++ is executed, which in this case increments the value of the loop counter by 1. This process repeats until the test fails, and then the sketch skips down to the commands after the loop. University of Portland - p. 8 of 9 - Exp - Arduino.docx
Therefore, for command is equivalent to the following commands: i = 0; // initialize counter variable (this command is executed only once) i < 3; // this test is true, so do the loop commands tone(speakerpin, frequency, duration); // generate tone delay(delaytime); // delay i++; // increment loop counter, so now i equals 1 i < 3; // this test is true, so do the loop commands tone(speakerpin, frequency, duration); // generate tone delay(delaytime); // delay i++; // increment loop counter, so now i equals 2 i < 3; // this test is true, so do the loop commands tone(speakerpin, frequency, duration); // generate tone delay(delaytime); // delay i++; // increment loop counter, so now i equals 3 i < 3; // this test is now false, so skip down to the commands after the loop Connect the oscilloscope to Arduino Pin 3 and measure the period of the tones. Make a sketch of the oscilloscope display and label the period and the maximum and minimum voltages. Then compute the fundamental frequency and the percent error compared to the frequency that was specified in the tone command. Modify the sketch to play tones of your choice. Save your sketch. Part 7: Explore Write a sketch to monitor a switch, potentiometer, or sensor and use that input to activate LEDs and/or the speaker in some way. VI. Conclusion Write a paragraph that summarizes what you have learned in this lab. What is an Arduino? What types of tasks can an Arduino be used for? How is an analog voltage represented? Checkpoint 2: Show your conclusion and demonstrate your sketch from Part 7 to the instructor. University of Portland - p. 9 of 9 - Exp - Arduino.docx