Created
March 12, 2021 15:12
-
-
Save elktros/8388795bd8bf442edf4d88d0e81054fd to your computer and use it in GitHub Desktop.
ESP32 PWM LED Fading.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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