PWM

Code Example

from periphery import PWM

# Open PWM channel 0, pin 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(channel, pin)[source]

Bases: object

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

Parameters:
  • channel (int) – Linux channel number.
  • pin (int) – Linux pin number.
Returns:

PWM object.

Return type:

PWM

Raises:
  • PWMError – if an I/O or OS error occurs.
  • TypeError – if channel or pin types are invalid.
  • ValueError – if PWM channel does not exist.
close()[source]

Close the sysfs PWM.

enable()[source]

Enable the PWM output.

disable()[source]

Disable the PWM output.

channel

Get the sysfs PWM channel number.

Type:int
pin

Get the sysfs PWM pin number.

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.