Skip to content

Instantly share code, notes, and snippets.

@elktros
Created March 12, 2021 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/8388795bd8bf442edf4d88d0e81054fd to your computer and use it in GitHub Desktop.
Save elktros/8388795bd8bf442edf4d88d0e81054fd to your computer and use it in GitHub Desktop.
ESP32 PWM LED Fading.
const int LEDPin = 16; /* GPIO16 */
int dutyCycle;
/* Setting PWM Properties */
const int PWMFreq = 5000; /* 5 KHz */
const int PWMChannel = 0;
const int PWMResolution = 10;
const int MAX_DUTY_CYCLE = (int)(pow(2, PWMResolution) - 1);
void setup()
{
ledcSetup(PWMChannel, PWMFreq, PWMResolution);
/* Attach the LED PWM Channel to the GPIO Pin */
ledcAttachPin(LEDPin, PWMChannel);
}
void loop()
{
/* Increasing the LED brightness with PWM */
for(dutyCycle = 0; dutyCycle <= MAX_DUTY_CYCLE; dutyCycle++)
{
ledcWrite(PWMChannel, dutyCycle);
delay(3);
//delayMicroseconds(100);
}
/* Decreasing the LED brightness with PWM */
for(dutyCycle = MAX_DUTY_CYCLE; dutyCycle >= 0; dutyCycle--)
{
ledcWrite(PWMChannel, dutyCycle);
delay(3);
//delayMicroseconds(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment