Photocells Created by lady ada Last updated on 2018-08-22 03:30:13 PM UTC
Guide Contents Guide Contents Overview 2 3 Some Basic Stats Problems you may encounter with multiple sensors Measuring Light 5 What the Heck is Lux? 5 Testing a Photocell Connecting a Photocell Using a Photocell 7 9 11 Analog Voltage Reading Method 11 Arduino Code 13 Simple Demonstration of Use Simple Code for Analog Light Measurements BONUS! Reading Photocells Without Analog Pins CircuitPython Example Projects Buy a Photocell © Adafruit Industries 4 4 13 14 17 21 24 25 https://learn.
Overview Photocells are sensors that allow you to detect light. They are small, inexpensive, low-power, easy to use and don't wear out. For that reason they often appear in toys, gadgets and appliances. They are often referred to as CdS cells (they are made of Cadmium-Sulfide), light-dependent resistors (LDR), and photoresistors. Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how much light is shining onto the squiggly face.
For most light-sentsitive applications like "is it light or dark out", "is there something in front of the sensor (that would block light)", "is there something interrupting a laser beam" (break-beam sensors), or "which of multiple sensors has the most light hitting it", photocells can be a good choice! Some Basic Stats These stats are for the photocell in the Adafruit shop which is very much like the PDV-P8001 (https://adafru.it/clX) .
Measuring Light As we've said, a photocell's resistance changes as the face is exposed to more light. When its dark, the sensor looks like an large resistor up to 10MΩ, as the light level increases, the resistance goes down. This graph indicates approximately the resistance of the sensor at different light levels.
Most datasheets use lux (https://adafru.it/aKS) to indicate the resistance at certain light levels. But what is lux (https://adafru.it/aKS) ? Its not a method we tend to use to describe brightness so its tough to gauge. Here is a table adapted from a Wikipedia article on the topic! (https://adafru.it/aKS) Illuminance Example 0.002 lux Moonless clear night sky 0.2 lux Design minimum for emergency lighting (AS2293). 0.27 - 1 lux Full moon on a clear night 3.
Testing a Photocell The easiest way to determine how your photocell works is to connect a multimeter in resistance-measurement mode (https://adafru.it/aZZ) to the two leads and see how the resistance changes when shading the sensor with your hand, turning off lights, etc. Because the resistance changes a lot, an auto-ranging meter works well here. Otherwise, just make sure you try different ranges, between 1MΩ and 1KΩ before 'giving up'. © Adafruit Industries https://learn.adafruit.
© Adafruit Industries https://learn.adafruit.
Connecting a Photocell Because photocells are basically resistors, they are non-polarized. That means you can connect them up 'either way' and they'll work just fine! Photocells are pretty hardy, you can easily solder to them, clip the leads, plug them into breadboards, use alligator clips, etc. The only care you should take is to avoid bending the leads right at the epoxied sensor, as they could break off if flexed too often. © Adafruit Industries https://learn.adafruit.
© Adafruit Industries https://learn.adafruit.
Using a Photocell Analog Voltage Reading Method The easiest way to measure a resistive sensor is to connect one end to Power and the other to a pull-down resistor to ground. Then the point between the fixed pulldown resistor and the variable photocell resistor is connected to the analog input of a microcontroller such as an Arduino (shown) For this example I'm showing it with a 5V supply but note that you can use this with a 3.3v supply just as easily.
Ambient light like… Ambient light (lux) Photocell resistance (Ω) LDR + R Current thru Voltage (Ω) LDR +R across R Dim hallway 0.1 lux 600KΩ 610 KΩ 0.008 mA 0.1 V Moonlit night 1 lux 70 KΩ 80 KΩ 0.07 mA 0.6 V Dark room 10 lux 10 KΩ 20 KΩ 0.25 mA 2.5 V Dark overcast day / Bright room 100 lux 1.5 KΩ 11.5 KΩ 0.43 mA 4.3 V Overcast day 1000 lux 300 Ω 10.03 KΩ 5V 0.
Arduino Code Simple Demonstration of Use This sketch will take the analog voltage reading and use that to determine how bright the red LED is. The darker it is, the brighter the LED will be! Remember that the LED has to be connected to a PWM pin for this to work, I use pin 11 in this example. These examples assume you know some basic Arduino programming. If you don't, maybe spend some time reviewing the basics at the Arduino tutorial? (https://adafru.it/aKU) © Adafruit Industries https://learn.adafruit.
/* Photocell simple testing sketch. Connect one end of the photocell to 5V, the other end to Analog 0. Then connect one end of a 10K resistor from Analog 0 to ground Connect LED from pin 11 through a resistor to ground For more information see http://learn.adafruit.com/photocells */ int photocellPin = 0; int photocellReading; int LEDpin = 11; int LEDbrightness; void setup(void) { // We'll send debugging Serial.
© Adafruit Industries https://learn.adafruit.
/* Photocell simple testing sketch. Connect one end of the photocell to 5V, the other end to Analog 0. Then connect one end of a 10K resistor from Analog 0 to ground For more information see http://learn.adafruit.com/photocells */ int photocellPin = 0; int photocellReading; // the cell and 10K pulldown are connected to a0 // the analog reading from the analog resistor divider void setup(void) { // We'll send debugging information via the Serial monitor Serial.
BONUS! Reading Photocells Without Analog Pins Because photocells are basically resistors, its possible to use them even if you don't have any analog pins on your microcontroller (or if say you want to connect more than you have analog input pins). The way we do this is by taking advantage of a basic electronic property of resistors and capacitors.
In this case, our 'bucket' is a 0.1uF ceramic capacitor. You can change the capacitor nearly any way you want but the timing values will also change. 0.1uF seems to be an OK place to start for these photocells. If you want to measure brighter ranges, use a 1uF capacitor. If you want to measure darker ranges, go down to 0.01uF. /* Photocell simple testing sketch. Connect one end of photocell to power, the other end to pin 2. Then connect one end of a 0.
void loop(void) { // read the resistor using the RCtime technique photocellReading = RCtime(photocellPin); if (photocellReading == 30000) { // if we got 30000 that means we 'timed out' Serial.println("Nothing connected!"); } else { Serial.print("RCtime reading = "); Serial.
© Adafruit Industries https://learn.adafruit.
CircuitPython It's easy to read how much light a photocell sees with CircuitPython and its built-in analog input support (https://adafru.it/CbI). By wiring the photocell to an analog input of your board you can read the voltage from it and see how it changes as the amount of light hitting the sensor changes too. First wire up a photocell to your board as shown on the previous page for Arduino.
photocell = analogio.AnalogIn(board.A1) At this point you can read the value property to get a reading of the light seen by the photocell. Try it: photocell.value Try covering the photocell with your hand to block the light it can see and read the value again: photocell.value Notice the value changed! When the sensor sees less light the value is reduced. The more light the sensor sees, the higher the value.
Here's a complete program that reads the photocell value and prints both the value and voltage every second. Save this as main.py on your board and open the serial output to see the printed values. Try shining light on the sensor or covering it up to see how the value and voltage change! import time import board import analogio # Initialize analog input connected to photocell. photocell = analogio.AnalogIn(board.A1) # Make a function to convert from analog value to voltage.
Example Projects Noisemaker that changes frequency based on light level. (https://adafru.it/aKV) Motor value and directional control with photoresistors and microcontroller Line-following robot that uses photocells to detect the light bouncing off of white/black stripes Another robot, this one has two sensors and moves towards light (https://adafru.it/aKW) (they're called Braitenberg vehicles) Using a photocell and pocket laser pointer to create a breakbeam sensor (https://adafru.
Buy a Photocell Buy a Photocell (http://adafru.