Analog Read and a Photoresistor

This is where we can add other cool sensors to our Arduino. Remember analogRead is really a voltage comparator.
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.

  • 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.

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)

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.