Presentation is loading. Please wait.

Presentation is loading. Please wait.

ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland.

Similar presentations


Presentation on theme: "ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland."— Presentation transcript:

1 ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland State University http://web.cecs.pdx.edu/~me120

2 ME 120: Photoresistors and Arduino Programming Overview What is a photoresistor? How do we measure a photoresistor’s output? Basic Arduino circuit for reading and reporting Using “if” statements to respond to analog input readings See on-line reference: ❖ http://arduino.cc/en/Reference/HomePage http://arduino.cc/en/Reference/HomePage 2

3 ME 120: Photoresistors and Arduino Programming What is a photoresistor? 3

4 ME 120: Photoresistors and Arduino Programming A photoresistor is a semiconductor A photoresistor is a two-terminal semiconductor device that has an electrical resistance that depends on the light incident on the exposed semiconductor surface. The resistance decreases with increases in incident. 4 Incident light level Electrical resistance

5 ME 120: Photoresistors and Arduino Programming More information is available via the datasheet 1.Visit sparkfun.com 2.Enter “photoresistor” in the search box 3.Locate product #9088 or its more recent replacement 4.Click on the datasheet link 5.Note that there are many vendors 5

6 ME 120: Photoresistors and Arduino Programming Voltage divider circuit for photoresistor 6 Why is the fixed resistor on the bottom of the voltage divider?

7 ME 120: Photoresistors and Arduino Programming Basic Arduino code to read and report photoresistor output 7

8 ME 120: Photoresistors and Arduino Programming Display voltage divider output on the serial monitor Connect the voltage divider output to analog pin 0 8 See http://arduino.cc/en/Reference/AnalogRead http://arduino.cc/en/Reference/ void setup() { Serial.begin(9600); // Initialize serial port object } void loop() { int reading; float voltage; reading = analogRead(A0); // Read analog input channel 0 voltage = reading*(5.0/1023.0); // and convert to voltage Serial.print(reading); // Print the raw reading Serial.print(” ”); // Make a horizontal space Serial.println(voltage); // Print voltage value }

9 ME 120: Photoresistors and Arduino Programming Use an “if” statement to respond to analog input readings 9

10 ME 120: Photoresistors and Arduino Programming Logical statements to control code execution “if” construct to make a single choice “if – else” construct to choose either/or 10 if ( something is true ) { code block } if ( something is true ) { code block 1 } else { code block 2 } See http://arduino.cc/en/Reference/If http://arduino.cc/en/Reference/Else

11 ME 120: Photoresistors and Arduino Programming Syntax of “if” construct “if” construct to make a single choice “something is true” must be a logical expression Examples ❖ if ( x<0 ) ❖ if ( y>=z ) 11 if ( something is true ) { code block } SymbolMeaning <Is less than >Is greater than ==Is equal to >=Is greater than or equal to <=Is less than or equal to !=Is not equal to

12 ME 120: Photoresistors and Arduino Programming Test your understanding What is the value of z? 12 x = 2; y = 5; if ( x < y ) { z = y – x; } x = 2; y = 5; if ( x > y ) { z = y – x; } x = 2; y = 5; if ( (y-x)<= 3 ) { z = y/x; } x = 2; y = 5; if ( (y-x)<= 3 ) { z = y/x; } else { z = 0.0; } a. b. c. d.

13 ME 120: Photoresistors and Arduino Programming Compound “if-else-if” Implement a piecewise function Notice the error-handling for x x4 13 if ( x<x1 ) { Serial.println(“Error: x<x1”); } else if ( x<x2 ) { y = ya; } else if ( x<x3 ) { y = yb; } else if ( x<x4 ) { y = yc; } else { Serial.println(“Error: x>x4”); }

14 ME 120: Photoresistors and Arduino Programming Output dependent on photoresistor reading 14 void setup() { Serial.begin(9600); // Initialize serial port object } void loop() { int reading; float voltage; reading = analogRead(A0); // Read analog input channel 0 voltage = reading*(5.0/1023.0); // and convert to voltage if ( voltage < 2.5 ) { Serial.println(”Getting dark”); // Print the raw reading }

15 ME 120: Photoresistors and Arduino Programming Output dependent on photoresistor reading Next step ❖ Add a second test at 1.75 V (or some value). Print a different message for very low analog input (low ambient light values) Study questions ❖ What are minimum and maximum voltage levels for photoresistor outputs? ❖ Will the test for darkness work without converting to voltage first? 15


Download ppt "ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland."

Similar presentations


Ads by Google