Projects / ESP32 DevKit V1 encoder dimmer

ESP32 DevKit V1 encoder dimmer

Beginner1.8 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. ESP32

    Wire the encoder

    + to 3V3, GND to GND, CLK to GPIO32, DT to GPIO33, SW to GPIO25.

  2. ESP3213.4 mA

    Wire the LED

    Long leg through 330 ohms to GPIO18, short leg to GND.

  3. ESP3213.4 mA

    Upload the sketch

    The LED comes on at half brightness.

  4. ESP32

    Turn the knob right

    Brighter, and the number prints in the Serial Monitor at 115200.

  5. ESP32

    Turn it left

    Dimmer, down to off.

  6. ESP32

    Press the knob

    The light goes off, press again for the same level.

In short

How do you build an ESP32 DevKit V1 encoder dimmer?

An ESP32 DevKit V1 reads a KY-040 rotary encoder and sets the brightness of an LED with PWM, with a push to switch it off and on. You need ESP32 DevKit V1 30-pin (CH340), Rotary encoder module (KY-040), LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 110 minutes.

Level
Beginner
Time
About 110 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Encoder +ESP32 3V3Power at 3.3 V
Encoder GNDESP32 GNDCommon ground
Encoder CLKESP32 GPIO32Channel A of the encoder
Encoder DTESP32 GPIO33Channel B of the encoder
Encoder SWESP32 GPIO25Push switch, low when pressed
LED long leg330 ohm resistor to ESP32 GPIO18PWM output
LED short legESP32 GNDReturn

Code

A rotary encoder is a knob that turns without end stops and sends two square-wave signals, A and B, a quarter of a cycle apart. Which of the two changes first tells you the direction. The program reads both lines every half a millisecond and keeps the last two readings of each, forming a four-bit number, which it looks up in a table that returns plus one, minus one or zero. This method also copes with contact bounce, because a bounce that goes back and forth adds and takes away the same amount. Four small steps make one click of the knob. The brightness is set with PWM, switching the LED on and off five thousand times a second and changing the fraction of the time it is on. The eye does not see brightness in a straight line, so the duty is the square of the level, which makes the steps look even. A push on the knob switches the light off and on and remembers the level. The module has its own pull-up resistors, so power it from 3.3 volts, not 5, to keep the signals safe for the board. The resistor protects the LED.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int PIN_A = 32, PIN_B = 33, PIN_SW = 25, LED = 18;
const int LEVELS = 20;                             // 0 to 20 steps of the knob

int level = 10;
bool lightOn = true;
uint8_t state = 0;
int accum = 0;
bool lastSw = HIGH;
unsigned long lastPress = 0;

// A quadrature table: each row of four reads gives one step in one direction or the other.
const int8_t TABLE[16] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };

void setDuty(int duty) {
#if ESP_ARDUINO_VERSION_MAJOR >= 3
  ledcWrite(LED, duty);
#else
  ledcWrite(0, duty);
#endif
}

void update() {
  int duty = 0;
  if (lightOn) {
    duty = (level * level * 255) / (LEVELS * LEVELS);   // squared, so the steps look even to the eye
  }
  setDuty(duty);
}

void setup() {
  pinMode(PIN_A, INPUT_PULLUP);
  pinMode(PIN_B, INPUT_PULLUP);
  pinMode(PIN_SW, INPUT_PULLUP);
#if ESP_ARDUINO_VERSION_MAJOR >= 3
  ledcAttach(LED, 5000, 8);
#else
  ledcSetup(0, 5000, 8);
  ledcAttachPin(LED, 0);
#endif
  Serial.begin(115200);
  update();
}

void loop() {
  state = ((state << 2) | (digitalRead(PIN_A) << 1) | digitalRead(PIN_B)) & 0x0F;
  accum += TABLE[state];
  if (accum >= 4) {                                // four small steps make one click
    accum = 0;
    if (level < LEVELS) level++;
    update();
    Serial.println(level);
  } else if (accum <= -4) {
    accum = 0;
    if (level > 0) level--;
    update();
    Serial.println(level);
  }

  bool sw = digitalRead(PIN_SW);
  if (sw == LOW && lastSw == HIGH && millis() - lastPress > 200) {
    lightOn = !lightOn;
    lastPress = millis();
    update();
  }
  lastSw = sw;
  delayMicroseconds(500);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
StartLED at half brightness
Turn rightBrighter, one step per click
Turn leftDimmer
Turn fastNo steps missed
Press the knobOff, then on at the same level

If it does not work

Goes the wrong way

Swap the CLK and DT wires.

Skips or jumps

Check the encoder is on 3.3 V and the wires are firm.

LED never lights

Check the leg direction and that the resistor is on GPIO18.

Does not compile

Choose the ESP32 Dev Module board and the esp32 package.

Press does nothing

Check SW is on GPIO25 and GND is connected.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 DevKit V1 encoder dimmer?

ESP32 DevKit V1 30-pin (CH340), Rotary encoder module (KY-040), LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points.

How long does the ESP32 DevKit V1 encoder dimmer take to build?

About 110 minutes for a beginner maker, in 6 steps.

How do I know the ESP32 DevKit V1 encoder dimmer works?

Start: LED at half brightness. Turn right: Brighter, one step per click. Turn left: Dimmer.

Goes the wrong way: what should I check?

Swap the CLK and DT wires.

Skips or jumps: what should I check?

Check the encoder is on 3.3 V and the wires are firm.

LED never lights: what should I check?

Check the leg direction and that the resistor is on GPIO18.

Does not compile: what should I check?

Choose the ESP32 Dev Module board and the esp32 package.