当前位置:网站首页>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
边栏推荐
- Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
- Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
- Attention SLAM:一种从人类注意中学习的视觉单目SLAM
- Rainstorm effect in levels - ue5
- windows安装mysql8(5分钟)
- 浅谈测试开发怎么入门,如何提升?
- Make a simple graphical interface with Tkinter
- Deep learning environment configuration jupyter notebook
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
- Learn to use code to generate beautiful interface documents!!!
猜你喜欢
Part 7: STM32 serial communication programming
Linear algebra of deep learning
Set (generic & list & Set & custom sort)
Threejs image deformation enlarge full screen animation JS special effect
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
[C language] dynamic address book
Deeply explore the compilation and pile insertion technology (IV. ASM exploration)
Part IV: STM32 interrupt control programming
C9高校,博士生一作发Nature!
重上吹麻滩——段芝堂创始人翟立冬游记
随机推荐
Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
再聊聊我常用的15个数据源网站
Advanced learning of MySQL -- basics -- transactions
Eventbus source code analysis
Telerik UI 2022 R2 SP1 Retail-Not Crack
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
. Bytecode structure of class file
Chapter II proxy and cookies of urllib Library
Service asynchronous communication
深度学习简史(一)
Let's talk about 15 data source websites I often use
paddlehub应用出现paddle包报错的问题
[牛客] B-完全平方数
Dr selection of OSPF configuration for Huawei devices
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Linear algebra of deep learning
Learn to use code to generate beautiful interface documents!!!
A brief history of deep learning (I)
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)