Built-in project · Seeed XIAO ESP32-C3

XIAO C3 knob

A potentiometer on D0 dims an LED on D10, using the XIAO’s own pin names rather than the chip’s GPIO numbers. The whole build, a Seeed XIAO ESP32-C3 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 $10.65. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A board's pin names versus GPIO numbers
  • A 12-bit ADC and scaling it down to 8 bits
  • Calibrated millivolt readings
  • Printing on a timer with millis
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Seeed XIAO ESP32-C3
  • Parts: 5

How it works

A potentiometer sits between 3V3 and ground, with its middle pin on D0. An LED on D10 is wired through a 220 ohm resistor to ground. The sketch uses the XIAO's own pin names, not the chip's GPIO numbers.

The ADC reads the knob as 0 to 4095. Shifting that right by four bits gives 0 to 255, which analogWrite uses for the LED's brightness. Every 200 ms the sketch also prints the knob's voltage in millivolts, using the core's calibrated reading.

Parts list

5 parts, plus the jumper wires. They retail for about $10.65; on Mokxi they're free.

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 2Seeed XIAO ESP32-C3 pin D0
Potentiometer pin 3Seeed XIAO ESP32-C3 pin GND
LED, green pin CSeeed XIAO ESP32-C3 pin GND
Potentiometer pin 1Seeed XIAO ESP32-C3 pin 3V3
Resistor, 220 Ω pin 1Seeed XIAO ESP32-C3 pin D10
LED, green pin AResistor, 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.

sketch.ino · Seeed XIAO ESP32-C3
// Knob: a potentiometer on D0 sets the brightness of an LED on D10.
//
// D0 is A0, which is GPIO 2 and ADC1 channel 2. The converter is twelve bits,
// 0 to 4095, and at its default 11 dB attenuation the count is not a straight
// line to 3.3 V (a C3 reads 4095 from about 2.9 V), so the sketch asks the core
// for calibrated millivolts rather than scaling the count itself.
// D3 is labelled A3 on the board too, but it is on ADC2, which Mokxi does not
// model, so use D0, D1 or D2 for anything analogue.

const int KNOB = A0;
const int LED = D10;

unsigned long lastReport = 0;

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
}

void loop() {
  int raw = analogRead(KNOB);
  analogWrite(LED, raw >> 4);

  unsigned long now = millis();
  if (now - lastReport >= 200) {
    lastReport = now;
    long mv = (long)analogReadMilliVolts(KNOB);
    Serial.print("D0 ");
    Serial.print(mv);
    Serial.println(" mV");
  }
  delay(10);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial at 115200 and sets LED (D10) as an output. The knob pin needs no setup for analogRead.

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
}

loop sketch.ino

Reads KNOB and writes raw >> 4 to the LED, which scales 0 to 4095 down to 0 to 255. Then it checks millis(). If 200 ms have passed since lastReport, it prints analogReadMilliVolts(KNOB) as a voltage. A 10 ms delay ends each pass.

void loop() {
  int raw = analogRead(KNOB);
  analogWrite(LED, raw >> 4);

  unsigned long now = millis();
  if (now - lastReport >= 200) {
    lastReport = now;
    long mv = (long)analogReadMilliVolts(KNOB);
    Serial.print("D0 ");
    Serial.print(mv);
    Serial.println(" mV");
  }
  delay(10);
}

Troubleshooting

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

  • The knob does not work on D3 This project
    D3 is on ADC2, which this simulator does not model. Use D0, D1 or D2 for analog input.
  • The LED is always dim or always bright This project
    Check that the potentiometer's middle pin is on D0, with 3V3 and GND on the outer pins.
  • 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.