Lab 14 Electronic Sensors And The Arduino Microcontroller

4m ago
918.31 KB
10 Pages

Lab 14Electronic Sensors and the Arduino MicrocontrollerObjectives – in this lab you will Understand how microcontrollers use electronics to interact with the outside worldLearn the basics of writing programs for a microcontrollerWrite programs that measure and respond to input from electronic sensorsRead a potentiometer, control LEDs, and produce audio outputKey Prerequisites Labs 1-13Required Resources Arduino UNO, PC with internet access, Lab Kits, DMM and OscilloscopeReferences Arduino on WikipediaArduino Programming Language ReferenceIntro to Arduino from SparkFunArduino on Mac OS-XWhat’s a Microcontroller? (Good sensor reference for Parallax Microcontroller)This lab introduces a ubiquitous element of modern electronics systems: the microcontroller. Up tonow, our circuit labs have demonstrated various physical properties of electronics. In this lab we willexplore how circuit designers use the principals we’ve been learning to interface computing systems(microcontrollers) with the outside world, allowing for a vast array of consumer electronics andindustrial applications.Microcontrollers occupy a key (though hidden) place in the electronic devices that we use every day.They are literally everywhere, providing the intelligence to make products behave properly.1

Lab 14 Electronic Sensors and the Arduino MicrocontrollerToday’s hobby microcontroller market allows non-engineers access to these powerful devices forindependent experimentation and invention. The market is growing rapidly and now includes suchcelebrity products as Arduino, PicAxe, and Raspberry Pi, and classics such as the Lego MindstormsRCX/NXT Brick and one of the earliest—and still popular—Parallax Basic Stamp 2.In this lab we will introduce one of the more recent arrivals, the Arduino UNO Rev 3, one in a broadline of product from the Arduino open-source hardware and software company and its devoted usercommunity. From Wikipedia:The first Arduino was introduced in 2005. The project leaders sought to provide aninexpensive and easy way for hobbyists, students, and professionals to create devices thatinteract with their environment using sensors and actuators. Common examples for beginnerhobbyists include simple robots, thermostats and motion detectors [1].Figure 2: The Arduino UNO R3 is a complete microcontroller system for about 25 [2].VocabularyAll key vocabulary used in this lab are listed below, with closely related words listed together: Integrated Development Environment Sketch variable function2

Lab 14 Electronic Sensors and the Arduino MicrocontrollerDiscussion and ProcedurePart 1. Setup and TestingInstalling Arduino Software (for online students)To begin, if you are working remotely on this lab, you’ll need to download the Arduino softwareIntegrated Development Environment (IDE), which is the application where we write and executeprograms on our Arduino microcontroller. Simply visit the Getting Started with Arduino page andfollow the link for your operating system (Windows, MAC OS-X or Linux). Continue following thesetup instructions all the way through running the “Blink” example program. Verify that the yellowLED near pin 13 blinks 1 second on, 1 second off after the program has been uploaded. If you runinto problems, visit the “troubleshooting suggestions” link from the “Getting Started” guide and/oremail the instructor for suggestions.Connecting for the First Time (for classroom students)If you are working in the classroom, your laptop should already have the Arduino software installed.Here are the steps to bringing up the Arduino software for the first time:1. Before running the Arduino IDE, first plug the USB cable into your Arduino, and the other endinto your laptop. Windows will load the correct USB drivers and display a message in the lowerright hand corner “Arduino UNO on COM Port X” where X is a number. Remember thisnumber, since it can be useful later.2. Open the Arduino program3. Select Tools Board and choose Arduino UNO4. Select Tools Serial Port and if you see multiple ports, choose COM Port X, where X was thenumber shown in step 1. This step was able to resolve connection problems that some people raninto in the lab.5. Open the blink program (in Arduino-speak, a program is a “sketch”). Choose File Examples 01.Basics BlinkYou should get a program that looks something like the code in Figure 3 below.6. Before running the blink program, make a note of how fast the yellow LED next to Pin 13 on theArduino is blinking. It’s probably going on for ½ second, off for ½ second, or about 1 second fora complete cycle.7. Now upload the Blink program from your editor into the Arduino. Click the right-arrow buttonbeneath the word “Edit”. There will be some messages shown on the bottom of the screen as theupload takes place. If everything works ok, you should notice the yellow LED on the Arduinoslow down to 1 second on, 1 second off, or about 2 seconds for a complete cycle.All StudentsThe programming language used by the Arduino is a variant of C, a powerful programming languageused in a lot of microcontrollers. If you look closely at the program in Figure 3, you’ll notice it has3

