Table of Contents

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.

Generate PWM

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.

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 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.

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 the line to start PWM on (e.g. 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 the line to stop PWM on (e.g. PwmDO0).
  • Insert a MulticastSubject operator named Behavior 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).

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 the line to enable the PWM pulse on (e.g. 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 in ms (e.g. 100).
  • 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.

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