Arduino with Photoresistor sensor

A photoresistor or light-dependent resistor (LDR) or photocell is a light-controlled variable resistor.

The photoresistor is not to differentiate Anode and Cathode.
symbol of photoresistor


How do I attach an photoresistor to the board? The following schematic shows the basic circuit.
If you pick the resistors correctly, you can make the output of this circuit vary between a little over zero and a little under five volts for your range of light intensity. To pick the resistor, you need to realize that this circuit is just a generalization of the basic building block of analog electronics, the voltage divider.







int photoRsPin = 0;
int photoRsVal = 0;
int minValLight = 100;
int LEDPin = 13;
int LEDState = 0;

void setup(){
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  photoRsVal = analogRead(photoRsPin);
  Serial.println(photoRsVal);

  if (photoRsVal < minValLight && LEDState == 0){
    digitalWrite(LEDPin, HIGH);
    LEDState = 1;
  }

  if (photoRsVal > minValLight && LEDState == 1){
    digitalWrite(LEDPin, LOW);
    LEDState = 0;
  }

  delay(100);
}



Comments

Popular Posts