ESP32 – Simple Luminance Controller

In this tutorial, the aim is to control the light brightness of a room. For this, an ESP32, a led (to simulate the actuator), and an LDR resistor are used. The luminosity value is set via the serial port.
In order to deepen your knowledge about LDR resistance, I advise you to visit this tutorial. For the next steps, you can use the Arduino IDE or PlatformIO together with the VS Code. If you have any questions about installing the libraries in Arduino IDE, you can view the following link. If you need more information about how to configure PlatformIO and VS Code, check this.

Parts Required

  • Arduino.h (just when use PlatformIO);
  • analogWrite.h (for PlatformIO and Arduino IDE);
  • ESP32;
  • Micro-USB to USB-A cable
  • LDR resistor;
  • LED;
  • 10kΩ resistor;
  • 330Ω resistor;
  • Jumper wires;
  • 1 Breadboard.

Circuit

The board used for this assembly is represented in Figure 1.

In order to control the LED intensity, it must be connected to a pin that supports PWM (Pulse Width Modulation, if you need more information you can read this tutorial). For this reason, the LED is connected to pin D4 (see Figure 1). As it is necessary to use another ADC to be able to convert the voltage value obtained in the voltage divider of the LDR, this was connected to pin D15 of the development board used. These connections can be seen in Figure 2.

Figure 1 (Source [1])
Figure 2 (Adapted from [2])

Libraries

In order to be able to correctly use the analog inputs (according to the code presented), it is necessary to install libraries that allow these operations to be carried out. In case these libraries are not installed, it is not possible to use the functions related to analog pins. Because it is through them that some configurations are made.

In PlatformIO, the following library presented in Figure 3 was used. You can search for “AnalogWrite.h” library and click on the Install button.

Figure 3

For the Arduino IDE, the library shown in Figure 4 was used. To show this window you can press ” Cntrl + Shift + i ” or click Sketch -> Include Library -> Manage Libraries…

Figure 4

Code

In order to carry out the necessary operations, we used only the “analogWrite.h” library in addition to the “Arduino.h” (which is usual, since the project created is based on the Arduino framework).


The developed process goes through the following steps:

  1. Illumination reading (obtained by LDR);
  2. Setting the desired luminance (serial port reading);
  3. Verification of the desired luminance;
  4. LED brightness adjustment.

In order to obtain the luminance value, is used the “conversion” function used in this tutorial.

Results

In Figure 5 and Figure 6 is represented the output result on the serial port. In Figure 7 you can see my hardware implementation.

Figure 5
Figure 6
Figure 7

References

[1] https://circuits4you.com

[2] https://www.programmersought.com/article/46417861001/

Leave a Reply

Your email address will not be published. Required fields are marked *