Mastering MicroPython PWM Output

Pulse Width Modulation (PWM) is a powerful technique widely used in electronics for controlling the power delivered to a device, adjusting the brightness of LEDs, controlling the speed of motors, and more. MicroPython, a lean and efficient implementation of the Python 3 programming language that runs on microcontrollers, provides an easy - to - use interface for generating PWM signals. In this blog, we will explore the fundamental concepts of MicroPython PWM output, learn how to use it, look at common practices, and discover best practices.

Table of Contents#

  1. Fundamental Concepts of PWM
  2. MicroPython PWM Output Basics
  3. Usage Methods
  4. Common Practices
  5. Best Practices
  6. Conclusion
  7. References

Fundamental Concepts of PWM#

PWM is a method of encoding a message into a pulsing signal. A PWM signal is characterized by two main parameters:

  • Frequency: The number of times the signal repeats per second, measured in Hertz (Hz). A higher frequency means the signal pulses more often.
  • Duty Cycle: The percentage of time the signal is in the high state during one period. For example, a 50% duty cycle means the signal is high for half of the period and low for the other half.

By varying the duty cycle, we can control the average power delivered to a device. For instance, when controlling an LED, a higher duty cycle will make the LED brighter, while a lower duty cycle will make it dimmer.

MicroPython PWM Output Basics#

In MicroPython, the machine.PWM class is used to generate PWM signals. To use it, you first need to import the machine module and then create a PWM object associated with a specific pin.

import machine
 
# Create a PWM object on pin 2
pwm = machine.PWM(machine.Pin(2))

Usage Methods#

Setting the Frequency#

You can set the frequency of the PWM signal using the freq() method.

import machine
 
pwm = machine.PWM(machine.Pin(2))
# Set the frequency to 1000 Hz
pwm.freq(1000)

Setting the Duty Cycle#

The duty cycle is set using the duty() method. The duty cycle value is typically in the range of 0 - 1023 (on most MicroPython platforms).

import machine
 
pwm = machine.PWM(machine.Pin(2))
pwm.freq(1000)
# Set the duty cycle to 50% (512 out of 1023)
pwm.duty(512)

Stopping the PWM Signal#

To stop the PWM signal, you can set the duty cycle to 0.

import machine
 
pwm = machine.PWM(machine.Pin(2))
pwm.freq(1000)
pwm.duty(512)
# Stop the PWM signal
pwm.duty(0)

Common Practices#

Controlling LED Brightness#

One of the most common applications of PWM is controlling the brightness of an LED.

import machine
import time
 
led = machine.PWM(machine.Pin(2))
led.freq(1000)
 
# Gradually increase the brightness
for duty in range(0, 1024):
    led.duty(duty)
    time.sleep(0.001)
 
# Gradually decrease the brightness
for duty in range(1023, -1, -1):
    led.duty(duty)
    time.sleep(0.001)
 
# Turn off the LED
led.duty(0)

Controlling a Servo Motor#

Servo motors can also be controlled using PWM. The position of a servo is determined by the pulse width of the PWM signal.

import machine
import time
 
servo = machine.PWM(machine.Pin(2), freq=50)
 
# Move the servo to the minimum position
servo.duty(25)
time.sleep(1)
 
# Move the servo to the maximum position
servo.duty(125)
time.sleep(1)
 
# Move the servo back to the center position
servo.duty(75)
time.sleep(1)
 
# Stop the servo
servo.duty(0)

Best Practices#

Choose the Right Frequency#

The frequency of the PWM signal depends on the application. For LED brightness control, a frequency in the range of 100 - 1000 Hz is usually sufficient. For servo motors, a frequency of 50 Hz is commonly used.

Error Handling#

When working with PWM, it's important to handle errors properly. For example, if the pin is already in use by another peripheral, creating a PWM object may fail.

import machine
 
try:
    pwm = machine.PWM(machine.Pin(2))
    pwm.freq(1000)
    pwm.duty(512)
except Exception as e:
    print(f"An error occurred: {e}")

Resource Management#

Make sure to release the PWM resources when they are no longer needed. This can be done by setting the duty cycle to 0 and potentially deinitializing the PWM object.

import machine
 
pwm = machine.PWM(machine.Pin(2))
pwm.freq(1000)
pwm.duty(512)
 
# Release the resources
pwm.duty(0)
pwm.deinit()

Conclusion#

MicroPython's PWM output capabilities provide a simple and effective way to control various electronic devices. By understanding the fundamental concepts of PWM, learning the usage methods, and following common and best practices, you can easily implement PWM - based applications such as LED brightness control and servo motor control. Whether you are a beginner or an experienced developer, MicroPython's PWM functionality can greatly simplify your projects.

References#