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!

2.12 Feel the Light

In this lesson, we’ll learn how to use a photoresistor (also known as a light-dependent resistor or LDR) with the Raspberry Pi Pico 2 to measure light intensity. A photoresistor changes its resistance based on the amount of light it receives: the brighter the light, the lower the resistance. This makes it ideal for detecting changes in ambient light.

What You’ll Need

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

Newton Lab Kit

450+

Newton Lab Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico 2

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Resistor

1(10KΩ)

BUY

6

Photoresistor

1

BUY

Circuit Diagram

sch_photoresistor

In this circuit, a 10K resistor and a photoresistor are connected in series, forming a voltage divider. GP28 reads the voltage across the photoresistor, while the 10K resistor provides protection by limiting current.

  • Bright Light: The photoresistor’s resistance decreases, lowering its voltage and the GP28 reading. In strong light, its resistance approaches zero, and GP28 reads close to 0. At this time, the 10K resistor plays a protective role, so that 3.3V and GND are not connected together, resulting in a short circuit.

  • Darkness: The photoresistor’s resistance increases, raising its voltage and the GP28 value. In complete darkness, its resistance is nearly infinite (the 10K resistor is negligible), and GP28 reads close to 1023.

The calculation formula is shown below.

Digital Value = (Analog Voltage/3.3V) * 1023

Wiring Diagram

wiring_photoresistor

Writing the Code

Note

  • You can open the file 2.12_feel_the_light.ino from newton-lab-kit/arduino/2.12_feel_the_light.

  • Or copy this code into Arduino IDE.

  • Select the Raspberry Pi Pico 2 board and the correct port, then click “Upload”.

const int sensorPin = 28;   // Photoresistor connected to GP28 (ADC2)

void setup() {
  Serial.begin(115200);    // Initialize Serial Monitor
}

void loop() {
  // Read the analog value from the photoresistor
  int sensorValue = analogRead(sensorPin);
  // Print the sensor value to the Serial Monitor
  Serial.println(sensorValue);
  delay(500);  // Wait half a second before reading again
}

When the code is running and the Serial Monitor is open:

  • Observing the Sensor Values:

    You should see a stream of numbers representing the analog values from the photoresistor.

  • Interacting with the Photoresistor:

    • Shine a flashlight or a lamp on the photoresistor. The sensor values should decrease (since resistance decreases with more light).

    • Cover the photoresistor with your hand or place it in a dark area. The sensor values should increase (since resistance increases with less light).

Understanding the Code

  1. Defining the Sensor Pin:

    Assigns sensorPin to GPIO 28, which is connected to the analog input.

    const int sensorPin = 28;   // Photoresistor connected to GP28 (ADC2)
    
  2. Initializing Serial Communication:

    Starts serial communication, allowing you to print messages to the Serial Monitor.

    Serial.begin(115200);
    
  3. Reading the Analog Value:

    Reads the analog voltage at sensorPin and returns a value between 0 and 1023.

    int sensorValue = analogRead(sensorPin);
    
  4. Printing the Sensor Value:

    Outputs the sensor value to the Serial Monitor.

    Serial.println(sensorValue);
    
  5. Adding a Delay:

    Waits for 500 milliseconds before the next reading.

    delay(500);
    

Converting to Voltage

If you want to see the actual voltage value being read, you can modify the code:

const int sensorPin = 28;   // Photoresistor connected to GP28 (ADC2)

void setup() {
  Serial.begin(115200);    // Initialize Serial Monitor
}

 void loop() {
   int sensorValue = analogRead(sensorPin);
   // Convert the analog reading to voltage
   float voltage = sensorValue * (3.3 / 1023.0);
   Serial.print("Sensor Value: ");
   Serial.print(sensorValue);
   Serial.print("  Voltage: ");
   Serial.print(voltage);
   Serial.println(" V");
   delay(500);
 }

Further Exploration

  • Control an LED Based on Light:

    Use the photoresistor to control the brightness of an LED or turn it on/off based on light levels.

  • Data Logging:

    Record the light intensity over time to monitor changes in the environment.

  • Build a Night Light:

    Create a light that turns on automatically when it gets dark.

Conclusion

In this lesson, you’ve learned how to use a photoresistor with the Raspberry Pi Pico to measure light intensity. By reading the analog voltage from a voltage divider circuit, you can detect changes in light levels and use this information in your projects.