X
Home > Blog > STEM for ESP32 > HOW to Read Light Intensity by Photoresistor with ESP32

HOW to Read Light Intensity by Photoresistor with ESP32

By r December 25th, 2024 235 views

1. Overview

KD2104

Photoresistor is an analog sensor whose resistance changes with light intensity. With this characteristic, we design circuits to convert resistance changes into voltage ones. This sensor can be applied to simulate people’s judgment of the ambient light intensity, so as to facilitate friendly interaction.

2. Parameters

Operating voltage: DC 3.3 ~ 5V
Current: 0.2 mA
Maximum power: 0.001 W
Operating temperature: -10°C ~ +50°C
Output signal: analog signal
Dimensions: 24 x 32 x 18 mm (wit)
Positioning hole: Diameter of 4.8 mm
Interface: Telephone socket

3. Principle

The working principle of photoresistor is based on the internal photoelectric effect. The brighter the light is, the lower the resistance will be.

a21

When the light intensity changes, the resistance value fluctuates, so the voltage detected at the signal end S also changes (0 ~ 3.3V). This voltage change is a continuously changing analog which takes any value within 0 ~ 3.3V. Our main board is able to directly process not analog signals, but digital ones, so ADC(Analog to Digital Converter) acquisition is required.

What is ADC?

An ADC(Analog to Digital Converter) converts analog to digital. Our board has integrated ADC acquisition, which can be used directly.

kidsIOT ADC technical specifications

  1. Resolution: 12bit

    An n-bit ADC means that the ADC has 2ⁿ scales. A 12-bit ADC includes total 212=4096 scales, and outputs 4096 digital values from 0 to 4095, with each scale is

  2. General ADC input voltage calculation:


    : reference voltage

a22

4. Wiring Diagram

The photoresistor is an analog module. It needs to be connected to a red socket of the main board.

IOT-red

Here we connect the module to socket 7. From the port view of the kidsIOT board, socket 7 is analog io port io35.

When light shines on the photoresistor, it outputs the value read at the moment.

b5

5. Test Code

/*  
  Project name: 8_light
  Function: Read the analog value of the photoresistor
  Author: keyestudio    
  Hardware connection:  
    - Connect to port 7 
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Connect to port 7   
    - Before uploading test code, please correctly connect to the development board and port
    - Set the baud rate to 9600 before using serial monitor printing
*/
#define light_Pin 35  //set photoresistor to IO35

void setup() {
  //Set the baud rate
  Serial.begin(9600);
  //set IO35 to input
  pinMode(light_Pin, INPUT);
}

void loop() {
  //Read the analog value of photoresistor
  int light_value = analogRead(light_Pin);
  //Print the analog value of photoresistor
  Serial.print("Analog value:");
  Serial.println(light_value);
  //Delay 300ms to observe the printed analog value
  delay(300);
}


6. Test Result

After uploading the test code, open the serial monitor and set the baud rate to 9600, and the serial monitor prints the analog values of the photoresistor. Cover the sensor with our hand to dim the ambient light, and the analog value decreases.

a23

7. Code Explanation

7.1 analogRead(light_Pin);

We have learned the function of reading digital signals. Herein, analogRead() is a function that reads analog signals.

Syntax: analogRead(Pin);

Pin: pin number of the read analog value

Please refer to the official website for details: analogRead() | Arduino Documentation

HOW to Detect Tilt with ESP32
Previous
HOW to Detect Tilt 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