keyestudio
Share your creative ideas with us here
By ran | 03 January 2025 | 0 Comments

Make LED "Breathe" on Raspberry Pi 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,you can refer to experiment six.

Components

wps97

wps98

wps54

ESP32 Board*1

ESP32 Expansion Board*1

Keyestudio Purple LED Module*1

image-20230602113648311

wps100

 

3P Dupont Wire*1

MicroUSB Cable*1

 

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. *
Name
E-mail
Content
Verification code