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 a LED (where the duty cycles 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:
Configure PWM
Each output has its own PwmFrequencyDO0 and PwmDutyCycleDO0 registers.
- 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 re-written while the PWM signal is running.
Start and Stop PWM
The PwmStart and PwmStop registers 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- SelectPwmDO0.
- 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- SelectPwmDO0.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, press A to configure, 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 function on the output with the OutputPulseEnable register and configure the duration in the PulseDO0 register.
- Insert a
KeyDownoperator and set theFilterproperty toF. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectOutputPulseEnablePayload.OutputPulseEnable- SelectDO0.
- Insert a
MulticastSubjectoperator namedBehavior Commands. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPulseDO0Payload.PulseDO0- Set the burst duration to 100 ms.
- 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.
Last updated with:
Hardware v2.1
Firmware v3.3
Bonsai Package v0.2