ESP32 Blink + Potentiometer: Wiring, Code and Simulator
To build this ESP32 Blink + Potentiometer project you need an ESP32 DevKit and a 220 Ω resistor, a red LED and a potentiometer. GPIO2 (the blue on-board LED) and GPIO4 blink; a potentiometer on GPIO34 sets the speed through the 12-bit ADC. Make the 6 connections in the wiring table, upload the MicroPython code, and turn the knob while it runs and watch the serial monitor: pot.read() goes 0 to 4095 on the ESP32.
Open this circuit in the free ESP32 DevKit simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | ESP32 DevKit | the microcontroller board |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | red LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
Wiring
| # | From | To |
|---|---|---|
| 1 | ESP32 DevKit D4 | 220 Ω resistor leg 1 |
| 2 | 220 Ω resistor leg 2 | red LED anode (A, long leg) |
| 3 | ESP32 DevKit GND | red LED cathode (C, short leg) |
| 4 | ESP32 DevKit 3V3 | Potentiometer VCC |
| 5 | ESP32 DevKit GND | Potentiometer GND |
| 6 | ESP32 DevKit D34 | Potentiometer SIG |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the ESP32 DevKit. No install or account is needed.
- Add the parts. Add a 220 Ω resistor, a red LED and a potentiometer from the parts palette.
- Wire 1. Connect ESP32 DevKit D4 to 220 Ω resistor leg 1.
- Wire 2. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
- Wire 3. Connect ESP32 DevKit GND to red LED cathode (C, short leg).
- Wire 4. Connect ESP32 DevKit 3V3 to Potentiometer VCC.
- Wire 5. Connect ESP32 DevKit GND to Potentiometer GND.
- Wire 6. Connect ESP32 DevKit D34 to Potentiometer SIG.
- Paste the MicroPython code. Paste the MicroPython code below into the code editor.
- Run it. Press Run. Turn the knob while it runs and watch the serial monitor: pot.read() goes 0 to 4095 on the ESP32.
MicroPython code
# ESP32 blink and a knob. The blue on-board LED is GPIO2; a red LED is on GPIO4.
# The potentiometer on GPIO34 sets the blink speed (ESP32 ADC: 0..4095).
from machine import Pin, ADC
import time
onboard = Pin(2, Pin.OUT)
led = Pin(4, Pin.OUT)
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB) # full 0..3.3 V range
while True:
raw = pot.read()
delay = 100 + raw // 8 # 100 ms .. about 610 ms
print("pot", raw, "delay", delay)
onboard.value(not onboard.value())
led.value(onboard.value())
time.sleep_ms(delay)
What to try
Turn the knob while it runs and watch the serial monitor: pot.read() goes 0 to 4095 on the ESP32.
Frequently asked questions
How do I wire the Blink + Potentiometer project on an ESP32 DevKit?
Make 6 connections: ESP32 DevKit D4 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to red LED anode (A, long leg); ESP32 DevKit GND to red LED cathode (C, short leg); ESP32 DevKit 3V3 to Potentiometer VCC; ESP32 DevKit GND to Potentiometer GND; ESP32 DevKit D34 to Potentiometer SIG.
What parts do I need for this ESP32 project?
An ESP32 DevKit and a 220 Ω resistor, a red LED and a potentiometer.
Can I simulate this ESP32 project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and MicroPython code in your browser for free, with no install and no hardware. MicroPython runs on an emulated core, with Wi-Fi served by a virtual network.
What does the code do?
GPIO2 (the blue on-board LED) and GPIO4 blink; a potentiometer on GPIO34 sets the speed through the 12-bit ADC.