ESP32 knob
A pot on GPIO 32 dimming an LED, with the raw reading printed: at 11 dB a real ESP32 reads about 1890 at half the rail and 4095 from about 3.1 V. The whole build, an ESP32 DevKit 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 $14.65. On Mokxi they're free.
What you'll learn
- Reading a knob with a 12-bit analog input
- Dimming an LED with
analogWrite - Rescaling a range with
map - Why a real ESP32 reading is not perfectly linear
- Difficulty: Beginner
- Time: About 15 minutes
- Board: ESP32 DevKit V1
- Parts: 5
How it works
A potentiometer is wired across 3V3 and ground, and its middle pin goes to GPIO32. Turning the knob changes the voltage on that pin. The ESP32 measures it with its analog-to-digital converter and gives a number from 0 to 4095.
The sketch scales that number down to 0 to 255 and uses it to set the brightness of the LED on GPIO4. It also prints both numbers to the serial monitor every 200 ms. The reading is not perfectly straight: at the default setting, about 1890 is half the rail and 4095 arrives near 3.1 V.
Parts list
5 parts, plus the jumper wires. They retail for about $14.65; on Mokxi they're free.
- 1 × ESP32 DevKit V1
- 1 × Full-size breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
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 pin | Connects to |
|---|---|
| Potentiometer pin 2 | ESP32 DevKit V1 pin D32 |
| Potentiometer pin 1 | ESP32 DevKit V1 pin GND |
| LED, red pin C | ESP32 DevKit V1 pin GND |
| Resistor, 220 Ω pin 1 | ESP32 DevKit V1 pin D4 |
| Potentiometer pin 3 | ESP32 DevKit V1 pin 3V3 |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Knob: a potentiometer on GPIO32 sets how brightly the LED on GPIO4 burns.
//
// GPIO32 is ADC1 channel 4. At the default 11 dB attenuation a real ESP32's
// reading bends: a pot across a 3.3 V rail reads about 1890 in the middle and
// 4095 from about 3.1 V, before the end of its travel. The sketch prints what
// it read so the number is not a mystery.
const int kKnob = 32;
const int kLed = 4;
void setup()
{
Serial.begin(115200);
pinMode(kLed, OUTPUT);
}
void loop()
{
int raw = analogRead(kKnob);
int duty = map(raw, 0, 4095, 0, 255);
analogWrite(kLed, duty);
Serial.print("knob ");
Serial.print(raw);
Serial.print(" -> duty ");
Serial.println(duty);
delay(200);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 115200 baud so the readings can be printed, and sets the LED pin kLed (GPIO4) as an output.
void setup()
{
Serial.begin(115200);
pinMode(kLed, OUTPUT);
}loop sketch.ino
Reads the knob with analogRead(kKnob), then uses map to turn 0 to 4095 into a duty of 0 to 255. analogWrite sets the LED brightness from that duty. It prints the raw and scaled values, then waits 200 ms.
void loop()
{
int raw = analogRead(kKnob);
int duty = map(raw, 0, 4095, 0, 255);
analogWrite(kLed, duty);
Serial.print("knob ");
Serial.print(raw);
Serial.print(" -> duty ");
Serial.println(duty);
delay(200);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The reading jumps to 4095 before the knob reaches the end This project
That is normal for a real ESP32. At this setting the input tops out near 3.1 V. - The reading never changes This project
The wiper (middle pin 2) must go to GPIO32, with 3V3 on pin 3 and ground on pin 1. - 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.