【Hardware Peripheral Use】——PWM

Basic concept of PWM

PWM is the abbreviation of Pulse Width Modulation (Pulse Width Modulation), which is a technology that uses digital signals to control the output of analog circuits . PWM is usually used to control the magnitude of voltage or current, and the average value of the output signal is controlled by changing the width of the pulse.
insert image description here

The PWM signal consists of a series of periodic pulses, and the width of each pulse represents the magnitude of the output voltage or current. If the width of the pulse is 100%, the output voltage or current will remain at the maximum value;
if the width of the pulse is 0%, the output voltage or current will be 0. By changing the width and period of the pulse, we can adjust the average value of the output voltage or current, thereby realizing the control of the circuit components.

PWM technology has a wide range of applications in practical applications, such as motor control motor control , LED lighting adjustment, audio digital conversion, etc. In MicroPython, we can use the corresponding PWM module for digital signal control, so as to realize various control and application scenarios.

How to use PWM

pyb.pwm

pyb.pwmIt is a module used to control PWM output in MicroPython. It provides some methods and attributes for setting and controlling parameters such as frequency and duty cycle of PWM output, so as to realize the control of various application scenarios.

The following are pyb.pwmcommonly used methods and properties:

method

  • pulse_width(pulse_width): Set the high-level duration of the pulse, in milliseconds.
  • freq(freq): Set the PWM output frequency, the unit is Hz.
  • init(freq=1000, duty=50, timer=4, channel=1, pin=None): Initialize PWM output, freq is frequency, duty is duty cycle (50% by default), timer and channel are timer and channel number (4 and 1 by default).

Attributes

  • pulse_width(): Returns the high level duration of the current pulse in milliseconds.
  • freq(): Returns the current PWM output frequency in Hz.
  • duty(): Returns the duty cycle of the current PWM output, expressed as a percentage.

Sample code:

import pyb

# 初始化PWM输出
pwm = pyb.PWM(pyb.Pin.board.X1)

# 设置PWM输出的频率为2000Hz,占空比为20%
pwm.freq(2000)
pwm.duty(20)

# 持续输出PWM信号
while True:
    pass

The above code initializes a PWM output pin, and sets the output frequency to 2000Hz and the duty cycle to 20%. Continuously output the PWM signal in the while loop.

machine.pwm

machine.pwmIt is one of the modules used to control PWM output in MicroPython. It provides some methods and attributes for setting and controlling parameters such as frequency and duty cycle of PWM output, so as to realize the control of various application scenarios.

The following are machine.pwmcommonly used methods and properties:

method

  • freq(freq): Set the PWM output frequency, the unit is Hz.
  • duty(duty): Set the duty cycle of PWM output, the range is between 0 and 1.
  • deinit(): Stop PWM output.

Attributes

  • freq(): Returns the current PWM output frequency in Hz.
  • duty(): Returns the duty cycle of the current PWM output, expressed as a percentage.

Sample code:

import machine
import time

# 初始化PWM输出
pwm = machine.PWM(machine.Pin(12))

# 设置PWM输出的频率和占空比
pwm.freq(1000)
pwm.duty(0.5)

# 持续输出PWM信号
while True:
    time.sleep(1)
    pwm.duty(0.2)
    time.sleep(1)
    pwm.duty(0.8)

The above code initializes a PWM output pin, and sets the output frequency to 1000Hz and the duty cycle to 50%. Constantly change the duty cycle in the while loop to produce brightness changes of the PWM signal.

PWM available sensors

PWM signals are commonly used to control motors and servos. In these applications, the duty cycle of the PWM signal determines the speed and position of the motor or servo.

In addition to controlling motors and servos, PWM signals can also be used to control the output of some sensors, such as:

  1. Steering gear angle sensor: The steering gear angle sensor can determine the current position and angle of the steering gear by receiving the PWM signal. By changing the duty cycle of the PWM signal, the position of the servo can be changed to obtain different angle measurements.
    insert image description here

  2. Temperature Sensor: Some temperature sensors (such as Thermistor) can use a PWM signal to read the temperature value. The duty cycle of the PWM signal can control the output voltage of the sensor, thereby affecting the resistance value of the temperature sensor, and the temperature value can be obtained by reading the resistance value. (Basically, the most components are placed on the development board, which we novices cannot use)

  3. Light sensor: The PWM signal can also be used to read the output of the light sensor. The frequency and duty cycle of the PWM signal can affect the output voltage of the light sensor, thereby affecting the measurement range and resolution of the sensor.

The parameters of the PWM signal need to be determined according to the specific sensor and application conditions, and adjusted in combination with the characteristics of the sensor.

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/130217321