🛠 Graduer une LED avec le PWM d'un timer

GD32F103 (GigaDevice) — chip + Blue-Pill-compatible boards · intermediaire · Schéma : IMG-gd32f103-intermediaire-1-pwm-led.svg — LED externe avec résistance 330 ohms sur PA6 pilotée en PWM par un timer du GD32F103C8, signal dent de scie annoté

Objectives

Use the PWM output (MDP in French) of one of the GD32F103's 16-bit timers to gradually vary the brightness of an LED. The GD32F103C8 features up to 10 general-purpose 16-bit timers, 2 advanced timers, and 2 basic timers; the community Arduino core maps analogWrite to the timer channels. You will understand the principle of duty cycle and the clock constraint of APB1 (54 MHz max) / APB2 (108 MHz max).

Hardware

Complete Code

// pwm_led.ino — GD32F103C8, core CommunityGD32Cores
const uint8_t PWM_PIN = PA6;   // sortie timer (cf. tableau AF du core) > 

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

void loop {
  for (int p = 0; p <= 255; p += 4) {   // rampe montante
    analogWrite(PWM_PIN, p);
    Serial.println(p);
    delay(15);
  }
  for (int p = 255; p >= 0; p -= 4) {   // rampe descendante
    analogWrite(PWM_PIN, p);
    delay(15);
  }
}

Expected Verification

The external LED smoothly ramps up and down in brightness (breathing effect), without abrupt steps. With a logic analyzer or oscilloscope, you see a square wave of constant frequency whose duty cycle follows the 0-255 ramp. The onboard PC13 LED does not blink: it is not on this channel.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials