Basic PWM AT commands
AT+DRVPWMINIT: Initialize the PWM module with a specified frequency and GPIO
AT+DRVPWMDUTY: Set the duty cycle of a specified PWM channel
AT+DRVPWMFADE: Set the PWM fade
Introduction
PWM, or Pulse Width Modulation, is a technique for controlling analog signals, widely used in electronics and automation control. PWM controls the output of an analog circuit by varying the pulse width to control the signal’s duty cycle.
AT+DRVPWMINIT: Initialize the PWM module with a specified frequency and GPIO
Set command
Command:
AT+DRVPWMINIT=<freq>,<ch0_gpio>,[,...,<ch4_gpio>]
Response:
OK
Parameters
<freq>: PWM output frequency in Hz
<ch0_gpio>: GPIO pin for PWM output
Description
The set frequency must satisfy the formula: master_clock / (period + 1) / clockdiv, so some frequencies that cannot be divided evenly cannot be output.
ch0_gpio options: 16, 35, 28, 2, 10
ch1_gpio options:: 17, 36, 3, 11, 29
ch2_gpio options:: 0, 18, 12, 30, 40
ch3_gpio options:: 1, 19, 13, 31, 41
ch4_gpio options:: 4, 7, 14, 32, 42
Frequency range: [3, 160K] HZ
Example
// pwm channel0 outputs at 10KHz
AT+DRVPWMINIT=10000,16
AT+DRVPWMDUTY: Set the duty cycle of a specified PWM channel
Set command
Command:
AT+DRVPWMDUTY=<ch0 duty>[,...,<ch4 duty>]
Response:
OK
Parameters
<ch0 duty>: Duty cycle of PWM output on channel0
<ch1 duty>: Duty cycle of PWM output on channel1
<ch2 duty>: Duty cycle of PWM output on channel2
<ch3 duty>: Duty cycle of PWM output on channel3
<ch4 duty>: Duty cycle of PWM output on channel4
Description
The set duty cycle will affect the frequency set during initialization.
Example
// pwm channel0 outputs a PWM waveform with a high level cycle count of 10
AT+DRVPWMDUTY=10
AT+DRVPWMFADE: Set the fade mode of a specified PWM channel
Set command
Command:
AT+DRVPWMFADE=<ch0_target_duty>,<ch0_fade_time>[,...,<ch4_target_duty>,<ch4_fade_time>]
Response:
OK
Parameters
<ch0_target_duty>: Target duty cycle of PWM output on channel0
<ch0_fade_time>: Time (ms) to change to the target duty cycle on channel0
<ch1_target_duty>: Target duty cycle of PWM output on channel1
<ch1_fade_time>: Time (ms) to change to the target duty cycle on channel1
<ch2_target_duty>: Target duty cycle of PWM output on channel2
<ch2_fade_time>: Time (ms) to change to the target duty cycle on channel2
<ch3_target_duty>: Target duty cycle of PWM output on channel3
<ch3_fade_time>: Time (ms) to change to the target duty cycle on channel3
<ch4_target_duty>: Target duty cycle of PWM output on channel4
<ch4_fade_time>: Time (ms) to change to the target duty cycle on channel4
Description
When the target duty cycle is greater than the current duty cycle, the duty cycle will increment; otherwise, it will decrement.
Example
// pwm waveform duty cycle changes from the current duty cycle to 100 in 10 seconds
AT+DRVPWMFADE=100,10000