A photoresistor, or LDR, lets your Arduino sense how bright or dark a room is by turning light into a changing resistance value you can read on an analog pin. It is a cheap way to add ambient light awareness to a project, and with MicroPython on an Arduino Giga R1 WiFi you can read it in just a few lines of code.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MicroPython Light Sensor
What you need
- Arduino Giga R1 WiFi
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Light Sensor
- 10 kΩ Resistor
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno/Mega/Giga
- Sensors/Servo Expansion Shield for Arduino Mega/Giga
- Breadboard Shield for Arduino Mega/Giga
- Enclosure for Arduino Giga
- Power Splitter for Arduino Giga
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The LDR has two pins, and since it is just a resistor, it has no polarity, so either pin can go either way. Because a photoresistor only changes resistance rather than producing a voltage directly, you wire it together with a 10 kΩ resistor to form a voltage divider, so the analog pin sees a voltage that shifts with light level. Connect the junction between the LDR and the resistor to A0 on the Arduino Giga R1 WiFi.
How it works
A photoresistor is a resistor whose resistance changes with the amount of light hitting it. In bright light its resistance drops, and in darkness its resistance rises. By reading the voltage produced at the midpoint of the voltage divider, the Arduino can infer how much light is falling on the sensor and translate that into a usable number.
Code
The script sets up an ADC on pin A0 and continuously reads it. Since read_u16() returns a 16-bit value, the code shifts it right by 4 bits to get it back into the familiar 0 to 4095 range used by the board's 12-bit ADC. That value is then compared against a handful of thresholds to print a plain-English description, from 'Dark' up through 'Very bright', with a half-second pause between readings.
arduino-micropython-light-sensor.py
""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-light-sensor """ from machine import ADC, Pin import time # Initialize ADC (Analog to Digital Converter) adc = ADC(Pin('A0')) # The Arduino Giga WiFi pin A0 connected to the light sensor while True: # Read the input on analog pin ADC0 (value between 0 and 4095) value = adc.read_u16() >> 4 # Read the 16-bit analog value from the sensor and convert back to 12-bit ADC description = "" # We'll have a few thresholds, qualitatively determined if value < 49: description = "Dark" elif value < 829: description = "Dim" elif value < 2038: description = "Light" elif value < 3267: description = "Bright" else: description = "Very bright" print(f"Analog reading: {value} - {description}") time.sleep(0.5) # delay for 500 milliseconds Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-light-sensor

