Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 11: Photoresistor Module

In this lesson, you will learn how to use a photoresistance sensor with an ESP32 Development Board to measure light intensity. We’ll explore how the sensor detects different light levels and processes and displays these readings on the serial monitor. This project is ideal for beginners as it provides hands-on experience with analog sensors and real-time data handling in Arduino programming.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Photoresistor Module

BUY

Breadboard

BUY

Wiring

../_images/Lesson_11_Photoresistance_Module_esp32_bb.png

Code

Code Analysis

  1. Setting Up the Sensor Pin and Serial Communication

    We start by defining the sensor pin and initializing serial communication in the setup function. The photoresistor is connected to the pin 25.

    const int sensorPin = 25;  // Pin connected to the photoresistor
    
    void setup() {
      Serial.begin(9600);  // Start serial communication at 9600 baud rate
    }
    
  2. Reading and Displaying Sensor Data

    In the loop function, we continuously read the analog value from the sensor and print it to the Serial Monitor. We also add a short delay to stabilize the readings.

    void loop() {
      Serial.println(analogRead(sensorPin));  // Read and print the analog value
      delay(50);                              // Short delay to stabilize readings
    }