X
Home > Blog > Automatic LED! HOW to Use Photoresistor Change Brightness with ESP32

Automatic LED! HOW to Use Photoresistor Change Brightness with ESP32

By r January 2nd, 2025 217 views


1. Overview

Automatic adjustment of LED brightness controlled by photoresistor is an intelligent lighting technology, which detects the ambient light intensity in real time and converts these data into electrical signals. The system will analyse and process these electrical signals according to a pre-set algorithm or logic, so as to automatically adjust the brightness of the LED.

When the ambient light is dark, the LED will automatically turn on; When the ambient light is sufficient, the LED dims accordingly.

This automatic adjustment not only helps save energy, but also creates a more comfortable light environment for users. This adjustment is widely used in home, office and many public places.

2. Wiring Diagram

b9

3. Test Code

/*  
  Project name: 13_Automatic_lighting
  Function: The brightness of the led is automatically adjusted by the analog value of the photoresistor
  Author: keyestudio    
  Hardware connection:  
    - Connect photoresistor to port 7
    - Connect traffic light module to port 9
  Library:  
    - none
  Cautions:  
    - Make sure you are connected to the correct port   
    - Before uploading test code, please correctly connect to the development board and port
*/
#define light_Pin 35    //connect the photoresisotr to pin IO35
#define RED_LED_PIN 17  //Connect the red LED to pin IO17

#define redLED_PWM_Channel 1  // Red led light using PWM channel 1
#define pwmFreq 5000          // PWM frequency
#define pwmResolution 8       //Set the PWM resolution to 8 so the value range is 0-255

void setup() {
  //Set IO35 to input
  pinMode(light_Pin, INPUT);
  // initialize PWM
  ledcSetup(redLED_PWM_Channel, pwmFreq, pwmResolution);
  ledcAttachPin(RED_LED_PIN, redLED_PWM_Channel);
}

void loop() {
  //read photoresisotr analog value
  int light_value = analogRead(light_Pin);
  //map photoresisotr analog value to 0-255, so value 4095 = 0; 0 = 255;
  int brightness = 255 - ((light_value * 255) / 4095);
  //Light the led according to the brightness value of the photoresistor
  ledcWrite(redLED_PWM_Channel,brightness);
  //delay 300ms
  delay(300);
}

4. Test Result

After uploading the test code, the brightness of the LED will change with ambient light intensity. The brighter the environment is, the darker the LED will light. The darker the environment is, the brighter the LED will be.

Cover the photoresistor, and the LED gets brighter. Remove you hand and the LED dims.

Modules Interaction! HOW to Use Button Control LED with ESP32
Previous
Modules Interaction! HOW to Use Button Control LED with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us