← ELECTRONICS

ESP32 Basics: LED, Button, LED Bar, PWM and RGB LED

What this is

Notes to self after working through Chapters 1–5 of the Freenove ESP32-WROVER starter kit tutorial (Arduino IDE). The board is plugged into the GPIO extension board on a breadboard and powered and programmed over USB-C. This post just records what I built and the points I want to remember.

What I did
Ch.ProjectNew idea
1Blinkdigital output, current-limiting resistor
2Button & LED / table lampdigital input, pull-up resistor, debouncing
3LED bar flowing lightarrays and loops over many pins
4Breathing LED / meteor lightPWM (LEDC), duty cycle
5RGB LED colours3-channel PWM, common anode

Ch.1 Blink

Circuit: GPIO2 → 220 Ω → LED (long leg = anode +) → GND. Code: set the pin as an output in setup(), then switch it HIGH/LOW in loop(). Every Arduino sketch is setup() (runs once) plus loop() (runs forever).

#define PIN_LED 2

void setup() {
  pinMode(PIN_LED, OUTPUT);
}

void loop() {
  digitalWrite(PIN_LED, HIGH);  // on
  delay(1000);
  digitalWrite(PIN_LED, LOW);   // off
  delay(1000);
}
LED circuit on the breadboard
Blink circuit (LED off)
Blue LED turned on
Blink circuit (LED on)

Why the resistor matters: a GPIO pin outputs 3.3 V, and an LED drops a roughly fixed forward voltage $V_f$. The resistor takes the rest and sets the current:

\[I = \frac{V_{\mathrm{GPIO}} - V_f}{R}\]

With a red LED ($V_f \approx 2$ V) and 220 Ω this gives $I \approx 6$ mA, which is safe. Blue LEDs have $V_f \approx 3$ V, so the current is smaller and the LED a bit dimmer, but it is still clearly visible. Without the resistor the current is limited only by the pin, which can damage both the LED and the ESP32.

Ch.2 Button & LED

The button is wired between GPIO13 and GND, with a 10 kΩ pull-up resistor to 3.3 V. When it is not pressed, the pin is pulled up to HIGH. When it is pressed, the pin is shorted to GND and reads LOW. So the logic is active-low: pressed = LOW. Without a pull-up (or pull-down) the input pin floats and reads random values.

#define PIN_LED    2
#define PIN_BUTTON 13

void setup() {
  pinMode(PIN_LED, OUTPUT);
  pinMode(PIN_BUTTON, INPUT);   // external pull-up; INPUT_PULLUP uses the internal one
}

void loop() {
  if (digitalRead(PIN_BUTTON) == LOW) digitalWrite(PIN_LED, HIGH);
  else                                digitalWrite(PIN_LED, LOW);
}

Table lamp version: each press toggles the LED. Mechanical contacts bounce for a few ms, so one press can look like several presses. The fix is to check again after about 20 ms, then wait until the button is released:

void loop() {
  if (digitalRead(PIN_BUTTON) == LOW) {
    delay(20);                                   // debounce
    if (digitalRead(PIN_BUTTON) == LOW) {
      digitalWrite(PIN_LED, !digitalRead(PIN_LED));  // toggle
    }
    while (digitalRead(PIN_BUTTON) == LOW);      // wait for release
  }
}
Push button and LED circuit
Button + LED (table lamp)

Ch.3 LED bar flowing light

A 10-segment LED bar, with each segment on its own GPIO and its own 220 Ω resistor. The pins go in an array, and a for-loop lights them one by one, forward and then backward.

byte ledPins[] = {15, 2, 0, 4, 5, 18, 19, 21, 22, 23};
int ledCount = sizeof(ledPins);

void setup() {
  for (int i = 0; i < ledCount; i++) pinMode(ledPins[i], OUTPUT);
}

void loop() {
  for (int i = 0; i < ledCount; i++) {          // forward
    digitalWrite(ledPins[i], HIGH); delay(100);
    digitalWrite(ledPins[i], LOW);
  }
  for (int i = ledCount - 1; i >= 0; i--) {     // backward
    digitalWrite(ledPins[i], HIGH); delay(100);
    digitalWrite(ledPins[i], LOW);
  }
}

Note: on the WROVER, GPIO16/17 are used by the PSRAM, so they are skipped. Some other pins (0, 2, 12, 15) are strapping pins, which set the boot mode. They work as outputs, but if the board refuses to boot or upload, suspect what is connected to them.

LED bar graph with one segment lit
LED bar flowing light

Ch.4 Analog & PWM

A digital pin is only on or off, but switching it fast and changing the fraction of time it is on (the duty cycle $D$) makes the LED look dimmer or brighter. The eye averages it out:

\[V_{\mathrm{avg}} = D \cdot 3.3\ \mathrm{V}, \qquad D = \frac{\text{duty}}{2^{\text{bits}} - 1}\]

On the ESP32 this is the LEDC peripheral: choose a frequency (e.g. 1 kHz, fast enough not to flicker) and a resolution (8 bits → duty 0–255). Breathing LED = ramp the duty up and down:

#define PIN_LED 2
#define CHN     0
#define FRQ     1000
#define PWM_BIT 8

void setup() {
  ledcAttachChannel(PIN_LED, FRQ, PWM_BIT, CHN);  // core 3.x
}

void loop() {
  for (int i = 0; i < 255; i++) { ledcWrite(PIN_LED, i); delay(10); }
  for (int i = 255; i > 0; i--) { ledcWrite(PIN_LED, i); delay(10); }
}

API gotcha: the older ESP32 Arduino core (2.x) used ledcSetup(chn, freq, bits) + ledcAttachPin(pin, chn) + ledcWrite(chn, duty). Core 3.x replaced these with ledcAttach / ledcAttachChannel, and ledcWrite now takes the pin. Old sample code fails to compile on the new core, so check the core version first. Meteor light (4.2) = the LED bar from Ch.3, but each segment gets a decreasing duty so a bright head is followed by a fading tail.

Ch.5 RGB LED

An RGB LED is three LEDs (R, G, B) in one package, each with its own resistor and PWM channel. Mixing the three duties gives any colour. The kit's LED is common anode: the shared leg goes to 3.3 V, and a colour lights when its pin is pulled LOW. So the logic is inverted: write 255 − value.

const byte ledPins[] = {15, 2, 4};   // R, G, B
const byte chns[]    = {0, 1, 2};

void setup() {
  for (int i = 0; i < 3; i++) ledcAttachChannel(ledPins[i], 1000, 8, chns[i]);
}

void setColor(int r, int g, int b) {
  ledcWrite(ledPins[0], 255 - r);   // common anode -> invert
  ledcWrite(ledPins[1], 255 - g);
  ledcWrite(ledPins[2], 255 - b);
}

void loop() {
  setColor(random(256), random(256), random(256));  // random colour
  delay(200);
}

The gradient version steps a 0–255 value through a colour wheel function, which maps it smoothly R → G → B → R, instead of picking random values.

RGB LED glowing green
RGB LED

Takeaways

Always put a resistor in series with an LED: $R = (3.3 - V_f)/I$. Input pins need a pull-up or pull-down, otherwise they float; the button here is active-low. Buttons bounce, so debounce them. PWM gives fake analog: brightness ∝ duty. Check common anode vs common cathode, because it inverts the logic. Mind the WROVER pin limits (16/17 used by PSRAM, strapping pins) and the LEDC API change in core 3.x.

Reference

Freenove ESP32-WROVER Board (tutorial & code) ↗github.com Arduino-ESP32 LEDC API ↗docs.espressif.com