Mounted to the front side underneath the edge so they can be hidden.
I also want to be able to control the brightness depending on the room light. I am going to use a power supply that I can plug into a SONOS Zigbee plug [for my home assistant] so they are automatic for sunset
Mainly because I want to control the light, so if the kitchen lights are on these do not need to be one and it'll cut off the power to the lights, etc.
What I am trying to figure out is how to create a sketch that will control the brightness. So say the sensor detects certain amounts of light it can adjust brightness automatically. So if the kitchen is completely dark I don't have blaring white lights under the cabinets, maybe brightness at 50%.
Is there any example I could look at to perhaps get an idea of how to proceed? I'm not a sketch writer and have struggled with them.
Any input and/or help would be greatly appreciated!
WS2812 LEDs are not controlled by PWM. Nor is a relay a good way to control them. WS2812 LEDs are powered by 5V. Use a library like FastLED to control them. Measure the light level with a Light Dependent Resistor (LDR). Then you can use the setBrightness() function to set the global brightness of the LED strips to the desired level or set individual LEDs to the brightness and color desired.
First off Thank you ALL!!! The relay has a photoelectric cell attached so my thinking was depending on the reading from that I could control the brightness. With the sketch you provided I believe I still can!
Please note that I did not realize you were talking about WS2812 LEDs. I thought you were talking about a normal 12V led strip. My previous comments were for controlling the brightness of a 12V led strip.
To control WS2812 LEDs, just use the FastLED library. They have some good example codes. You don't need a MOSFET or anything. You control them with one digital pin attached to the DIN pin of the WS2812 LED strip.
Here is a demo of how to control the WS2812 LED strip brightness with an LDR. I have enabled the internal pullup on A0 to replace the 10K resistor in the demo for simplicity. The 10K resistor will increase sensitivity, use it if you like. Adjust the value of DIVISOR to get the range of brightness that you want. The code in the for loop just puts some color to the strip for the demo. The strip will be dim with full light and bright on in the dark.
#include <FastLED.h>
const byte DATA_PIN = 4;
const byte ldrPin = A0;
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 28
CRGB leds[NUM_LEDS];
byte brightness = 100;
const byte DIVISOR = 3; // adjust for range of LDR readings
void setup()
{
Serial.begin(115200);
Serial.println("Starting FastLED brightness with LDR demo");
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(brightness);
pinMode(ldrPin, INPUT_PULLUP);
}
void loop()
{
int brightNow = analogRead(ldrPin);
brightness = brightNow / DIVISOR;
FastLED.setBrightness(brightness);
static unsigned long timer = 0;
unsigned long interval = 100;
if (millis() - timer >= interval)
{
timer = millis();
Serial.println(brightness); // read the range of values light to dark and adjust
// DIVISOR to give the brightness range required
for (int n = 0; n < NUM_LEDS; n++)
leds[n].setHSV( 5 * n, 255, 100);
}
FastLED.show();
}
Also adjust NUM_LEDS to your strip and the constructor and the initializer in setup for your particular strip.
Yes I get all that but the wiring is the now the thing I'm having an issue with. I can figure out how to write the sketch now but trying to figure out how to use the ldr and the leds has me stumped a bit. Looking at the example they are using a 2 wire led.... the led strip has the 5+V and ground plus the data wire.... so just trying to figure that part out.
I left the leds set to 1 to test it. There will be multiple LEDS and I don't have them hooked up to an external power supply yet so I figured for testing the Uno would power 1.
Here is the output:
The 1 light is on.... but when I have a light on the LED stays on.
Not sure what's going on here, possible a bad LDR?
That just means the map function needs to be adjusted.
Replace the map function with this:
ldrValue = map(ldr, 0, 300, 128, 0);
If the light doesn’t turn on, change the second number(300), to something lower. You can adjust that value higher or lower to adjust when the LEDs turn on or off relative to the LDR sensor.
Also, can you post a screenshot of when you shine a light on it and when you don’t?
Thank you for all your help! Still not working though. I can adjust the number and nothing is changing.
No matter what the ldr is reading the light stays on. If I turn on a light the light stays on. If I turn off the light the light stays on. So really not really sure what is going on.