XIAO S3 knob
The XIAO knob circuit on the ESP32-S3: D0 is A0 on GPIO 1, and the count is printed as the converter gives it. The whole build, a Seeed XIAO ESP32-S3 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.15. On Mokxi they're free.
What you'll learn
- Reading a knob with analogRead
- A 12-bit reading from 0 to 4095
- Scaling a reading down to 0 to 255
- PWM brightness with analogWrite
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Seeed XIAO ESP32-S3
- Parts: 5
How it works
The potentiometer sits between 3V3 and GND, and its middle pin goes to D0, which is analog input A0 (GPIO 1). Turning the knob changes the voltage there. The ADC turns it into a number from 0 to 4095.
The sketch shifts that number right by 4 bits to get 0 to 255, and uses it to set the PWM brightness of the green LED on D10. It also prints the raw count every 200 ms.
Parts list
5 parts, plus the jumper wires. They retail for about $13.15; on Mokxi they're free.
- 1 × Seeed XIAO ESP32-S3
- 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, greenIn 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 | Seeed XIAO ESP32-S3 pin D0 |
| Potentiometer pin 3 | Seeed XIAO ESP32-S3 pin GND |
| LED, green pin C | Seeed XIAO ESP32-S3 pin GND |
| Potentiometer pin 1 | Seeed XIAO ESP32-S3 pin 3V3 |
| Resistor, 220 Ω pin 1 | Seeed XIAO ESP32-S3 pin D10 |
| LED, green 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 A0 sets the brightness of an LED on D10.
//
// A0 is GPIO 1, ADC1 channel 0. The converter is twelve bits, 0 to
// 4095, and at its default 12 dB attenuation a real S3 saturates at about
// 3.1 V, so a pot across the 3.3 V rail reads 4095 a little before the end.
// The sketch prints the count it reads.
const int KNOB = A0;
const int LED = D10;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
int raw = analogRead(KNOB);
analogWrite(LED, raw >> 4);
Serial.print("A0 ");
Serial.println(raw);
delay(200);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts the serial port at 115200 baud and makes LED (D10) an output. The knob pin KNOB needs no setup.
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
}loop sketch.ino
analogRead(KNOB) returns a count from 0 to 4095. raw >> 4 divides it by 16, giving 0 to 255, which analogWrite uses as the LED's brightness. It then prints the raw count and waits 200 ms.
void loop() {
int raw = analogRead(KNOB);
analogWrite(LED, raw >> 4);
Serial.print("A0 ");
Serial.println(raw);
delay(200);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED reaches full brightness before the knob is fully turned. This project
A real S3 reads about 4095 a little before 3.3 V, so this is expected. - The LED never changes. This project
Check the potentiometer's middle pin goes to D0, and the LED resistor goes to D10. - 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.