LDR giving only zeros

so first here is the code I used to test the LDR


const int ldrpin = A2;
 int ldrstat = analogRead(ldrpin);

void setup(){
pinMode(ldrpin, INPUT);
Serial.begin(9600);
}

void loop() { 
 Serial.println(ldrstat);     
}

so this code is spouse to give me the light value form the ldr but all I get is just 0 no matter if there is flashlight in front of it or my hand completly blocking everything.. I think it's the resistor fault because in tinckercad everything runs smoothly but not IRL.

What happens when the A2 pin is wired to 5V?

I do not think the pinmode setting is required to read analog signals.

Show a schematic of your thingy, show an image of your thingy all connected and not working.

It looks to me that you do not have a power source for the LDR. Try changing "INPUT" in the pinMode command to "INPUT_PULLUP."

forgot to mention that the LDR is connected to 5v

You have two problems. 1. is that you need a pull down resistor for the LDR to pass any current and develop a voltage drop. 2. Using A2 as an int causes the compiler to assign A2 as a variable not a pin. When the LDR is connected between +5 and A2, it is a pull up resistor and the ADC should show 1023 but, it shows 0, so you are not reading the input on pin A2.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.