ohmio

Circuits / Microcontrollers

ESP32-C3: a potentiometer dims an LED (ADC and PWM)

analogRead turns a voltage into 0…4095 (12 bits, up to about 2.5 V); analogWrite sets the share of time the pin is high

ESP32-C3: a potentiometer dims an LED (ADC and PWM), simulated on a breadboard

Tap a part to see its voltages and currents. It is a model, not a measurement.

What do you need?

How does an ESP32-C3 dim an LED with a potentiometer?

analogRead(4) turns the voltage on GPIO4 into a number from 0 to 4095, the 12 bits of the ESP32-C3's converter. There is a catch: with its usual setting the converter tops out at about 2.5 V, while the potentiometer goes up to 3.3 V. So the last quarter of the turn always reads 4095, and only the first three quarters dim the LED. With the knob at the middle, the wiper is at 1.65 V and the reading is about 2700. analogWrite(3, reading / 16) maps that onto 0 to 255, here about 168, so GPIO3 is high about two thirds of the time. The LED receives quick pulses through R1 (220 Ω) and your eye averages them into a steady brightness. The program prints each reading to the serial monitor every 200 ms.

Values from the Protoboard ISO simulation: a model, not a bench measurement.