Chapter 11 Photoresistor & LED
In this chapter, we will learn how to use a photoresistor.
Project 11.1 NightLamp
A photoresistor is very sensitive to the amount of light present. We can take advantage of the characteristic to make a nightlight with the following function: when the ambient light is less (darker environment) the LED will automatically become brighter to compensate and when the ambient light is greater (brighter environment) the LED will automatically dim to compensate.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
LED x1
|
Jumper M/M x4
|
||
Photoresistor x1 |
Resistor 220Ω x1
|
Resistor 10kΩ x2
|
|
Component knowledge
Photoresistor
A photoresistor is simply a light sensitive resistor. It is an active component that decreases resistance with respect to receiving luminosity (light) on the component’s light sensitive surface. A photoresistor’s resistance value will change in proportion to the ambient light detected. With this characteristic, we can use a photoresistor to detect light intensity. The photoresistor and its electronic symbol are as follows.
The circuit below is used to detect the change of a photoresistor’s resistance value:
In the above circuit, when a photoresistor’s resistance vale changes due to a change in light intensity, the voltage between the photoresistor and resistor R1 will also change. Therefore, the intensity of the light can be obtained by measuring this voltage.
Circuit
The circuit of this project is similar to project Soft Light. The only difference is that the input signal is changed from a potentiometer to a combination of a photoresistor and a resistor.
Schematic diagram |
|
Hardware connection |
If you need any support, please contact us via: support@freenove.com
|
Sketch
The circuit used is similar to the project Soft Light. The only difference is that the input signal of the AIN0 pin of ADC changes from a potentiometer to a combination of a photoresistor and a resistor.
Sketch_11.1_Nightlamp
Download the code to ESP32-WROVER, if you cover the photoresistor or increase the light shining on it, the brightness of the LED changes accordingly.
The following is the program code:
1/**********************************************************************
2 Filename : NightLamp
3 Description : Controlling the brightness of LED by photoresistor.
4 Auther : www.freenove.com
5 Modification: 2024/06/18
6**********************************************************************/
7#define PIN_ANALOG_IN 4
8#define PIN_LED 12
9#define CHAN 0
10#define LIGHT_MIN 372
11#define LIGHT_MAX 2048
12void setup() {
13 ledcAttachChannel(PIN_LED, 1000, 12, CHAN);
14}
15
16void loop() {
17 int adcVal = analogRead(PIN_ANALOG_IN); //read adc
18 int pwmVal = map(constrain(adcVal, LIGHT_MIN, LIGHT_MAX), LIGHT_MIN, LIGHT_MAX, 0, 4095); // adcVal re-map to pwmVal
19 ledcWrite(PIN_LED, pwmVal); // set the pulse width.
20 delay(10);
21}
Reference
- constrain(amt, low, high)
#define constrain(amt,low,high) ((amt)<(low)? (low):((amt)>(high)? (high):(amt)))
Constrain the value amt between low and high.









