Skip to content

Pulse width modulation

David Planella edited this page Nov 13, 2017 · 4 revisions

The BeagleBone's AM3359 processor offers a few ways of generating PWM signals. For the time being, PyBBIO only supports the eHRPWM1 and eHRPWM2 modules, which provide a total of four 3.3V, 16-bit PWM outputs.


Example programs:

PyBBIO/examples/fade.py


Functions:

analogWrite(pwm_pin, value, resolution=RES_8BIT)

Sets the duty cycle of the given PWM output using the given resolution. The resolution defaults to 8 bits, so if no resolution is given it acts like Arduino's analogWrite(), taking values in the range 0-255. Resolution may be one of the defined RES_8BIT, RES_16BIT to take advantage of the 16-bit timer (values of 0-65536), PERCENT to set the duty cycle in percent, or any integer greater than 0. 'value' should be an integer.

pwmWrite(pwm_pin, value, resolution=RES_8BIT)

Exactly the same as analogWrite(); for those who don't like 'analog' in the name.

pwmFrequency(pwm_pin, freq_hz)

Sets the frequency in Hertz of the given PWM pin's module. Each module can only run at a single frequency, so PWM1A and PWM1B will always be running at the same frequency, and likewise for PWM2A and PWM2B. If not called, the frequency of each module will default to 100kHz.
Note: the output signal won't be enabled until analogWrite() or pwmWrite() is called.

pwmEnable(pwm_pin)

Enables the given pin for output. Called automatically by analogWrite(), so shouldn't generally need to be called.

pwmDisable(pwm_pin)

Disables given PWM pin. Unlike calling analogWrite(pwm_pin, 0), calling this will relinquish control of the pin, allowing it to be used by another process.


Available PWM pins:

Pins listed in form:

Location - pin
Header P8:
P8.13 - PWM2B
P8.19 - PWM2A
Header P9:
P9.14 - PWM1A
P9.16 - PWM1B