LDR / Photo Resistor help

Greetings Particle Community.
I am newbie here & getting started with Photon & LDR. I’m aiming at a prototype to daylight control an LED (connected to D0) using an LDR (connected to A0) i.e. while its bright sunlight the LED won’t power on, whereas during dark the LED would.

The issue is with this following code I can’t make it work. The LED glows in daylight & even in dark ( when I cover the LDR), it remains same.

int photoCellPin = A0;
int photoCellReading = 0;
int ledPin = D0;
int ledBrightness =0;

void setup() {
    pinMode (ledPin,OUTPUT);


void loop() {
    photoCellReading = analogRead (photoCellPin);
    ledBrightness = 255 - map (photoCellReading,0,4095,0,255);
    analogWrite (ledPin, ledBrightness);
    delay (1000);


I’m not sure what I’m doing wrong here.

1.i’m deducting 255 from ledBrightness in loop function to ensure the reverse value is stored i.e when light is High led brightness should be low & vice versa.

2.I have also tried through map function i.e map (photoCellReading,0,4095,255,0) with no success.

Can someone help me with what’s the problem in 1 & 2? And what’s the best way to resolve my problem?

Thanks in advance !

To get a different reading than HIGH with the LDR, you need to build a voltage divider.

How have you wired your LDR?


hey @ScruffR - many thanks for your reply. Here is my circuit for you reference

Just for reference - Resistor on LED is 150 ohm while that on LDR is 1.5K ohm.
Do you see any problem here either in the circuit or the code?

What is the range of resistance of this LDR in the dark and light. Is 1500 ohms the value in bright light or in the dark? I suspect that your resistor value is too low, but it’s hard to tell without knowing the range.

Hello @Ric - Just measured the LDR resistance.
In full brightness the LDR resistance is 12- 15K while in darkness it is ~130K.
1500 ohms is value of the carbon resistor (green one) connected to the LDR. Thanks !

So, the value of your resistor is definitely too low. The formula for calculating the voltage at A0 is:

Bright: 3.3 * 1500/(15000 + 1500) = 0.3 volts

Dark: 3.3 * 1500/(130000 + 1500) = 0.04 volts

So you see, you’re only using a small fraction of the total range available to you. You should work through the math to find a more suitable value. It will need to be more in the range of the dark resistance value.

1 Like

Sounds great. I will try changing the 1500 ohm resistor to higher & keep you updated on the development.

One more question on map function. Do this function arguments correctly maps low light to high (255) brightness & vice versa?

map (photoCellReading,0,4095,255,0)

Or do I need to subtract 255 from the photoCellReading & then store it in ledBrighness variable. Which one is most preferred?

Yes it does.

BTW, you can map over a smaller range than the full 0 to 4095. Once, you figure out what the minimum and maximum values that you can get, you can use that range to map to 0 to 255.

1 Like

Many thanks @Ric. You have been immensely helpful. :grinning: I have added a 500K resistor - works partially to my satisfaction. Now I know I must experiment further with the value to work it the way it should be.
Thanks once again.

Instead of experimenting with different resistors, why not do the calculations? If you put the formulas I showed before in a for-loop, you can see what results many different resistors would give, and see which one gives you the largest difference between the dark and light cases.