There are some situations in which connecting an LDR to an ESP makes sense. In this way, the brightness in a room can be measured relatively easily and for little money.
This value can then be used, for example, to detect presence (has someone switched the light on even though I’m not at home?) Or to set a display brightness according to the brightness (see for example: pxlBlck – automatic setting of the display brightness via LDR ).
This article describes how you can connect an LDR to an ESP8266 and read it out with ESPEasy.
I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂
You can find additional tips on ESPEasy in the following articles.
- ESPEasy – Execute commands and actions via HTTP, MQTT, UDP and directly
- ESPEasy – Compile firmware, flash it and configure WiFi access data
- ESPEasy/Wemos D1 Mini – Adapt ADC to a different maximum voltage
For the construction you have to master soldering tasks. The following articles provide tips on how to do this.
- Electronics – My friend the soldering iron
- Electronics – Soldering THT components by hand
- Electronics – Soldering SMD components by hand
Connect an LDR to the ESP8266
So that the ESP8266 can measure the ambient brightness using the LDR, you must of course first connect an LDR. An LDR is a light-dependent resistor, the resistance of it is dependent on the ambient brightness or the light that hits the LDR.
If the brightness changes, the resistance of the LDR also changes. If the LDR is connected to the ESP8266 via a voltage divider, this resistance and thus the brightness can be measured indirectly using the ADC.
The basic circuit diagram for connecting an LDR to an ESP8266 can be seen on the following circuit diagram.
You can find more examples in the following articles on the various pxlBlck platforms.
- pxlBlck – Build the pxlBlck_SlotClock
- pxlBlck – Build the pxlBlck_Pot
- pxlBlck – Build the pxlBlck_RingClock
Connect the LDR to a Wemos D1 Mini
The following instructions show how you can connect an LDR to a Wemos D1 Mini in a very space-saving manner. The instructions are an excerpt from the construction article for the pxlBlck – Build the pxlBlck_RingClock.
For this you need an LDR and a “normal” (1 / W) 1k resistor.
Also, some heat shrink tubing (not shown in the picture) is very helpful.
Then solder the 1k resistor to the contacts shown on the Wemos D1 MIni as shown.
So the resistor between GND and the input of the ADC is soldered. It functions as a series resistor to the LDR and, in combination with the LDR, forms a voltage divider.
Close-up of the soldered resistor.
Close-up of the soldered resistor.
You now have to solder the LDR between the contacts of the analog digital converter and 3V3.
For this you should protect at least one of the LDR contacts against short circuits with a shrink tube.
The LDR itself should then protrude approx. 5mm beyond the end of the Wemos D1 Mini board.
Another view of the installed LDR.
Configure the ADC of the ESP8266
So that the brightness values of the connected LDR can be sensed, you must first configure the analog digital converter of the ESP8266. Thanks to ESPEasy, this is done quickly.
To do this, switch to the web interface of your ESPEasy device. To do this, enter the IP address of the device in the address line of your browser.
How to find the IP address of devices in your network is also described in the article Displaying/Finding out the IP address of the devices in the network.
Then switch to the “Devices” area.
All configured devices are now listed in the table shown.
Now click on the “Edit” button in the first empty line.
You will now be redirected to a page where you can configure a device.
In order to configure the analog digital converter you have to choose the entry
Analog input – external
Now you should configure the device “Analog input – external” as indicated in the picture on the left.
You then have to confirm the settings by clicking on “Submit”.
Scale LDR value to another target range
The settings of the “Analog Input – internal” device contain another practical setting option.
Without further configuration, the ADC value (i.e. a value between 0 and 1023) is displayed. If you prefer to convert the value of the LDR or its measured brightness in another area, you can set this here.
To do this, you have the option of configuring the corresponding values for two points in the 2×2 table shown on the left.
In this case I found out (by trying it out) that my LDR has the value 400 at the highest brightness in the room. The brightness should now be converted into the range 0 to 10.
For this you have to configure the options in the section “Point 1” and “Point 2” as shown in the picture.
I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂
P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