Skip to content

Instantly share code, notes, and snippets.

@thankthemaker
Created December 22, 2020 15:28
Show Gist options
  • Save thankthemaker/0e10331ac1fe09ae0db7d22dcbda984a to your computer and use it in GitHub Desktop.
Save thankthemaker/0e10331ac1fe09ae0db7d22dcbda984a to your computer and use it in GitHub Desktop.
Fading with PWM on ESP32
const byte led_gpio = 27; // the PWM pin the LED is attached to
const byte led_gpio2 = 26; // the PWM pin the LED is attached to
int MAX = 255;
int DELAY = 50;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
ledcAttachPin(led_gpio, 0); // assign a led pins to a channel
ledcAttachPin(led_gpio2, 1); // assign a led pins to a channel
// Initialize channels
// channels 0-15, resolution 1-16 bits, freq limits depend on resolution
// ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
ledcSetup(0, 4000, 8); // 12 kHz PWM, 8-bit resolution
ledcSetup(1, 4000, 8); // 12 kHz PWM, 8-bit resolution
}
// the loop routine runs over and over again forever:
void loop() {
for (int i=0; i<MAX; i=i+5) {
ledcWrite(0, i); // set the brightness of the LED
delay(DELAY);
}
for (int i=MAX; i>0; i=i-5) {
ledcWrite(0, i); // set the brightness of the LED
delay(DELAY);
}
delay(500);
for (int i=0; i<MAX; i=i+5) {
ledcWrite(1, i); // set the brightness of the LED
delay(DELAY);
}
for (int i=MAX; i>0; i=i-5) {
ledcWrite(1, i); // set the brightness of the LED
delay(DELAY);
}
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment