当前位置:网站首页>ESP Arduino (IV) PWM waveform control output

ESP Arduino (IV) PWM waveform control output

2022-07-07 01:02:00 pocean2012

PWM As an important IO Output function , Widely applied , Common applications include :

1. Motor control ( adjust speed , Torque adjustment , Constant voltage / constant current / Constant torque control and so on )

2. Control the buzzer output tone

3. Play the sound file

4. Breathing lights

In a simple and crude way , It can be simulated directly with software

/*
gen the PMW pulse via software
*/

#define PULSE 18
#define LED 22
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(PULSE, OUTPUT);
  Serial.begin(115200);
}
int loop_count=0;
// the loop function runs over and over again forever
void loop() {
  
  digitalWrite(PULSE, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(20);                       // wait for a second
  digitalWrite(PULSE, LOW);    // turn the LED off by making the voltage LOW
  delayMicroseconds(10);                       // wait for a second
  

}

Can be controlled LED The brightness of , If you use Logic Grab waveform , Like this :

Low level 10us, The whole cycle 30us( Of course , It's about the same , Generally, add the time of software code execution ,us level ), It is basically consistent with the program setting . 

  But such examples are not practical , because CPU It's all occupied , Nothing else can be done .

So we need to have a way of background execution , Can be combined with other applications .

ESP32 stay arduino There is no PWM routine , Didn't like Arduino Official boards and so on analogWrite Method , So you can use it LEDC Control interface to output PWM wave , For the convenience of study , It can cooperate with logic analyzer software to capture and analyze waveforms .

Download link on official website :

Logic analyzer software from Saleae

Plug in can be customized , You can also use python To call API, Very powerful ! The page is also much more beautiful than the old version .

 ESP32 Of LEDC Interface is used to generate PWM Very convenient , See the article below

ESP32 Learning notes (15)——LEDC(PWM) Interface to use - Simple books

LEDC Interface function method

ESP32 There is one LEDC, Originally designed to control LED, You can do simple PWM Output , You can specify output to any GPIO. 
LEDC All in all 16 Three way passage (0 ~ 15), Divided into high and low speed groups , High speed channel (0 ~ 7) from 80MHz Clock driven , Low speed channel (8 ~ 15) from 1MHz Clock driven .

Method function :
double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits)
channel Is the channel number , Value 0 ~ 15;
freq, Set the frequency ;
resolution_bits Count digits , Value 0 ~ 20( This value determines the following ledcWrite The maximum value of the duty cycle in the method , If this value is written 10, Then the maximum duty cycle can be written 2^10-1=1023 ;
Channel final frequency = clock frequency / ( Division coefficient * ( 2^ Count digits ) );( The maximum division coefficient is 1024)
 

Case a , Output PWM square wave

#include <Arduino.h>

int freq = 2000;    //  frequency 
int channel = 0;    //  passageway 
int resolution = 8;   //  The resolution of the 
int dutyCycle=100;    // 

const int led = 18; // define the pin

void setup()
{

  ledcSetup(channel, freq, resolution); //  Set up channels 
  ledcAttachPin(led, channel);  //  Connect the channel to the corresponding pin 
  
  ledcWrite(channel, dutyCycle);  //  Output PWM
}

void loop()
{
  
}

Output waveform

From the measured data, we can see the frequency 2K, The duty cycle is close to 40%(100/255 Also almost )

  Case 2 .  Changing PWM wave -- Breathing lights

#include <Arduino.h>

int freq = 1000;    //  frequency 
int channel = 0;    //  passageway 
int resolution = 8;   //  The resolution of the 

const int led = 18;
void setup()
{

  ledcSetup(channel, freq, resolution); //  Set up channels 
  ledcAttachPin(led, channel);  //  Connect the channel to the corresponding pin 
}

void loop()
{
  //  Gradually brighten 
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle = dutyCycle + 5)
  {
    ledcWrite(channel, dutyCycle);  //  Output PWM
    delay(20);
  }

  //  Gradually darken 
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle = dutyCycle - 5)
  {
    ledcWrite(channel, dutyCycle);  //  Output PWM
    delay(20);
  }
}

  Observe the effect of breathing lamp

Case three . Play sounds of different tones , It must be matched with a buzzer with drive or a horn with power amplifier , Unfortunately, there is no

原网站

版权声明
本文为[pocean2012]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130937290122.html