-1

The CDS sensor module connected to the Arduino Nano returns only a value of 1023. my code hear

int Cds = A0; 
//int Led = 13;
int value;
 
void setup() {`enter code here`
    Serial.begin(9600);
    pinMode(Cds,INPUT);
    //pinMode(Led,OUTPUT);
}
 
void loop() {
    Cds=analogRead(A0); 
  
    Serial.println(Cds); 
    if(Cds<300) 
      Serial.println("dark");
    else
      Serial.println("bright"); 
      
    delay(1000);  
}

and nano is connected breadboard

jeongwoon
  • 1
  • 1
  • Looks ok. Check with voltmeter what is real voltage at pin `A0`. Possible reason: voltage is higher than Arduino can measure. Also measure which is voltage between Gnd and +5v – Ihor Drachuk Jul 26 '20 at 12:51
  • 1
    Also better change `pinMode(Cds,INPUT);` to `pinMode(A0,INPUT);` because later you use `Cds` not as pin, but as it's value – Ihor Drachuk Jul 26 '20 at 12:52
  • 1
    There is no problem with the code Is there a problem with the resistance or voltage? – jeongwoon Jul 26 '20 at 12:54
  • Yes, it could be the reason: 1) check voltage between Gnd and +5v. 2) check voltage between Gnd and A0. – Ihor Drachuk Jul 26 '20 at 12:56
  • Thank you bro i will try – jeongwoon Jul 26 '20 at 13:06
  • pins used in `analogRead` don't need `pinMode`. But your problem is rather the circuit, I fear. – datafiddler Jul 26 '20 at 17:42
  • How do you connect the CDS to A0? In order for an accurate measurement, ADC input on Arduino (ATmega328p to be more specific) requires the load to be connected to the ADC input has an impedance of around 10k ohm. A CDS sensor could have a resistance as high as several mega ohm when in dark (which can't be read properly by the ADC), and as low as hundred of ohm (which is okay). To overcome this problem, you need a voltage divider with a 10k with CDS, one side of CDS connect to 5v, the other side connect to 10k and as the input to A0, the other side of the 10k connect to GND. – hcheung Jul 28 '20 at 00:49

2 Answers2

0

Check analogReference() in the code for your board to configures the reference voltage used for analog input. If you have have correct reference voltage setup you get 1023 max value all the time.

0

Firstly, the first line in loop() should be changed to:

value = analogRead(Cds)

And from there, just use value for accessing the brightness, because Cds is just your alias for A0

The second possible Error source could be this:
If you just connect the module to GPIO and GND, nothing will happen, and it will always read the maximum value (1023).
You need to use it like a voltage divider, to read values which are relative to a certain voltage

Circuit diagram

If you connect everything like this, it should work.

Karrer
  • 44
  • 5