EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE LABORATORY 7: IR SENSORS AND DISTANCE DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING UNIVERSITY OF NEVADA, LAS VEGAS GOAL: This section will introduce the Sharp IR Distance Sensor and interface it with the Arduino UNO and OBJECTIVES: Learn the basics of Arduino Programming o Commands: setup() loop() Serial Library DistanceGP2Y0A21YK Library Interface with the Sharp Distance Sensor Generate a calculated distance Furthur expand and create an event based on the returned value OVERVIEW AND REQUIREMENTS: Infrared Proximity Sensor - Sharp GP2Y0A21YK 1 Output 2 Ground 3 VCC (5V) Fig. 1. Sensor The Sharp GP2Y0A21YK is an Infrared proximity Sensor. It shines a beam of IR light from an LED, and measures the intensity of light that is bounced pack using a phototransistor. This IR sensor is more economical than sonar rangefinders, yet it provides much better performance than other IR alternatives. Interfacing to most microcontrollers is straightforward: the single analog DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 1
output can be connected to an analog-to-digital converter for taking distance measurements, or the output can be connected to a comparator for threshold detection. The detection range of this version is approximately 10 cm to 80 cm (4" to 32"); a plot of distance versus output voltage is shown below. Fig. 2. Voltage and distance relation As you can see the output from the sensor is not linear. In order to compensate for this we are using a library that contains a Look-up Table (LUT) that has stored distance values relative to the analog output voltage from the IR sensor. How the Sensor Works 1. A pulse of IR light is emitted by the emitter. 2. This light travels out in the field of view and hits an object. 3. The reflected light returns to the detector and creates a triangle between the point of reflection, the emitter, and the detector. 4. The angles in this triangle vary based on the distance to the object. 5. The receiver uses a precision lens to transmit the reflected light onto various portions of the enclosed linear CCD array based on the angle of the triangle described above. 6. The CCD array can then determine what angle the reflected light came back at and therefore, it can calculate the distance to the object. DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 2
COMPONENTS: Arduino Uno USB A-B Cable Sharp GP2Y0A21YK IR Proximity Sensor Host PC Installed Arduino Uno drivers and IDE DIAGRAM: DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 3
OPERATION: Using the distance sensor 1. Download and extract the DistanceGP2Y0A21YK.zip file included in this lab. https://faculty.unlv.edu/eelabs/soft/distancegp2y0a21yk.zip 2. Copy the extracted DistanceGP2Y0A21YK into the Arduino libraries folder. 3. If you already have the Arduino client running, make sure to restart the client after copying the Distance folder into the libraries folder (i.e. extract the zip into libraries folder). The client will not recognize that the folder is there if you do not. 4. Wire the circuit as shown in the schematic. Your final result should be something similar to the following snapshot. 5. Attach the Arduino UNO to the host PC with the use of the USB cable. DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 4
6. Open the Arduino IDE and create a new sketch titled IRSensor. Verify that the correct COM port is in use. 7. Upload the following sketch to your Arduino UNO (Code 1): #include <DistanceGP2Y0A21YK.h> //Load Distance Library DistanceGP2Y0A21YK Dist; int distance; void setup() { Serial.begin(9600); Dist.begin(A0); } //Define the struct //Variable to hold distance //Start Serial //Start Distance Lib void loop() { distance = Dist.getDistanceCentimeter(); //Use LUT to get Distance Serial.print("\nDistance in centimeters: "); //Print Serial.print(distance); delay(100); } 8. Verify your results by running the COM window and testing the IR Sensor. If your sketch is not compiling please add the following line at the top of DistanceGP2Y0A21YK_LUTs.h library file (it can be found in the folder DistanceGP2Y0A21YK): typedef const unsigned char prog_uchar; DEMO AND SCREENSHOTS: DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 5
PRELAB: What does analogread() returns? How to convert this value to a voltage (0 5V)? EXPERIMENTS: Experiment 1 1. Wire the circuit and sensor as mentioned in Operation section 2. Verify the operation of the sensor 3. Demonstrate the results to the TA Experiment 2 1. Extend the circuit: a. Using the breadboard shield, add an LED to your design b. Modify the code so that when the IR Sensor detects a distance less than 10 centimeters the LED turns on. 2. Demonstrate the results to the TA Experiment 3 1. Extend the circuit from Experiment 2: a. Using the breadboard shield, add one more LED, so you have green LED and red LED. b. Keep in mind, that the measurements below 10cm and above 80 cm are erroneous. However for the purpose of this experiment assume 40 cm as the higher bound (i.e. you are assuming that only measurements between 10 cm and 40 cm are correct). Modify the code so green LED is on when measurements are correct, and red LED turns on when measurements are incorrect (outside of assumed range). c. Display the measurements in the serial monitor d. Demonstrate the operation to the TA Experiment 4 1. By plotting the output voltage versus inverse number distance (1/cm), we can get the linear relation such as in Fig. 3. 2. Using the equation y = 20.99x + 0.19, where y is voltage output and x is inverted distance, modify Code 1 to find the distance without the use of the included library. 3. Make 5 measurements for the library and your method (same object distance for the pair of measurements). Place them in the table: No. 1. 2. 3. 4. 5. Library measurement A Your method measurement B A B DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 6
What are your conclusions? Fig. 3. Output characteristics with inverse number of distance POSTLAB REPORT DELIVERIES Include the following elements in your postlab report: 1. Theory of operation a. Describe how the distance sensor works b. List 3 different applications of the distance sensors 2. Results of the experiments For each experiment, include: a. The code that you developed for the experiment. Each line that was added must be highlighted and commented with the explanation of what is its meaning. b. Brief explanation how the goal of the experiment was reached c. Screenshots of the serial monitor with the values, presenting the operation of your code For experiment 4, explain your method. Write your conclusions about the differences between library and your results. 3. Answer the questions: a. Is the output of the IR sensor analog or digital? b. Find out the technical data about the sensor used: i. Detecting distance DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 7
ii. Operating temperature iii. Operating supply voltage 4. Conclusions a. Write down your conclusions, things learned, problems encountered during the lab and how they were solved, etc. DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING 8