Arduino for BPI:bit 3 : using LEDC (PWM)

Arduino core for the ESP32 not the general Arduino PWM function analogWrite(pin, value) ,Instead, ESP32 has a LEDC designed to control leds.

The LEDC on ESP32 has a total of 16 channels (0-15), divided into high and low speed channels (0-7) driven by 80MHz clock and low speed channels (8-15) driven by 1MHz clock.

purpose

Use LEDC to realize breathing lamp

Matching is introduced

authoring tool: vscode + platformIO Install the tutorial

Hardware: bpi:bit

main function

  • double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits) Set the frequency and count number (duty cycle resolution) corresponding to the LEDC channel, This method returns the final frequency

Channel final frequency = clock/(frequency division coefficient * (1 << count digit));(maximum frequency division coefficient is 1024)

参数 功能
channel Channel number ,0 ~ 15
freq Desired setting frequency
resolution_bits (this value determines the writable value of duty ratio in the ledcWrite method later. For example, write 10 for this value, then the maximum duty ratio can be written as 1023 . (1<<resolution_bits)-1 )
  • void ledcWrite(uint8_t channel, uint32_t duty) Specify the channel output a certain duty ratio waveform

  • double ledcWriteTone(uint8_t channel, double freq) like Arduino tones, which emit a certain sound (depending on the frequency) when the passive buzzer is attached.

  • double ledcWriteNote(uint8_t channel, note_t note, uint8_t octave) This method is a further encapsulation of the above method, which can directly output the signal of the specified mode and scale sound

Parameter function
note debug,as do、re、mi、fa……,value as NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs, NOTE_A, NOTE_Bb, NOTE_B

octave scale,value as 0~7;

Related content of music theory can refer to the following article: http://www.360doc.com/content/17/1231/01/47685146_717797647.shtml https://www.musicbody.net/sns/index.php?s=/news/index/detail/id/406.html

  • uint32_t ledcRead(uint8_t channel) Returns the value of the specified channel duty ratio

  • double ledcReadFreq(uint8_t channel) Returns the current frequency of the specified channel (this method returns 0 if the current duty cycle is 0)

  • void ledcAttachPin(uint8_t pin, uint8_t channel) Bind the LEDC channel to the specified IO port for output

  • void ledcDetachPin(uint8_t pin) Remove the LEDC function of IO port

example

#include <Arduino.h>

int freq = 2000;    // 频率
int channel = 0;    // 通道
int resolution = 8;   // 分辨率

const int led = 18;
void setup()
{

  ledcSetup(channel, freq, resolution); // 设置通道
  ledcAttachPin(led, channel);  // 将通道与对应的引脚连接
}

void loop()
{
  // 逐渐变亮
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle = dutyCycle + 5)
  {
    ledcWrite(channel, dutyCycle);  // 输出PWM
    delay(20);
  }

  // 逐渐变暗
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle = dutyCycle - 5)
  {
    ledcWrite(channel, dutyCycle);  // 输出PWM
    delay(20);
  }
}

summary

In Arduino core for the ESP32, there is no analogWrite(pin, value) method used to output PWM in Arduino, but LEDC (LED Control) is used to realize PWM function