Component | Build Status | NuGet Package |
---|---|---|
System.Device.Pwm |
You can create a PWM channel from a pin number, this is the recommended way. Keep in mind, you will have to allocate the pin in the case of ESP32 and make sure your pin is PWM enabled for STM32 devices.
// Case of ESP32, you need to set the pin function, in this example PWM3 for pin 18:
Configuration.SetPinFunction(18, DeviceFunction.PWM3);
PwmChannel pwmPin = PwmChannel.CreateFromPin(18, 40000);
// You can check then if it has created a valid one:
if (pwmPin != null)
{
// You do have a valid one
}
You can adjust the duty cycle by using the property:
pwmPin.DutyCycle = 0.42;
The duty cycle goes from 0.0 to 1.0.
It is recommended to setup the frequency when creating the PWM Channel. You can technically change it at any time but keep in mind some platform may not behave properly when adjusting this element.
You can as well, if you know the chip/timer Id and the channel use the create function:
PwmChannel pwmPin = new(1, 2, 40000, 0.5);
This is only recommended for advance users.
PWM precision may vary from platform to platform. It is highly recommended to check what precision can be achieved, either with the frequency, either with the duty cycle.
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
The list of contributors to this project can be found at CONTRIBUTORS.
The nanoFramework Class Libraries are licensed under the MIT license.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.
This project is supported by the .NET Foundation.