Changing brightness of WS2812 using photoresistor

Hi, for my project I want to use a photoresistor to control the brightness of my WS2812-Module. I am using the <Freenove_WS2812_Lib_for_ESP32.h>-library on an ESP32 and already have tried using the setBrightness() function. Can someone help?
Thanks in advance!

#include <Freenove_WS2812_Lib_for_ESP32.h>
Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(8, 15, 0, TYPE_GRB);
void setup(){
  digitalWrite(pLED, HIGH);
  digitalWrite(pLCDLED, HIGH);
  strip.begin();
  strip.setBrightness(100);
  Serial.begin(9600);
  strip.setLedColorData(0, 0, 169, 255);
  strip.setLedColorData(1, 195, 255, 0);
  strip.setLedColorData(2, 255, 33, 0);
  strip.setLedColorData(3, 158, 0, 0);
  strip.setLedColorData(4, 0, 169, 255);
  strip.setLedColorData(5, 195, 255, 0);
  strip.setLedColorData(6, 255, 33, 0);
  strip.setLedColorData(7, 158, 0, 0);
  strip.show();
}
void loop(){
  int photoBrightness = analogRead(photoresistor) / 40.96;
  Serial.println(photoBrightness);
  strip.setBrightness(photoBrightness);
}

Welcome to the forum

Please post your full sketch, using code tags when you do, and describe what problems you have with that sketch

That is a HUGE clue.

So are you saying that photoBrightness does not change with the amount of light on the photoresistor?

How do you have the photoresistor connected?

The variable changes and the resistor is connected properly. It seems to be a problem with the function I mentioned above.

Does it matter that I set the brightness in the beginning?

You need to update the state of the strip in loop() to make the brightness change

strip.show() sounds like the function you need

Good call but I just tried and it didn't work.

Post your revised sketch in a new reply

The Adafruit library does brightness in a non-destructive manner.

Other smart pixel libraries may not; once you've reduced brightness, it won't get brighter if you change it back up.

As for what you tried, always post the code or we won't know if you did what was suggested and correctly.

a7

  int photoBrightness = analogRead(photoresistor) / 40.96;
  Serial.println(photoBrightness);
  strip.setBrightness(photoBrightness);
  strip.show();

This code is the only thing I've changed.

What values do you see printed for photoBrightness ?

1 Like

Thanks. With a small sketch it is best to post it in its entirety. "this is all I changed" makes work for us.

But I write to ask what values are appropriate for the brightness, and what values are you giving it?

4096 / 40.96 = 100 maximum, is that what you are aimed for?

Print the value you send to the brightness function at the moment you send it.

Oh wait, you do. What values are printed?

a7

In the dark about 100 and in direct light it depends on the brightness but with a flashlight directly above it is 0.
And yes, 100 is supposed to be the highest value.
Giving you the entire sketch would give to much work because it is massive due to the size of my project.

This is the setBrightness() function of the library

void Freenove_ESP32_WS2812::setBrightness(uint8_t brightness)
{
    br = constrain(brightness, 0, 255);
}

The br variable is only used in the setLedColorData() function of the library

My conclusion would be that the setBrightness() is a waste of time except when used in setup()

esp_err_t Freenove_ESP32_WS2812::setLedColorData(int index, uint8_t r, uint8_t g, uint8_t b)
{
    uint8_t p[3];
    p[rOffset] = r * br / 255;
    p[gOffset] = g * br / 255;
    p[bOffset] = b * br / 255;
    return set_pixel(index, p[0], p[1], p[2]);
}

If I may ask, what exactly is your solution?

Use a different library that supports changing the brightness of the LED on the fly

Do you have one in mind?

FastLED has a good reputation and I believe that it can do what you want

Thanks, I'm going to try it.