Chapter 12 Photoresistor & LED

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

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

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

LED x1

Chapter01_03

Jumper M/M x4

Chapter01_05

Photoresistor x1

Chapter12_06

Resistor 220Ω x1

Chapter01_04

Resistor 10kΩ x2

Chapter07_04

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.

../../../_images/Chapter12_00.png

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

../../../_images/Chapter12_01.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.

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_02

Hardware connection

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

Chapter12_03

Code

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Codes of this project is logically the same as the project Soft Light.

12.1_Nightlamp

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

Click “Run current script”. Cover the photoresistor with your hands or illuminate it with lights, the brightness of LEDs will change.

The following is the program code:

 1from machine import Pin,PWM,ADC
 2import time
 3
 4pwm =PWM(Pin(25,Pin.OUT),1000)
 5adc=ADC(Pin(36))
 6adc.atten(ADC.ATTN_11DB)
 7adc.width(ADC.WIDTH_10BIT)
 8
 9try:
10    while True:
11        adcValue=adc.read()
12        pwm.duty(adcValue)
13        print(adc.read())
14        time.sleep_ms(100)
15except:
16    pwm.deinit()