Table of Contents

Generate PWM

The four general-purpose outputs DO0DO3 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:

Generate PWM

Configure PWM

Each output has its own PwmFrequencyDO0 and PwmDutyCycleDO0 registers.

Configure PWM

  • Insert a KeyDown operator and set the Filter property to A.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select PwmFrequencyDO0Payload.
    • PwmFrequencyDO0 - Set the PWM frequency in Hz (e.g. 1000). Valid values are 1 to 10000.
  • Insert a MulticastSubject operator named Behavior Commands.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select PwmDutyCycleDO0Payload.
    • PwmDutyCycleDO0 - Set the duty cycle in percent (e.g. 50). Valid values are 1 to 99.
  • Insert a MulticastSubject operator named Behavior 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.

Start and Stop PWM

  • Insert a KeyDown operator and set the Filter property to S.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select PwmStartPayload.
    • PwmStart - Select PwmDO0.
  • Insert a MulticastSubject operator named Behavior Commands.

In a separate branch:

  • Insert a KeyDown operator and set the Filter property to D.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select PwmStopPayload.
    • PwmStop - Select PwmDO0.
  • Insert a MulticastSubject operator named Behavior 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.

Generate PWM Bursts

  • Insert a KeyDown operator and set the Filter property to F.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select OutputPulseEnablePayload.
    • OutputPulseEnable - Select DO0.
  • Insert a MulticastSubject operator named Behavior Commands.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select PulseDO0Payload.
    • PulseDO0 - Set the burst duration to 100 ms.
  • Insert a MulticastSubject operator named Behavior 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