Built-in project · ESP32-DevKitC V4

MicroPython dimmer: a knob on GPIO 36, an LED on 25, on an ESP32-DevKitC V4

MicroPython on an ESP32: the ADC reads a potentiometer on GPIO 36 and its reading is the PWM duty of the LED on GPIO 25. The whole build, an ESP32-DevKitC V4 and 3 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.65. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a knob with the ADC
  • Setting the ADC range and bit width
  • PWM duty as LED brightness
  • Filtering printed output with a threshold
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 5

How it works

The potentiometer sits between ground and 3.3 V, and its middle pin goes to GPIO 36 (the VP pin). Turning the knob changes the voltage there. The ADC turns it into a number from 0 to 1023.

That same number sets the PWM duty on GPIO 25 at 1000 Hz, so the LED gets brighter as you turn the knob up. The program prints the value only when it moves by more than 8.

Parts list

5 parts, plus the jumper wires. They retail for about $10.65; 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
Potentiometer pin 2ESP32-DevKitC V4 pin VP
Resistor, 220 Ω pin 1ESP32-DevKitC V4 pin 25
Potentiometer pin 1Ground
Potentiometer pin 33.3 V
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
# Soft light: the potentiometer on GPIO 36 sets the LED's brightness.
# The ADC reads 0 to 1023 (10 bits) across 0 to 3.3 V, and that number
# is the PWM duty on GPIO 25.
from machine import ADC, Pin, PWM
import time

pot = ADC(Pin(36))
pot.atten(ADC.ATTN_11DB)          # the whole 0 to 3.3 V range
pot.width(ADC.WIDTH_10BIT)        # 0 to 1023
led = PWM(Pin(25), freq=1000)
last = -1

while True:
    value = pot.read()
    led.duty(value)
    if abs(value - last) > 8:
        print("knob", value)
        last = value
    time.sleep_ms(10)

Troubleshooting

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

  • The reading maxes out early or looks cut off. This project
    The line pot.atten(ADC.ATTN_11DB) opens the full 0 to 3.3 V range. Keep it.
  • The LED does not change. This project
    Check that the knob's middle pin goes to VP (GPIO 36) and the LED resistor goes to pin 25.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • 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 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.