11. Chapter Photoresistor & LED

In this chapter, we will learn how to use a photoresistor.

11.1. Project 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.

11.1.1. Component List

ESP32-S3 WROOM x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Photoresistor x1

Chapter12_00

LED x1

Chapter01_03

Resistor 220Ω x1

Chapter01_04

Jumper M/M x4

Chapter01_05

Resistor 10KΩ x1

Chapter02_01

11.1.2. Component knowledge

11.1.2.1. 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.

../../../_images/Chapter12_01.png

The circuit below is used to detect the change of a photoresistor’s resistance value:

../../../_images/Chapter12_02.png

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.

11.1.3. 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

Chapter12_03

Hardware connection.

If you need any support, please feel free to contact us via: support@freenove.com

Chapter12_04

11.1.4. Sketch

The circuit used is similar to the project Soft Light. The only difference is that the input signal of the pin of ADC changes from a potentiometer to a combination of a photoresistor and a resistor.

11.1.4.1. Sketch_Nightlamp

../../../_images/Chapter12_05.png

Download the code to ESP32-S3 WROOM, if you cover the photoresistor or increase the light shining on it, the brightness of the LED changes accordingly.

If you have any concerns, please contact us via: support@freenove.com

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/07/01
 6**********************************************************************/
 7#define PIN_ANALOG_IN   1
 8#define PIN_LED         14
 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}

11.1.4.2. 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.

long map(long value,long fromLow,long fromHigh,long toLow,long toHigh);

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.