0
votes

I have been working on a project with Arduino and have come across something that I find fascinating/confusing. So, I had to test something before constructing this project. I built a simple circuit that consists of just an LED and photoresistor. What I had to test was whether the photoresistor was capable of determining the brightness of an LED that was being dimmed through PWM. My initial expectation was that this would not work (the photoresistor would either read 1023 or 0 because PWM is achieved digitally). To my surprise, the photoresistor was able to accurately read the brightness of the LED (accurately to an extent -- this is simply based off of comparing the apparent brightness of the PWM LED with an LED placed in series with a certain resistor)! This is exactly what I wanted, but I am just curious as to why this works. I am not sure if my original doubt was due to a misunderstanding of photoresistors or PWM. Any help would be much appreciated. Thank you!

Here is the code I am running (I am not using the analogWrite() function because the project I am working on requires me to have a certain level of control over the PWM):

const int LED_PIN = 9;
const int PHOTO_PIN = 0;

//These values have been altered and tested
const int HIGH_TIME = 250;
const int LOW_TIME = 2750;

void setup()
{
  pinMode(LED_PIN, OUTPUT);
  pinMode(PHOTO_PIN, INPUT);

  Serial.begin(9600);
}

void loop()
{
 digitalWrite(LED_PIN, HIGH);
 delayMicroseconds(HIGH_TIME);
 digitalWrite(LED_PIN, LOW);
 delayMicroseconds(LOW_TIME);

 Serial.println(analogRead(PHOTO_PIN));
}
2

2 Answers

0
votes

A "photoresistor" is a variable resistor. That is the simplest way to say it.

Just imagine your potentiometer, you can control its resistance by turning the little knob and then analogRead it. The photoresistor on the other side, changes it resistance depending on the light intensity. Because of that, the resistance will go up and down depending on your LED.

For "HOW" it actually works, see here.

Now, there are a couple of factors to consider:

1 - The ambient light of your room.

2 - The distance between your LED

So hope I helped you learn a little more about photoresistors!

0
votes

The response time of the photo resistor is much slower than the PWM frequencies you are using. So it averages the on and off times of the LED and gives a resistance proportional to the average light. If you were using a photodiode with a fast response time, it would be able to "see" the LED go on and off.

I suggest that you don't try to write to the Serial port every time through the loop since it will quickly fall behind at 9600 baud. Perhaps write every 500 times through the loop.