-
接线
IRF520调压模块
1、输入 VCC GND 接ESP32模块的3.3V SIG引脚 接ESP32的PWM信号输入D15脚
2、VCC GND 输入电源V2 此电源不能和 1的电源有瓜葛 (不能共地 )
3、V+ V- 输出的可变电压 电压在0V -- V2(2中的电源电压) 变化
使用D15引脚的PWM信号控制3中的电压在 0 到V2之间变化 实现无极调压 -
Arduino实现PWM步骤:
1.首先,选择一个PWM通道,从0到15共有16个通道。
2.然后,设置PWM信号频率。对于LED来说,使用5000 Hz的频率是合适的。
3. 设置信号的占空比分辨率,分辨率从1到16位。此处将使用8位分辨率,可以使用0到255的值来控制LED亮度(2的8次方)。
4. 指定信号将出现在哪个或哪些GPIO上。为此,将使用以下函数:
ledcAttachPin(GPIO, channel) //输出信号的GPIO,产生信号的通道。
5. 使用PWM控制LED亮度,可以使用以下函数:ledcWrite(channel, dutycycle); // PWM信号的通道和占空比
- 代码
#include <Arduino.h> // 定义PWM信号的引脚 const int ledPin = 15; // // 设置PWM信号频率 const int freq = 5000; //设置PWM通道 0-15 const int ledChannel = 0; //设置信号的占空比分辨率 0-255 const int resolution = 8; void setup(){ // configure LED PWM functionalitites ledcSetup(ledChannel, freq, resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(ledPin, ledChannel); } void loop(){ // increase the LED brightness for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){ // changing the LED brightness with PWM ledcWrite(ledChannel, dutyCycle); delay(15); } // decrease the LED brightness for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){ // changing the LED brightness with PWM ledcWrite(ledChannel, dutyCycle); delay(15); } }
ESP32使用Arduino环境PWM驱动IRF520模块实现调压
于 2022-10-14 20:48:40 首次发布