Skip to main content
This doc relates to the below revision of hardware
Mecha Logo

Comet (rev5)

For Pilot users

The all new Mecha Comet

Better in all ways, the next revision of Mecha Comet, coming soon.

Learn more

PWM

Overview

Pulse Width Modulation (PWM) is a technique used to control the power delivered to electrical devices by modulating the duty cycle of a signal. It is widely used in embedded systems and computing for applications such as motor control, LED dimming, and signal generation.

What is PWM?

PWM is a method for generating an analog-like signal using digital means. Instead of varying the voltage continuously, PWM rapidly switches the signal between high and low states at a fixed frequency. The proportion of time the signal stays in the high state (duty cycle) determines the effective power delivered to the device.

How PWM Works?

PWM signals are defined by:

  • Frequency: The number of times the signal repeats per second (Hz).
  • Duty Cycle: The percentage of time the signal stays high in a single cycle.

For example, a PWM signal with a period of 1ms and a duty cycle of 50% means the signal stays high for 0.5ms and low for 0.5ms in each cycle.

Why is PWM Useful?

PWM is crucial in embedded systems for various reasons:

  • Energy Efficiency: Allows control of power without excessive heat dissipation.
  • Motor Speed Control: Adjusting the duty cycle alters motor speed.
  • LED Dimming: Changing the duty cycle controls brightness levels.
  • Signal Processing: Can be used to generate analog signals from digital sources.

PWM Pins

The Mecha Comet device provides two PWM-capable GPIO pins:

PWM ChannelGPIO Pin
PWM1GPIO1_IO01
PWM2GPIO5_IO04

Accessing PWM on Using gpiod

Since the Mecha Comet device runs a newer Linux kernel, we will use the gpiod library instead of the deprecated sysfs interface.

Installing gpiod

Ensure gpiod tools and libraries are installed:

$ sudo apt update && sudo apt install gpiod libgpiod-dev python3-libgpiod

Enabling and Configuring PWM

To use PWM with gpiod, follow these steps:

  1. Identify the PWM line using gpioinfo:

    $ gpioinfo | grep pwm
  2. Configure the PWM pin using gpioset:

    $ gpioset --mode=time --sec=5 gpiochip0 1=1
  3. Adjust the duty cycle dynamically by modifying the pulse width in a loop.


Example of Controlling LED Brightness with PWM

This example demonstrates the implementation of Pulse Width Modulation (PWM) to control the brightness of an LED. PWM is a technique used to generate an analog-like signal from a digital output by varying the duty cycle of a square wave. This method allows for fine-grained control of the LED's brightness, and the same principle can be applied to regulate other devices requiring variable power or signal levels, such as motor speed, audio output, or servo positioning.

Using Python

import os
import time
import sys

PWM_CHIP_PATH = "/sys/class/pwm/pwmchip0"
PWM_PATH = os.path.join(PWM_CHIP_PATH, "pwm0")

def write_sysfs(path, value):
try:
with open(path, 'w') as f:
f.write(str(value))
except OSError as e:
print(f"Error writing to {path}: {e}")
sys.exit(1)

def export_pwm0():
write_sysfs(os.path.join(PWM_CHIP_PATH, "export"), "0")

def unexport_pwm0():
write_sysfs(os.path.join(PWM_CHIP_PATH, "unexport"), "0")

def set_period(period):
write_sysfs(os.path.join(PWM_PATH, "period"), period)

def set_duty_cycle(duty_cycle):
write_sysfs(os.path.join(PWM_PATH, "duty_cycle"), duty_cycle)

def enable_pwm0():
write_sysfs(os.path.join(PWM_PATH, "enable"), "1")

def disable_pwm0():
write_sysfs(os.path.join(PWM_PATH, "enable"), "0")

def main():
# Export and initialize PWM
export_pwm0()
time.sleep(0.1) # Wait a bit to allow the pwm0 directory to appear
set_period(10000)
set_duty_cycle(0)
enable_pwm0()

# Vary the duty cycle
duty_cycles = [100, 1000, 2000, 5000, 10000]
for dc in duty_cycles:
set_duty_cycle(dc)
time.sleep(1)

# Clean up
disable_pwm0()
unexport_pwm0()

if __name__ == "__main__":
main()

To Run the Python Script on the Mecha Comet:

  1. Save the script as pwm_example.py.
  2. Make script executable:
    $ chmod +x pwm_example.py
  3. Run the script:
    $ python3 pwm_example.py

Ensure you have the necessary permissions to access the /sys/class/pwm directory and modify the PWM settings.


Use Cases

  1. Direct Current (DC) Motor Speed Regulation Pulse Width Modulation (PWM) offers precise control over the rotational speed of Direct Current (DC) motors. By adjusting the duty cycle of the applied PWM signal, the average voltage delivered to the motor is varied. An increased duty cycle results in a higher average voltage, thereby increasing the motor's speed.

  2. Servo Motor Angular Positioning Servo motors necessitate specific Pulse Width Modulation (PWM) signals to accurately control their angular position. The duty cycle of the PWM signal directly correlates with the desired angular displacement of the servo motor's shaft.

  3. Light Emitting Diode (LED) Brightness Control The perceived brightness of a Light Emitting Diode (LED) can be smoothly adjusted by modulating the duty cycle of the applied Pulse Width Modulation (PWM) signal. An elevated duty cycle results in a higher average power delivered to the LED, leading to increased brightness.