Lab 14 Electronic Sensors and the Arduino Microcontrollersome things that your program doesn’t. For instance the line int led 13; This line creates avariable – just like in MATLAB. This variable holds the number 13. It is also restricted to only holdvalues of type “integer” or whole numbers, hence the word int that comes at the beginning. Thisvariable comes in handy farther down the program when the pinMode and digitalWrite commandsare used. We use the variable led instead of 13 because that provides a little extra clarity on whatour program is doing. It also makes it easier to change the pin we are using later to something else,such as 7 or 9, without editing all those statements as well.8. Modify your program so it looks exactly like the program in Figure 3 below, by declaring the ledvariable and using it in the other commands shown. Rerun your program and make sure it stillworks.Figure 3. Blink, a typical Arduino SketchFigure 3 also has a number of red boxes to show you the structure of an Arduino program. Ingeneral, it’s best to create variables in the area marked by the first red box. Variables created herecan be referred to anywhere else in your program.The second red box shows the setup function. A function is a block of code, surrounded by { and },with a name at the top (in this case, void setup() ). The setup function is where we perform anyinitialization steps we need to do. In this case, we have to indicate that Pin 13 will be used foroutput. This allows an extra amount of current to come out of the pin when it is set to high, so thatwe can illuminate an LED with it.4

Lab 14 Electronic Sensors and the Arduino MicrocontrollerThe third red box shows the loop function. This function starts running after the setup function, andit keeps running over and over again until we turn off the power to the Arduino, or upload a newprogram. This is where all of the action happens! In this case we are turning on the LED, waiting 1second, turning off the LED, waiting another second, and repeating this over and over again.This general form is common to all Arduino programs. In summary, there are three sections:1) a section for creating variables that will be used in the program2) a section for initializing any pins or other variables we will be using3) a section that repeats continuously until the power is turned off or a new sketch uploadedWe will be exploring this program structure further in the following example applications.Part 2. Flashing LEDsIn this section, we will continue with the flashing LED program by connecting two more LEDs tothe Arduino and programming different blinking patterns. Note that the Arduino can run on powerprovided to it through the USB cable, so you do not need to connect power to the Arduino board.9. To begin, disconnect the USB cable from the Arduino and wire two LEDs and two 220 resistors to your breadboard with jumper wires connecting to the Arduino as shown in Figure 4below right. One LED is connected to Pin 13 of the Arduino, while the second LED is connectedto Pin 12. Recall that the 220 resistor is required to keep the current from overdriving the LEDand burning it out, and that the long wire of the LED goes on the higher voltage side.Figure 4. Schematic and Breadboard view of Arduino connected to two LEDs.10. When you reconnect the USB to your Arduino, you should notice the left LED on yourbreadboard blinking at the same rate as the yellow LED on the Arduino. You can learn a little5

