I want to install some LED stripes on my motorcycle, i.e. as brake light and indicators. I use WS2812 LED stripes which can be very bright - especially with many LEDs on at the same time.

I want to measure the ambient light so that in the bright sun I can light the LEDs with, let's say, 100%. But in the night maybe just with 30% intensity. I want to detect the ambient light to change the intensity.

I don't need high resolution and I don't need high speed. If the ambient light is measured i.e. once per second with 4 bit resolution that would be already good enough.

Which ambient light sensor (or photo diode or whatever) makes sense for this little project and how to connect it? Does it make sense to use more than one light sensor for a better average ambient light value (just in case one sensor is in the shadow, another in the sun)?

I found these sensors form a Chinese supplier:



But an I2C interface seems to be overkill for my simple project.

Any suggestions?

2 Answers 2


Perhaps a LDR and a 10k ohm resistor might work.

enter image description here

#define LDRpin A0
unsigned int LDRvalue = 0;

void setup(){

void loop(){
  LDRvalue = analogRead(LDRpin);
  • 2
    Note that swapping the LDR and the resistor might make the wiring easier: you will just have to pull a single wire from the LDR to the Arduino, as the ground connection can be made through the chassis. And even if you pull two wires, you have an extra safety: an accidental short from the GDN wire to the chassis won't fry anything, whereas a short from +5V could burn the Arduino. Jun 18, 2018 at 7:36
  • Thanks, that looks good. I just checked and it seems there are many types of this sensor 5506 / 5516 / 5526 / 5528 / 5537 / 5539 / 5549. The main difference seems to be the resistance. Min is 2~6kOhm for the GL5506, max is 45~140 kOhm for the GL5549. I guess they will all work with the correct fixed resistor. Any recommendation?
    – Edgar
    Jun 18, 2018 at 8:25
  • Different resistance + LDR combinations will all work - it's just a matter of what sensitivity it gives you to different light levels.
    – Majenko
    Jun 18, 2018 at 9:43

You can also use an LED. Connect the LED and its resistor between two port pins. Then, when you want to measure ambient light in your program, do this:

  1. Make sure any pullups/pulldowns are disabled.
  2. To discharge the LED, start with switching it on for a short moment (a clock cycle is enough) by setting the pins to output and setting anode high and cathode low.
  3. Reverse the polarity by setting the anode pin low and the cathode pin high.
  4. Set the cathode pin to input.
  5. Using a timer and a pin change interrupt, measure the time it takes for the cathode pin to become low.

Since the capacity of the LED varies with light intensity, the time it takes for the LED to discharge through its resistor and the pin resistance is is an indication of light intensity.

  • Thanks, I saw that example. But somehow I think let LEDs do what they are designed for and use other parts (i.e. LDR from the answer above) for what they are designed for...
    – Edgar
    Jun 18, 2018 at 10:47
  • You're saying the functionality of a LED changes with external light intensity? What? How does that even work? Can you provide more details, or a citation, because this is really surprising to me.
    – Benubird
    Jun 18, 2018 at 12:23
  • 1
    @Benubird Turns out, there is a Wikipedia article about it: en.wikipedia.org/wiki/LED_circuit#LED_as_light_sensor
    – AndreKR
    Jun 18, 2018 at 12:30
  • @Benubird In fact any semiconductor junction will function as a (poor quality) photodiode. There is nothing special about light emitting diodes, except that for non-light-emitting diodes that are not designed to be photodiodes, the junction is covered in opaque material to prevent the photoelectric effect happening. In some semiconductor devices, the opaque material is simply black paint which can be scraped off to make a photodiode!
    – alephzero
    Jun 18, 2018 at 13:13
  • @alephzero Wow, that is very cool! So OP doesn't need a separate sensor, they can just repurpose their LEDs. I wonder if anyone has built some sort of prepackaged auto-adjusting LED
    – Benubird
    Jun 18, 2018 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.