By ran | 30 September 2024 | 0 Comments
HOW to Make a Breathing LED with ESP32
Overview
A“breathing LED”is a phenomenon where an LED’s brightness smoothly changes from dark to bright and back to dark, continuing to do so and giving the illusion of an LED“breathing. This phenomenon is similar to a lung breathing in and out. So how to control LED’s brightness? We need to take advantage of PWM.
Components
Connection Diagram
Test Code
//**********************************************************************
/*
* Filename : Breathing Led
* Description : Make led light fade in and out, just like breathing.
* Auther : http//www.keyestudio.com
*/
#define PIN_LED 0 //define the led pin
#define CHN 0 //define the pwm channel
#define FRQ 1000 //define the pwm frequency
#define PWM_BIT 8 //define the pwm precision
void setup() {
ledcSetup(CHN, FRQ, PWM_BIT); //setup pwm channel
ledcAttachPin(PIN_LED, CHN); //attach the led pin to pwm channel
}
void loop() {
for (int i = 0; i < 255; i++) { //make light fade in
ledcWrite(CHN, i);
delay(10);
}
for (int i = 255; i > -1; i--) { //make light fade out
ledcWrite(CHN, i);
delay(10);
}
}
//*************************************************************************************
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on,we will see that the LED on the module gradually gets dimmer then brighter, cyclically, like human breathe.
Leave a Reply
Your email address will not be published.Required fields are marked. *
CATEGORIES