There are some situations in which it makes sense to connect an LDR to an ESP. This makes it relatively easy and inexpensive to measure the brightness in a room.
This value can then be used, for example, to detect presence (Has someone switched on the light even though I'm not at home?) or to set a display brightness according to the brightness (see, for example: pxlBlck - Automatic adjustment 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 notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
For the assembly you have to master soldering tasks. The following articles contain tips on this.
- Electronics - My friend the soldering iron
- Electronics – Solder THT components by hand
- Electronics – Solder SMD components by hand
In the following list you will find all the parts you need for the assembly.
Connect an LDR to the ESP8266
In order for the ESP8266 to be able to measure the ambient brightness using the LDR, you must of course first connect an LDR. An LDR is a light-dependent resistor whose resistance depends 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 in the following circuit diagram.
You can find further examples in the following articles on the various pxlBlck platforms.
Connecting 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 way. The instructions are an excerpt from the assembly article for the pxlBlck - build pxlBlck_RingClock.
For this you need an LDR and a "normal" (1/W) 1k resistor.
Some shrink tubing (not shown in the picture) is also very helpful.
Then solder the 1k resistor to the contacts of the Wemos D1 MIni as shown.
The resistor is soldered between GND and the input of the ADC. It thus functions as a series resistor to the LDR and forms a voltage divider in combination with the LDR.
Close-up of the soldered resistor.
Close-up of the soldered resistor.
You must now solder the LDR between the contacts of the analog digital converter and 3V3.
To do this, you should protect at least one of the LDR contacts against short circuits with heat-shrink tubing.
The LDR itself should then protrude approx. 5mm beyond the end of the board of the Wemos D1 Mini.
Another view of the installed LDR.
Configure the ADC of the ESP8266
So that the brightness values of the connected LDR can be read in, you must first configure the analog digital converter of the ESP8266. Thanks to ESPEasy, this is done quickly.
To do this, go 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 out the IP address of devices in your network is also described in the article Display/find out the IP address of the devices in the network described.
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 be redirected to a page where you can configure a device.
To configure the Analog to Digital Converter you have to add the entry
Analog input – external
Now you should configure the "Analog input - external" device as shown in the picture on the left.
You must then 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 to a different range, you can set this here.
In the 2×2 table shown on the left, you have the option of configuring the corresponding values for two points.
In this case, I have found out (by trial and error) that my LDR has a value of 400 at the highest brightness in the room. The brightness should now be converted into the range 0 to 10.
To do this, you must configure the options in the "Point 1" and "Point 2" section as shown in the image.
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS 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 think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