Generate PWM
The four general-purpose outputs DO0–DO3 can generate hardware-timed pulse-width modulation (PWM) signals. The PWM signal can be tested with either a speaker (where the frequency controls the pitch) or an LED (where the duty cycle controls the brightness). Refer to the connections article to set up either accessory on DO0, which we will use for the rest of these examples.
This article covers how to configure the frequency and duty cycle, as well as how to generate and stop the PWM in Bonsai.
The complete workflow is shown below. Copy and paste it into Bonsai or build each section by following the step-by-step instructions below.
Configure PWM
Each output has its own frequency and duty cycle registers, for instance PwmFrequencyDO0 and PwmDutyCycleDO0 for DO0. Configure these registers before starting the PWM signal.
- Insert a
KeyDownoperator and set theFilterproperty toA. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPwmFrequencyDO0Payload.PwmFrequencyDO0- Set the PWM frequency in Hz (e.g. 1000). Valid values are 1 to 10000.
- Insert a
MulticastSubjectoperator namedBehavior Commands. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPwmDutyCycleDO0Payload.PwmDutyCycleDO0- Set the duty cycle in percent (e.g. 50). Valid values are 1 to 99.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow and press A to configure a 1 kHz, 50% duty cycle waveform on DO0. Frequency and duty cycle can also be modified while the PWM signal is running.
Start and Stop PWM
Use the PwmStart and PwmStop registers to start and stop the waveform on any combination of outputs.
- Insert a
KeyDownoperator and set theFilterproperty toS. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPwmStartPayload.PwmStart- Select the line to start PWM on (e.g.PwmDO0).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toD. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPwmStopPayload.PwmStop- Select the line to stop PWM on (e.g.PwmDO0).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, then press S to start and D to stop the waveform on DO0.
Generate PWM Bursts
To generate a PWM train of fixed duration, enable the pulse mode on the output with the OutputPulseEnable register and configure the duration in the appropriate output line pulse duration register (e.g. PulseDO0 for DO0).
- Insert a
KeyDownoperator and set theFilterproperty toF. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectOutputPulseEnablePayload.OutputPulseEnable- Select the line to enable the PWM pulse on (e.g.DO0).
- Insert a
MulticastSubjectoperator namedBehavior Commands. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPulseDO0Payload.PulseDO0- Set the burst duration in ms (e.g.100).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, press F once to configure the pulse, then press S. The PWM runs for 100 ms and stops on its own, which translates to either a short beep with a speaker or a brief flash with an LED.
Warning
OutputPulseEnable is a selection mask over all digital outputs. Any output not selected in the payload has its pulse mode disabled. Make sure to list every output that should keep pulse mode (e.g. DO0, SupplyPort0).
Last updated with:
Hardware v2.1
Firmware v3.3
Bonsai Package v0.2