🦁 Tutorial 11 - Photoresistor

A photoresistor, also known as a light-dependent resistor (LDR), is a type of resistor whose resistance changes in response to the intensity of light falling on it. It is made of a semiconductor material that exhibits photoconductivity, meaning its electrical conductivity changes with light exposure.

Photoresistors are commonly used in electronic circuits to detect and measure the intensity of light. They can be used in a wide range of applications, including:

  1. Light sensing: Photoresistors can be used to detect the presence or absence of light in various lighting conditions. For example, they can be used in automatic street lighting systems that turn on street lights at dusk and turn them off at dawn.
  2. Energy-saving applications: Photoresistors can be used in energy-saving applications, such as controlling the brightness of displays or adjusting the intensity of ambient lighting based on the availability of natural light.
  3. Security systems: Photoresistors can be used in security systems to detect changes in light levels, such as in burglar alarm systems that trigger an alarm when an intruder disrupts the light falling on a photoresistor.
  4. Photography: Photoresistors can be used in photography applications, such as in light meters or exposure control circuits, to measure and adjust the amount of light for optimal photography settings.
  5. Robotics and automation: Photoresistors can be used in robotics and automation applications to enable robots or automated systems to sense and respond to changes in light conditions, such as following a light source or avoiding bright light.

Overall, photoresistors are versatile components that are commonly used for light sensing and light-dependent control in various electronic circuits and applications.

Components Needed

ComponentQuantity
Raspberry Pi Pico W1
Micro USB Cable1
Breadboard1
WiresSeveral
Resistor1 - 10KΩ
Photoresistor1

Fritzing Diagram

Code

import machine
import utime

photoresistor = machine.ADC(28)

while True:
    light_value  = photoresistor.read_u16()
    print(light_value)
    utime.sleep_ms(10)

Code Explanation

Importing necessary modules:

import machine
import utime

The code imports the machine module, which is typically used for interacting with hardware on microcontrollers, and the utime module, which is used for time-related operations.

Setting up the potentiometer and LED pins:

potentiometer = machine.ADC(28)
led = machine.PWM(machine.Pin(15))
led.freq(1000)

The code sets up a potentiometer and an LED. It creates an ADC (Analog-to-Digital Converter) object for the potentiometer using pin number 28 and a PWM (Pulse Width Modulation) object for the LED using pin number 15. It also sets the frequency of the LED PWM signal to 1000 Hz using the freq() method.

Reading potentiometer value and controlling LED duty cycle:

while True:
    value = potentiometer.read_u16()
    print(value)
    led.duty_u16(value)
    utime.sleep_ms(200)

The code enters an infinite loop and reads the value of the potentiometer using the read_u16() method, which reads the analog value as a 16-bit unsigned integer. It stores the value in the value variable. The code then sets the duty cycle of the LED PWM signal using the duty_u16() method, passing the potentiometer value as the duty cycle value. This means the brightness of the LED will be controlled by the value of the potentiometer. Finally, the code sleeps for 200 milliseconds using the utime.sleep_ms() method, providing a delay to avoid continuous reading and processing of potentiometer values.