PWM

Code Example

from periphery import PWM

# Open PWM chip 0, channel 10
pwm = PWM(0, 10)

# Set frequency to 1 kHz
pwm.frequency = 1e3
# Set duty cycle to 75%
pwm.duty_cycle = 0.75

pwm.enable()

# Change duty cycle to 50%
pwm.duty_cycle = 0.50

pwm.close()

API

class periphery.PWM(chip, channel)[source]

Bases: object

Instantiate a PWM object and open the sysfs PWM corresponding to the specified chip and channel.

Parameters:
  • chip (int) – PWM chip number.
  • channel (int) – PWM channel number.
Returns:

PWM object.

Return type:

PWM

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if chip or channel types are invalid.
  • LookupError – if PWM chip does not exist.
  • TimeoutError – if waiting for PWM export times out.
PWM_STAT_RETRIES = 10
PWM_STAT_DELAY = 0.1
close()[source]

Close the PWM.

enable()[source]

Enable the PWM output.

disable()[source]

Disable the PWM output.

devpath

Get the device path of the underlying sysfs PWM device.

Type:str
chip

Get the PWM chip number.

Type:int
channel

Get the PWM channel number.

Type:int
period_ns

Get or set the PWM’s output period in nanoseconds.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not int.
Type:

int

duty_cycle_ns

Get or set the PWM’s output duty cycle in nanoseconds.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not int.
Type:

int

period

Get or set the PWM’s output period in seconds.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not int or float.
Type:

int, float

duty_cycle

Get or set the PWM’s output duty cycle as a ratio from 0.0 to 1.0.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not int or float.
  • ValueError – if value is out of bounds of 0.0 to 1.0.
Type:

int, float

frequency

Get or set the PWM’s output frequency in Hertz.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not int or float.
Type:

int, float

polarity

Get or set the PWM’s output polarity. Can be “normal” or “inversed”.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not str.
  • ValueError – if value is invalid.
Type:

str

enabled

Get or set the PWM’s output enabled state.

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if value type is not bool.
Type:

bool

class periphery.PWMError[source]

Bases: exceptions.IOError

Base class for PWM errors.