photoresistor module reverse

I have photo resistor module KY-018 ... but ..

when the light is strong it reads small value .. and in dark reads highest value.

I want to reserve it because the value is important for me ..

Thanks

Subtract the actual value from 1023

kenwood120s:
Subtract the actual value from 1023

Sorry I don't understand you ,,

My code :

int pot = A1;
int potvalue;
int led = 9;
int ledstate;
int phs = A0;
int phsvalue;

void setup()

{

  pinMode(led, OUTPUT);
  Serial.begin(9600);

}

void loop()

{
  potvalue = analogRead(pot);
  ledstate = potvalue/4;
  analogWrite(led,ledstate);
  phsvalue = analogRead(phs);
  Serial.println(phsvalue);
  
}

Well an analog value can go from 0 to 1023.

You want to reverse your large and small values, ie make your 0's to be 1023's, and your 1023's to be 0's.

So if it's 0, subtract it from 1023 and you get 1023.
And if it's 1023, subtract it from 1023 and it's 0.

So change this:

potvalue = analogRead(pot);

To this:

potvalue = 1023 - analogRead(pot);

Thaaaaaanks a lot ..:slight_smile:

To read a photoresistor (a.k.a Light Dependent Resistor) you would use it and a fixed resistor to form a voltage divider. One of the two resistors is connected between the analog pin and +5V and the other is connected between the analog pin and Ground.

As more light hits the photoresistor the resistance goes down. To make More Light produce More Voltage you would connect the fixed resistor to Ground and the photoresistor to +5V. Sounds like you have them switched.