Analog Read and a Photoresistor

This is where we can add other cool sensors to our Arduino. Remember analogRead is really a voltage comparator.

Again, this means that we will actually be comparing two resistors to each other and will be able to determine exactly how different they are. This is not so interesting if we have two fixed resistors, but becomes cool when we have one fixed and one variable resistor. So far, the only variable resistors we've used are potentiometers. Now we're going to introduce photoresistors and thermistors.Photresistors change value depending on light level, while thermistors change based on temperature. Both can be used in the same way. The circuit to the right shows the circuit diagram you could use with either of these. You'll notice that there is a variable resistor and a fixed resistor both hooked up to your analog pin. For a fixed resistor try 10k ohms.The circuit below is the same one we used for the potentiometer circuit. Again, we need an LED in digital pin 13. Set up the Arduino with the circuit and the program and answer the questions below.

int potPin = 2; // select the input pin for the potentiometer

int ledPin = 13; // select the pin for the LED

int val = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT

}

void loop() {

val = analogRead(potPin); // read the value from the sensor

digitalWrite(ledPin, HIGH); // turn the ledPin on

delay(val); // stop the program for some time

digitalWrite(ledPin, LOW); // turn the ledPin off

delay(val); // stop the program for some time

}

    • Does it work the same as the potentiomemter?

    • Change the 10k ohm resistor for a 1-2k ohm resostor. Do you notice any differences?

What you should notice is that neither the 10k nore the 1k resistors will give you the same range of flash rates as the potentiomemter. How can we make it? Leave the resistors alone and try the following program. Once you send the program over, click on the "Serial Monitor" button and watch for your data.

int potPin = 2; // select the input pin for the potentiometer

int ledPin = 13; // select the pin for the LED

int val = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT

Serial.begin (9600); // tells the Arduino to get ready to send data back to the computer

}

void loop() {

val = analogRead(potPin); // read the value from the sensor

Serial.print ("Value = "); // report the text "Value = "

Serial.println (val); // report val, the number from the analogRead.

// The ln at the end of Serial.print means go onto the next line

digitalWrite(ledPin, HIGH); // turn the ledPin on

delay(val); // stop the program for some time

digitalWrite(ledPin, LOW); // turn the ledPin off

delay(val); // stop the program for some time

}

This will then adjust the flash rate to precisely match the light level. Next change this to fade an LED based on brightness.Note, all of this could be done with a thermistor. So that rather than light level, temperature controls things. Picture a fan that kicks on if temp gets to high. Or maybe you set up the tri color LED to be red when it's hot and blue when it's cold. It won't be the first time something like this has been done.

Now play with your circuit. Find the maximum and minimum values. Write these down. Use these as the minimum and maximum values for a map command (see the potentiometer lesson). Something like: map (val, min, max, 0, 1023)