Welcome to DFRobot: www.DFRobot.com.cn 23 / 49
#define LED_PIN D9
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// value has to be between 0 and valueMax
void ledcAnalogWrite
(uint32_t value, uint32_t valueMax = 255) {
// calculate duty
uint32_t duty = (LEDC_BASE_FREQ / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(LEDC_CHANNEL_0, duty);
}
void setup() {
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
}
void loop() {
ledcAnalogWrite(brightness);
brightness += fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
Please click verify/compile to review and please upload it after the confirmation.
Once you click Upload, IDE will send code to FireBeetle Board-ESP32.
You can see the breath of L LED light when you uploaded the codes.
Let’s review the codes and hardware and check the way they work.
Knowledge learning
What is PWM (pulse-width modulation) ?
Pulse-width modulation (PWM), or pulse-duration modulation (PDM) is a modulation technique
used to encode messages into a pulsing signal. MCU (Microcomtroller) controls the on-off of
switching devices and get a series of pulses has equally amplitude. And use these pulses to
replace sinusoid or other waveforms in need, shown as below.