PWM
Pulse-Width Modulation (PWM) generates a square-wave signal of fixed frequency and variable duty cycle. It is commonly used to simulate an analog voltage, drive a servo, control LED brightness, and generate simple tones. Both the frequency and the duty cycle are set in software.
Endpoint uses the standard System.Device.Pwm API. The only
Endpoint-specific step is calling PwmController.Initialize(pin) first,
which claims the pin for its timer channel and gives you the chip and
channel ids that PwmChannel.Create needs.
- .NET
- Python
- C/C++
using System.Device.Pwm;
using GHIElectronics.Endpoint.Core;
// Buzzer pin on EP-Domino24
var pin = EPDomino24.Pwm.Pin.PF9;
PwmController.Initialize(pin);
using var pwm = PwmChannel.Create(
PwmController.GetChipId(pin),
PwmController.GetChannelId(pin),
frequency: 1000,
dutyCyclePercentage: 0.25);
pwm.Start();
Thread.Sleep(1000);
pwm.Stop();
import time
from GHIElectronics.Endpoint.Core import EPDomino24, PwmController
from GHIElectronics.Endpoint.Devices.Pwm import PwmChannel
# Buzzer pin on EP-Domino24
pin = EPDomino24.Pwm.Pin.PF9
PwmController.Initialize(pin)
with PwmChannel.Create(PwmController.GetChipId(pin),
PwmController.GetChannelId(pin),
1000, 0.25) as pwm:
pwm.Start()
time.sleep(1)
pwm.Stop()
#include <chrono>
#include <thread>
#include <endpoint/PwmController.h>
#include <endpoint/EPDomino24.h>
using namespace GHIElectronics::Endpoint::Core;
using namespace GHIElectronics::Endpoint::Devices::Pwm;
// Buzzer pin on EP-Domino24
constexpr int pin = EPDomino24::Pwm::Pin::PF9;
PwmController::Initialize(pin);
auto pwm = PwmChannel::Create(PwmController::GetChipId(pin),
PwmController::GetChannelId(pin),
1000, 0.25);
pwm.Start();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
pwm.Stop();