Sensor

โ˜€๏ธ Photoresistor (Light Sensor)

๐Ÿ“Œ Description

A photoresistor (also called an LDR โ€“ Light Dependent Resistor) changes its resistance depending on how much light shines on it. More light โ†’ lower resistance โ†’ higher voltage reading. Less light โ†’ higher resistance โ†’ lower voltage reading.

๐ŸŽฏ Use

In our projects, the photoresistor is used to:

  • Measure light intensity.
  • Detect day/night conditions.
  • Control brightnessโ€‘based actions (like turning on LEDs when itโ€™s dark).

๐Ÿ‘‰ Think of the photoresistor as the robotโ€™s eyes for brightness โ€” it tells the robot how much light is around.

Module parameters

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
S Analog signal pins
  • Supply voltage: 3.3V/5V
  • Connection: PH2.0 terminal wire
  • Installation method: double screw fixing

๐Ÿ–ฅ๏ธ Corrected Code Explanation

import machine
import time

adc39 = machine.ADC(machine.Pin(39))          # Photoresistor connected to pin 39
adc39.atten(machine.ADC.ATTN_11DB)            # Set attenuation for wider voltage range
adc39.width(machine.ADC.WIDTH_12BIT)          # 12-bit resolution (0โ€“4095)

while True:
    light_intensity = adc39.read()            # Read light level
    print('Light Intensity: ', light_intensity)
    time.sleep(0.5)                           # Wait half a second

Step by Step

  1. adc39 = machine.ADC(machine.Pin(39)) โ†’ Reads the analog signal from the photoresistor.
  2. atten and width โ†’ Configure the ADC for accurate readings (0โ€“4095).
  3. while True: โ†’ Loop that repeats forever.
  4. light_intensity = adc39.read() โ†’ Reads the brightness level.
  5. print(...) โ†’ Shows the light intensity value.
  6. time.sleep(0.5) โ†’ Adds a short delay between readings.

๐Ÿ‘‰ Together, this program prints the light intensity every half second.


โœ… Conclusion of the Test

  • If the printed values increase when exposed to bright light and decrease in darkness, the program is working correctly.
  • This test shows how the robot can sense light levels and react to brightness changes.
  • Later, the photoresistor could be used to automatically turn on lights at night, adjust screen brightness, or trigger alarms when light changes suddenly.
On this page