Lab 14 Electronic Sensors and the Arduino Microcontrollermore about how the program works by reading the following tutorial link: By following the example of the code you are already running, add statements to flash pin 12 thesame way that Pin 13 is flashing. You can create another variable if you like in the initializationsection, such as led2 (declare int led2 12; right below int led 13; -- don’t forget thesemicolon, which acts like a period in C), and set it equal to 12. All else should be pretty easy tomodify and run, just make sure you don’t mess up the blocks created by the curly braces { and }!12. You might have come up with a loop function that does something like this:void loop() {digitalWrite(led, HIGH);delay(1000);digitalWrite(led, LOW);delay(1000);digitalWrite(led2, HIGH);delay(1000);digitalWrite(led2, LOW);delay(1000);}// turn the LED on (HIGH is the voltage level)// wait for a second// turn the LED off by making the voltage LOW// wait for a second// turn the LED2 on// wait for a second// turn the LED2 off// wait for a secondHowever, this doesn’t really work the way you might think. Notice the pattern of the lights. Thefirst LED turns on and off completely, then the second LED turns on and off completely.How can you make it so the two LEDs come on at the same time and turn off at the same time?If you think about it, you only need two delay statements. Statements that happen one after theother without a delay between them will occur practically simultaneously (the Aruino canprocess thousands of statements in one second). So if you remove the first and the third delaystatement, then change the led variables around a little, you’ll get something like this:void loop() {digitalWrite(led, HIGH);digitalWrite(led2, HIGH);delay(1000);digitalWrite(led, LOW);digitalWrite(led2, waitthetheforthetheforLED onLED2 on (at the same time as LED)a secondLED offLED2 off (at the same time as LED)a secondIf you run your program now, you should see the two LEDs are now flashing in sync.13. Modify the delay so the LEDs blink at twice the rate.14. Now modify so the LEDs flash in opposition ( Pin 12 On and Pin 13 off, followed by Pin 12 Offand Pin 13 on, in succession.15. Save your program as Blink2. Then paste a copy of your program into the datasheet.6

Lab 14 Electronic Sensors and the Arduino MicrocontrollerPart 3. Measuring the Position of a PotentiometerThe Arduino has a set of pins that measure analog input voltages ranging from 0 to 5 V, andconverting them to digital numbers ranging from 0 to 1023 using a build in Analog-to-DigitalConverter (ADC). We can use these pins to read the position of a potentiometer. Recall that apotentiometer is a variable resistor. The 5K potentiometer on the breadboard shown below is theblue cube-like component labelled R502 in your lab parts kit.If we connect one end of the “pot” to 0 V, the other end to 5 V, and the center pin to pin A0 of theArduino, the voltage appearing at pin A0 will reflect the rotational position of the potentiometershaft according to the properties of a voltage divider. By reading this voltage with the Arduino, wecan infer the setting of the potentiometer and modify the program to respond to changes in itsposition. More about this step can be found at 16. Disconnect the USB cable and add the additional wiring to incorporate the potentiometer in thesystem. Then plug the USB cable back in.17. Open the demo program by selecting File Examples 01.Basics AnalogReadSerial.18. Run the program by clicking the upload button.19. Open a Serial Monitor window by clicking the magnifying glass icon, at top right20. You should see a steady stream of numbers ranging from 0-1023, correlating to the position ofthe pot. As you turn your potentiometer, these numbers will respond almost instantly.7

Lab 14 Electronic Sensors and the Arduino Microcontroller21. Now for the challenge: modify your Blink2 program to use the value read by the analogReadcommand as the delay for the flashing of your LEDs in the Blink2 program. When you get thisworking, the LED flashing will speed up or slow down as you rotate the potentiometer shaft.Here are some hints to get you started:a. copy the “Serial.begin(9600);” command from the AnalogReadSerial program intothe setup function – inside the block of statements that begin with { and end with } –for the Blink2 program.b. copy the “int sensorValue analogRead(A0);” statement from theAnalogReadSerial program into the loop function – inside the block of statements thatbegin with { and end with } – for the Blink2 program. Put this statement at the top ofthe block, right after the line “void loop() {“c. copy the delay(1); statement into the bottom of the loop( ) block in Blink2d. Last of all, change the number you are using in the delay commands for your LEDblinking to sensorValue.22. Save your program as Blink3 and run it again. You should observe the LED blink rate change asyou rotate the pot.23. When it works, copy your code for Blink3 into the datasheet.Part 4. Sending Audio to a SpeakerIn this experiment, we will connect one wire of our speaker to digital pin 3 of the Arduino, and theother to ground, and send a ton