New project input

I'm laying out my under cabinet lighting. I'm going to use WS2812 Led's and put them in these:

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 :slight_smile:

I'm also throwing this in:

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!

Thank you!

What Arduino board?

What is the relay for?

What sensor for light brightness?

Post a schematic or block diagram of your proposed system.

It sounds like you are trying to make a 12V led dimmer with a relay (?).

Instead of using a relay, use a MOSFET like this:

That way, you can adjust the brightness and turn it off / on.
Note: Must use a N-channel MOSFET for this schematic.

Simple code to control brightness:

#define MOSFET 3 //pin D3

void setup(){
  pinMode(MOSFET, OUTPUT);
}

void loop(){
  analogWrite(MOSFET, 177);  // turn lights on 50% brightness; 0 - 255 is the brightness setting (PWM)
  delay(5000); // wait for 5 seconds
  for (int i=0;i<255;i++){
    analogWrite(MOSFET, i);  //increase LED brightness
    delay(20);
  }
  for (int j=255;j>0;j--){
    analogWrite(MOSFET, j);  //decrease LED brightness
    delay(20);
  }
}

Make sure to use a PWM pin.

Here is a suitable logic level transistor:
STP90NF03L

1 Like

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.

3 Likes

Wow :grimacing:

I somehow missed that they were WS2812 instead of 12V strips.

@cowboysdude, To control brightness on a WS2812 you just put FastLED.setBrightness(100); if you're using the FastLED library.

1 Like

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.

1 Like

You do not need the relay nor a MOSFET, just the photocell.

Go through this tutorial to see how to interface and program for WS2812 LED strips.

Here is a tutorial on using an LDR.

Put the 2 together to control the brightness using the setBrightess() function of the FastLED library.

2 Likes

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.

1 Like

Where do you get those quarter round tubes for the LEDs? I may do something similar for my shop cabinets, just for fun.

AliExpress

No, no you're fine. That's my fault, not yours! I'm still trying to hammer it out LOL

1 Like

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.

This simple sketch should adjust the brightness of the LEDs by using a LDR:

#include <FastLED.h>

//LED strip
#define NUM_LEDS 1
#define DATA_PIN 3

//LDR
#define LDR A0 //analog 0

CRGB leds[NUM_LEDS];

int ldr;
int ldrValue;

void setup(){
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  pinMode(LDR, INPUT);
}

void loop(){
  ldr = analogRead(LDR);

  ldrValue = map(ldr, 0, 1023, 0, 128); //128 is max brightness setting of LEDs

  FastLED.setBrightness(ldrValue);
  for (int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 255, 255);  //white color
    FastLED.show();
  }
  delay(100);  //repeat every 0.1 second
}

This link should help with wiring the LDR sensor:
https://create.arduino.cc/projecthub/electronicsfan123/interfacing-arduino-uno-with-ldr-8760ba

The WS2812 strip is wired like so:

LEDs Arduino
+5V(VIN) +5V
DIN D3
GND GND

Hope this helps!

This is how I have it wired.... of course it's not working LOL. [Very crude drawing]

I know this is wrong because well it's not working :wink:

Where am I getting this wrong?

Thank you!!!!!

How is it not working? The LEDs aren’t turning on? Your wiring photo is correct!

Try this code and report back with what is printed in the serial monitor:


#include <FastLED.h>

//LED strip
#define NUM_LEDS 1
#define DATA_PIN 3

//LDR
#define LDR A0 //analog 0

CRGB leds[NUM_LEDS];

int ldr;
int ldrValue;

void setup(){
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(100); //set generic brightness at beginning
  pinMode(LDR, INPUT);
}

void loop(){
  ldr = analogRead(LDR);

  ldrValue = map(ldr, 0, 1023, 128, 0); //128 is max brightness setting of LEDs

  Serial.println(“ldr: “);
  Serial.println(ldr);
  Serial.println(“ldrValue: “);
  Serial.println(ldrValue);

  FastLED.setBrightness(ldrValue);
  for (int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 255, 255);  //white color
    FastLED.show();
  }
  delay(100);  //repeat every 0.1 second
}

Don’t forget to change the NUM_LEDS variable to however many LEDs you have on your strip.

How many LEDs in your strip? The Arduino can supply limited current. For more than 5 or 6 you will need an external supply.

2 Likes

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:
LDR_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.