Photoresistor Value is Constant and Not Changing

I’m trying to get a readout of the photoresistor resistance, however the value is outputted at a constant and does not appear to change (stays at around 75,000). The value of the Lux however appears correct. Below is a photo and my code.

const int ledPin = 9;
#define photoPin A2
 
int raw = 0;
int Vin = 3.3;
float Vout = 0;
float R1 = 220; // known
float R2 = 0; // photoresistor reading
float buffer = 0;
 
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}
 
void loop()
{
  // Turn the LED on slowly
  for (int a=0; a<=255;a++)
  {
    analogWrite(ledPin, a);
    raw = analogRead(photoPin);
    if (raw) {
      buffer = raw * Vin;
      Vout = (buffer)/4095.0;
      buffer = (Vin/Vout) - 1;
      R2 = R1 * buffer;
      Serial.print("Vout: ");
      Serial.println(Vout);
      Serial.print("Lux: ");
      Serial.println(a*0.1243);
      Serial.print("R2: ");
      Serial.println(R2);
    }
    delay(30);
  }

Hi, welcome to the forum.
Sorry for not answering your question yet.

Breadboards have sometimes bad contacts and jumper wires can be broken. Can you check everything with a multimeter ?

Can you edit your post and put the sketch between lines with three backslash-single-quotes ?

```
Your sketch
```

The analogRead() return a value from the ADC, you should not use it in the if-statement as a condition.

if (raw) {     // <---  bug

When you see Vout on the serial monitor, you can verify that voltage with a multimeter.

Can you print more decimals, and show us the result ? Use the second parameter, for example ,8 when printing a float value.

Serial.println(Vout, 8);

Using a LDR to measure the LUX is not accurate.

Did you know that you can't use a LDR in direct sunlight ? When it gets too hot, then it is permanently damaged.

My Vout also stays constant at 0.2. The exact readings of R2 are 75,238. I am unsure why this is happening since my peers have the same setup as I do, however they are using an Arduino Uno. That is why I have 4095.0 in my code for Vout. Do you think this is a photoresistor issue?

I think there is 50% chance it is a wiring, broken wires, glue on the legs of the components or a breadboard with bad contacts.
Do you have a multimeter ? Can you measure that indeed 0.2V is going to A2 ?
If you don't have a multimeter then use other jumper wires and an other location on the breadboard.

A have not heard of a broken photoresistor (LDR) yet. Can you try a different LDR ?

What happens if you connect GND to A2 and then 3.3V to A2 ?
Can you replace the LDR with a few resistors to check if the printed values are changing ?

We prefer that you copy the output of the Serial Monitor to show it to us. Now you sometimes use a comma and sometimes a dot in a floating point number :thinking:
The language is "English" but this is international English. I always have to try really hard to not make fun of people who use nose-length or feet for length and a bottle of milk for volume :wink:

Hi,
Welcome to the forum.
Can you please try this bit of code and see what the IDE monitor shows?

const int ledPin = 9;
#define photoPin A2

int raw = 0;
int Vin = 3.3;
float Vout = 0;
float R1 = 220; // known
float R2 = 0; // photoresistor reading
float buffer = 0;

void setup()
{
  Serial.begin(9600);
  analogReadResolution(12);
}

void loop()
{
  raw = analogRead(photoPin);
  Serial.print(" raw input data =");
  Serial.println(raw);
  delay(250);
}

Do you have a DMM?

Thanks.. Tom.... :grinning: :+1: :coffee: :australia:

int Vin = 3.3;

An int variable cannot hold 3.3. it will be truncated to 3.

However, this value does not, or should not, matter in the calculation. If your formulae were correct, the value of Vin would "cancel out" at some point in the calculation. But I can see that this won't happen, so the formulae must be incorrect.

The equation you need to use its simply this:

raw / 4095 = Rfixed / (Rfixed+Rldr)

You need to solve the equation for Rldr and use that formula in your code.

Hello
What value has the resitor in conjunction with the LDR?

This circuit shows a common simplistic misunderstanding. The LDR is conencted wbtween piin and Vcc so as it conducts more, it pulls up to 5 V - against the pull-down resistor whcih is some "220" value I cannot quite discern because these blue 1% metal film resistors are hard to read even in good light, let alone a photo.

While this arrangement seems "logical" it is generally bad design. Particularly where the sensor - the LDR in this case - is to be mounted at the end of a cable, it is much more sensible to have it between the pin and ground and with a pull-up resistor to 5 V adjacent to the Arduino (or other microcontroller).

Only the ground need be taken out to external components, thus avoiding any danger of the 5 V supply being accidentally shorted out - or picking up or radiating interference.

The inverted sense of the readings made of the sensor is dealt with in the coding and involves no additional complexity.

220 Ohms

raw input data =19
raw input data =19
raw input data =18
raw input data =18
raw input data =19
raw input data =17
raw input data =16
raw input data =15
raw input data =17
raw input data =16
raw input data =16
raw input data =16
raw input data =16
raw input data =16
raw input data =16

This is what the serial monitor displays after a few iterations with the code you have provided. I do not have a DMM.

Specifically which part of the code I provided should I remove and add this line. This is one of my first times working with Arduino, I apologize.

Here is what the serial monitor outputs. I have ordered new LDR'S today.I have tried switching different spots on the board to no avail.

Lux: 0.00
R2: 900680.00
Vout: 0.00
Lux: 0.12
R2: 900680.00
Vout: 0.00
Lux: 0.25
R2: 900680.00
Vout: 0.00
Lux: 0.37
R2: 900680.00
Vout: 0.00
Lux: 0.50
R2: 900680.00
Vout: 0.00
Lux: 0.62
R2: 900680.00
Vout: 0.00
Lux: 0.75
R2: 900680.00
Vout: 0.00
Lux: 0.87
R2: 900680.00
Vout: 0.00
Lux: 0.99
R2: 900680.00
Vout: 0.00
Lux: 1.12
R2: 900680.00
Vout: 0.00
Lux: 1.24
R2: 900680.00
Vout: 0.00
Lux: 1.37
R2: 900680.00
Vout: 0.00
Lux: 1.49
R2: 900680.00
Vout: 0.00
Lux: 1.62
R2: 900680.00
Vout: 0.00
Lux: 1.74
R2: 900680.00
Vout: 0.00
Lux: 1.86
R2: 900680.00
Vout: 0.00
Lux: 1.99
R2: 900680.00
Vout: 0.00
Lux: 2.11
R2: 900680.00
Vout: 0.00
Lux: 2.24
R2: 450230.00
Vout: 0.00
Lux: 2.36
R2: 450230.00
Vout: 0.00
Lux: 2.49
R2: 450230.00
Vout: 0.00
Lux: 2.61
R2: 450230.00
Vout: 0.00
Lux: 2.73
R2: 450230.00
Vout: 0.00
Lux: 2.86
R2: 450230.00
Vout: 0.00
Lux: 2.98
R2: 450230.00

If the resistor in series with the LDR is 220 R then that will not help. As a starter measure the LDR resistance with the light on and set the series resistor to around this value .

Hi,
With my code running, and no LDR circuit.
Connect A2 to gnd, then connect A2 to 5V pins.
See what the monitor shows.

Tom.. :grinning: :+1: :coffee: :australia:

I am not entirely sure how to construct this circuit and a bit confused on the A2 pin. Could you walk me through this?

Hi,
The A2 pin is on your controller, it is the analog input that is measuring the voltage at the junction of the LDR and its series resistor.

  1. Unplug the lead to A2.
  2. Plug a lead into A2 with its other end connected to GND.
  3. Run my code and read what the monitor shows.
  4. Move the end of the lead from GND to 5V.
  5. Run my code and read what the monitor shows.

Tom... :grinning: :+1: :coffee: :australia:

Doing this resulted in:

  1. Raw output data = 0
    Then,
  2. Raw output data = 4095

Hi,
Good, it means your analogRead is working in code.

Tom.. :grinning: :+1: :coffee: :australia:

Would you assume my problem is instead affiliated with the code and not the circuit? I am still receiving a constant photoresistor resistance value. I am not sure what can be wrong with the circuit I constructed above.

I am trying to plot how the lux vs resistance changes with different resistor values. I removed the resistor and the photoresistor measures at a constant value of 621. I am not understanding why this value is constant, it should not be as the LED gets brighter by the for loop.