当前位置:网站首页>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
边栏推荐
- Service asynchronous communication
- Build your own website (17)
- 动态规划思想《从入门到放弃》
- [Niuke classic question 01] bit operation
- Configuring OSPF basic functions for Huawei devices
- 5种不同的代码相似性检测,以及代码相似性检测的发展趋势
- Let's talk about 15 data source websites I often use
- 批量获取中国所有行政区域经边界纬度坐标(到县区级别)
- 筑梦数字时代,城链科技战略峰会西安站顺利落幕
- from .cv2 import * ImportError: libGL.so.1: cannot open shared object file: No such file or direc
猜你喜欢
Data processing of deep learning
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
pyflink的安装和测试
Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Windows installation mysql8 (5 minutes)
Learn self 3D representation like ray tracing ego3rt
threejs图片变形放大全屏动画js特效
随机推荐
from .cv2 import * ImportError: libGL.so.1: cannot open shared object file: No such file or direc
Stm32f407 ------- DAC digital to analog conversion
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
通过串口实现printf函数,中断实现串口数据接收
Dr selection of OSPF configuration for Huawei devices
[牛客] [NOIP2015]跳石头
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Make a simple graphical interface with Tkinter
动态规划思想《从入门到放弃》
Address information parsing in one line of code
equals()与hashCode()
浅谈测试开发怎么入门,如何提升?
Js+svg love diffusion animation JS special effects
Cause of handler memory leak
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
筑梦数字时代,城链科技战略峰会西安站顺利落幕
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
Learn to use code to generate beautiful interface documents!!!