Home » fade an LED using an ESP32

fade an LED using an ESP32

by shedboy71

This example shows how easy it is to fade an LED using an ESP32

The code is fairly straightforward

ledcSetup(ledChannel, freq, resolution) – this sets a channel of which there are 16 available, a frequency and a resolution which can be between 1 and 16 to the ledcsetup function. The frequency is a mystery at the moment but i wouldn't set it too high.

We now need to attach this to a pin where the led will be connected via ledcAttachPin(A13, ledChannel) – I chose A13 or 15 as its numbered on my LOLIN32

If you open the pins_arduino.h you will see where I get the A13 from

static const uint8_t A0 = 36;
static const uint8_t A3 = 39;
static const uint8_t A4 = 32;
static const uint8_t A5 = 33;
static const uint8_t A6 = 34;
static const uint8_t A7 = 35;
static const uint8_t A10 = 4;
static const uint8_t A11 = 0;
static const uint8_t A12 = 2;
static const uint8_t A13 = 15;
static const uint8_t A14 = 13;
static const uint8_t A15 = 12;
static const uint8_t A16 = 14;
static const uint8_t A17 = 27;
static const uint8_t A18 = 25;
static const uint8_t A19 = 26;

The two for loops fade the led in and out

Code

[cpp]

int freq = 10000;
int ledChannel = 0;
int resolution = 8;

void setup() {

ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(A13, ledChannel);

}

void loop()
{
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++)
{
ledcWrite(ledChannel, dutyCycle);
delay(5);
}

for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle–)
{
ledcWrite(ledChannel, dutyCycle);
delay(5);
}

}

[/cpp]

You may also like

Leave a Comment

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.