4506 views|2 replies

155

Posts

1

Resources
The OP
 

【Beetle ESP32-C3】2. PWM breathing light (Arduino) [Copy link]

This article introduces the process of PWM development of Beetle ESP32-C3 in Arduino IDE to realize the breathing light function - in fact, the sample code of DFROBOT official website is only modified to IO7 as output.

1. PWM (Pulse Width Modulation): Pulse Width Modulation

Adjusting PWM includes: cycle adjustment and positive pulse adjustment. PWM is like "pedaling a bicycle" - pedaling when there is a positive pulse, and not pedaling when there is a negative pulse to let it slide. PWM can use this principle to perform operations such as "DC motor speed regulation" and "lighting brightness adjustment (i.e. breathing light)".

Figure 2-1 PWM principle

The Arduino core for the ESP32 does not have the analogWrite(pin, value) method used in general Arduino to output PWM . The Arduino-ESP32 framework provides a LEDC (LED control) library, which is designed to control LEDs to achieve breathing lights and the like. Of course, it can also be used for general PWM output.

Figure 2-2 Number of PWM channels of each series of ESP32

Lexin official document LEDC library link: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html .

The table given at the beginning of the document page (Figure 2-2 is a screenshot of it) shows the number of PWM channels included in each series of ESP32 MCUs. The LEDC of ESP32 has a total of 16 channels (0 ~ 15), which are divided into two groups: high-speed and low-speed. The high-speed channels (0 ~ 7) are driven by an 80MHz clock, and the low-speed channels (8 ~ 15) are driven by a 1MHz clock. The ESP32-C3 is indeed a "castrated version" of ESP32, providing 6 channels - after all, there are not so many IOs even if there are more channels.

2. Breathing light - IO7 controls LED

I chose to use IO7 for two reasons. First, the brightness of the onboard LED breathing light is not obvious. Second, IO7 is easy to connect on the breadboard (next to the core board), so there is no need to connect DuPont wires.

Figure 2-3 Breadboard connection of the breathing light case

The following is the case code, and the comments add two of my own understandings (marked by Author):

const int ledPin = 7;  // PWM生成后实际输出引脚

//设置PWM参数
const int freq = 5000;//PWM频率
const int ledChannel = 0;//信号生成GPIO
const int resolution = 8;//8位分辨率,by Author. C3可设置1~14bits

void setup(){
  //PWM参数设置
  ledcSetup(ledChannel, freq, resolution);

  //将生成信号通道绑定到输出通道上
  ledcAttachPin(ledPin, ledChannel);
}

void loop(){
  //逐渐变亮,by Author. 8位分辨率因而到255
  for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);
    delay(15);
  }

  //逐渐变暗
  for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);   
    delay(15);
  }
}

This post is from RF/Wirelessly

Latest reply

The routine comments are very clear. It is a very good article. I look forward to more wonderful works!  Details Published on 2022-8-7 10:49
 

6555

Posts

0

Resources
2
 

The official documentation of Espressif is OK

This post is from RF/Wirelessly
 
 

6818

Posts

11

Resources
3
 
The routine comments are very clear. It is a very good article. I look forward to more wonderful works!
This post is from RF/Wirelessly
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

快速回复 返回顶部 Return list