Light detector module

Hi,

I read on another forum about a guy suggesting to use a light sensor module with digital output ( https://www.aliexpress.com/item/32571120284.html ) to make my LED strip turn on at night, and off during the day.

I have connected the module to D3, setup the macro T=2, and as such is working, however, the light sensor has to reach off/on (turn light off / turn light on) before it will turn off the light, and again off/on to turn it on again.

Is it not possible to make it work like I want to somehow?

Hi,

the button pin is not really meant to interface to a sensor. Of course it is possible to get this to work, but I’d suggest a usermod to read the state of a pin to which the sensor is attached and then set the brightness accordingly using bri = x; or toggleOnOff().

Thanks, seems like it’s gonna be a bit above my head, as I have never written a piece of code :slight_smile:
Would I be able to find a sketch perhaps, that is already reading the state from a sensor, and then just more or less copy it, and replace the functions with toggleOnOff() ?

Just replace your usermod.cpp file with this (completely untested though!)
I recommend using the VS Code development environment with platformio, it makes it a lot simpler to compile WLED on your own :slight_smile:

#include "wled.h"

#define SENSOR_PIN 5 //pin D2

bool previousReading = false;
unsigned long lastReadingTime = 0;

void userSetup()
{
  pinMode(SENSOR_PIN, INPUT);
	previousReading = !digitalRead(SENSOR_PIN);
}

void userConnected()
{

}

void userLoop()
{
	if (millis() - lastReadingTime < 2000) return; //take reading every 2 seconds
	bool sensorReading = digitalRead(SENSOR_PIN);
  if (sensorReading != previousReading) { //reading has changed
		if (sensorReading) {
			bri = briLast;
		} else {
			if (bri != 0) briLast = bri;
			bri = 0;
		}
		colorUpdated(NOTIFIER_CALL_MODE_NO_NOTIFY);
		previousReading = sensorReading;
	}
	lastReadingTime = millis();
}

Thanks a lot :slight_smile:
I already have VS and platformio running from compiling Marlin firmware, so hopefully I can figure the rest out now.

I have to use pin D2 now instead of D3 correct ?

Correct! The problem with D3 is that the ESP is unable to boot if the pin D3 is pulled low :slight_smile:

I can’t get it to work.

When I boot it, it’s going dark after a couple of seconds, and staying there.
I tried changing pin 5 to 4, as I read that’s D2 for the 8266, but of course not sure at all, and it didn’t change anything.

Maybe a stupid question, but could be it be something with a pull-up resistor?
I keep reading about that :innocent:

Ah sorry. I confused the pins :see_no_evil: . Pin 5 is D1, 4 is indeed D2. So please try 5, because 4 is used by the optional infrared function of WLED, that might interfere.
Depending on the sensor type, a pull-up resistor between pin 5 and 3.3v might indeed be required, you could give that a try :innocent:

Thanks, seems like it’s working fine now :smiley:

Or no, apparently not :no_mouth:

I was grounding and 3.3 volting the D2 on the esp board, and that worked fine when I switched between them.
But when I add my sensor, it will not go LOW. I actually have to ground the D2 before it it’s working. It’s not enough to remove the voltage from the D2, eventhough it’s only about 0.15 volt.

Shouldn’t a low volt be enough to make it go LOW, or do I actually need to ground D2 somehow?

I fixed it for now by using pin D0 (16), and using input_pulldown_16 :smiley:

Still doesn’t answer my question though, about D2 needing a ground to go LOW, as 0.15V doesn’t make it go LOW.