Built-in project · ESP32-DevKitC V4

MicroPython night lamp: a photoresistor dims an LED, on an ESP32-DevKitC V4

MicroPython on an ESP32: a photoresistor and a 10k on GPIO 36 set the LED on GPIO 25, which glows brighter as the room gets darker. The whole build, an ESP32-DevKitC V4 and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $10.30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A voltage divider with a light sensor
  • Reading an analog value with the ADC
  • PWM duty as LED brightness
  • Turning a sensor reading into an output
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 6

How it works

The light sensor and a 10k resistor make a voltage divider. The resistor goes to 3.3 V and the sensor goes to ground, with GPIO 36 (VP) in the middle. In the dark the sensor's resistance climbs, so the voltage on the pin rises.

The program reads that voltage as a number from 0 to 1023. It uses the same number as the PWM duty for the LED on GPIO 25. So darker means a brighter lamp. It also prints the number every 200 ms.

Parts list

6 parts, plus the jumper wires. They retail for about $10.30; on Mokxi they're free.

Kits that can build it

These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 10k Ω pin 2ESP32-DevKitC V4 pin VP
Light sensor pin 1ESP32-DevKitC V4 pin VP
Resistor, 220 Ω pin 1ESP32-DevKitC V4 pin 25
Resistor, 10k Ω pin 13.3 V
Light sensor pin 2Ground
LED, red pin AResistor, 220 Ω pin 2
LED, red pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · ESP32-DevKitC V4
# Night lamp: a photoresistor under a 10k from 3.3 V. In the dark its
# resistance climbs, the voltage on GPIO 36 rises, and the LED on
# GPIO 25 gets brighter.
from machine import ADC, Pin, PWM
import time

light = ADC(Pin(36))
light.atten(ADC.ATTN_11DB)
light.width(ADC.WIDTH_10BIT)
led = PWM(Pin(25), freq=1000)

while True:
    value = light.read()
    led.duty(value)
    print("light sensor", value)
    time.sleep_ms(200)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The lamp does the opposite of what you want. This project
    In this wiring, dark gives a higher reading. To flip it, use 1023 - value for the duty.
  • The reading stays stuck at one number. This project
    Check the divider: the 10k to 3.3 V, the sensor to ground, and VP at the point where they meet.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The reading barely changes, or sits stuck near 0 or the top of the range. Light sensor
    An LDR is a resistor, so use it in a divider with a fixed resistor, about 10k, and read the middle node. Without the divider the analog pin sees nothing useful.
  • Readings go the wrong way: higher in the dark. Light sensor
    Which side the LDR sits on decides the direction. LDR on the supply side gives a higher reading in the light. LDR on the ground side gives a lower reading in the light. Swap them if needed.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.