Photocells and Photoresistors

Today were going to go over detecting light using and photoresistor and how we can use this information to control something (for this example: LEDS)

 

The basic idea here is that we can wire the photoresistor in some way so that it creates a voltage divider.  We can then read resultant voltage with our development board.

Components:

1X Arduino Compatible Board

1X Jumper Wire Set

2X 220Ω Resistors

2X LEDs

1X 10kΩ Resistor

1x Photoresistor

photoresistor

Connections:

PWM LED: long leg digital- pin 9; short leg 220Ω resistor then to ground

On/off LED: long leg digital- pin 2; short leg 220Ω resistor then to ground

 

And finally you want to 1 end of the photoresistor to one leg of the 10kΩ resistor.  Now the other leg of the photoresistor connects to power, the other leg of the resistor connects to ground, and the center leg where they are tied together connects to A5.

 

Code:

We will be ultilizing very basic functions such as analog read, analog write (PWM), and digital write.

 

 

/*Simple program to show how to use a photo resistor to detect light
 * intesity.  This information can then be used to control an object.
 * In This case 2 LEDS.  1) as an on/off and 2) as a PWM dimming LED
 */

int photoInput = A0; //declares pin as an analog pin 
float photoValue;    //variable used to store analog value 
float photoVoltage;  //analog signal converted into a voltage 
int pwmLED = 6;      //declares digital pin for PWM LED
int onOffLED = 2;    //declares digital pin for on off LED
float lightLimit = 2.5; //threshold to turn on/off LED


void setup() {
   Serial.begin(9600);
   pinMode(photoInput,INPUT);
   pinMode(pwmLED,OUTPUT);
   pinMode(onOffLED,OUTPUT);
}

void loop() {
  photoValue=analogRead(photoInput);
  photoVoltage=photoValue*5/1023;
  Serial.print("DAC: ");
  Serial.print(photoValue);
  Serial.print("     Voltage: ");
  Serial.println(photoVoltage);

  analogWrite(pwmLED,photoValue/1023*255);

  if(photoVoltage>lightLimit){
    digitalWrite(onOffLED,HIGH);}
    else{
      digitalWrite(onOffLED,LOW);
    }
  

  delay(100);

}

corra02

Leave a Reply

Your email address will not be published. Required fields are marked *