Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Using PWM

Cai Biesinger edited this page Jul 10, 2018 · 2 revisions

Unfamiliar with PWM? We have a whole guide on it!

Scarlet supports PWM usage on the Beaglebone Black (but not the Raspberry Pi yet), but all PWM outputs have the same core methods as required by the Scarlet.IO.IPWMOutput interface (shown below).

IPWMOutput Interface Methods:

  • void SetFrequency(float Frequency)
    • Sets the PWM output's frequency, in Hz.
  • void SetOutput(float DutyCycle)
    • Sets the output duty cycle. DutyCycle is expected to be between 0.0 and 1.0.
  • void SetDelay(float ClockDelay)
    • Sets the delay of the output signal. This can be useful if multiple signals are expected to time-correlate in a certain way. Not all platforms/outputs support this, and those that don't will mention it in their method comment, and simply ignore this. Input is expected to be between 0.0 and 1.0, as a percentage of the clock cycle.
  • void SetEnabled(bool Enable)
    • Whether the PWM output is currently active. Think of this like a hardware switch on the output, when disabled then enabled, state (frequency, duty cycle, etc) is kept.
  • void Dispose()
    • Cleans up resources for others' use.

Usage on BeagleBone Black

⚠️ Note the limitations of the PWM system explained at the bottom of the BBB Pin Diagram. This will likely govern how you connect and operate your PWM-accepting devices on the BBB.

To begin, add a device tree mapping addition call with your other similar lines:

BBBPinManager.AddMappingPWM(BBBPin Pin)

For example, I want to use output 1_A on P9_14. To do this, I'd call:

BBBPinManager.AddMappingPWM(BBBPin.P9_14);

Now, unlike some other systems, the IPWMOutput objects are created for you when you call BBBPinManager.ApplyPinSettings, so we simply access our desired output like this:

IPWMOutput OutA = PWMBBB.PWMDevice1.OutputA;

We can now edit our output's parameters, then start outputting, like this:

PWMBBB.PWMDevice1.SetFrequency(5000);
OutA.SetFrequency(5000); // Note that these two lines are the same. The frequency is shared between both outputs on a device.

OutA.SetOutput(0.45F);
OutA.SetEnabled(true);

There should now be a 45% duty cycle, 5kHz PWM signal on P9_14.

Logo

Quick Links:
NuGet
Pin Diagrams: RPi | BBB
Developers: CaiB, Baldstrom

General Info:
Home
Common Issues
Getting Started
Supported Devices

Sections:
Logging
DataLog
Filters
Hardware I/O:
- BeagleBone Black
- Raspberry Pi
- Pin Diagrams: RPi | BBB
- GPIO: Using | For Beginners
- PWM: Using | For Beginners
- ADC: Using | For Beginners
- I2C: Using | For Beginners
- SPI: Using | For Beginners
- UART: Using | For Beginners
- CAN: Using | For Beginners
Networking
Sensors
StateStore

Other: Interesting Case Studies

Clone this wiki locally