Carey Payette
Published © GPL3+

Windows IoT Core: Software PWM on the Raspberry Pi

Support for Software PWM is available for Windows IoT Core and the Raspberry Pi using a convenient NuGet Package.

BeginnerProtip1 hour6,834
Windows IoT Core: Software PWM on the Raspberry Pi

Things used in this project

Story

Read more

Schematics

Wiring Setup

Wiring setup

Code

MainPage.xaml Code Listing

Plain text
<Page
    x:Name="pgMain"
    x:Class="PiSoftwarePWM.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PiSoftwarePWM"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Unloaded="pgMain_Unloaded">
 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="125" />
            <RowDefinition Height="40"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock x:Name="lblSoftwarePWM" Margin="10" HorizontalAlignment="Left"  
                TextWrapping="Wrap" Text="Software PWM" VerticalAlignment="Center" 
                Width="439" Height="49" FontSize="36"/>
        <TextBlock x:Name="lblDutyCycle" Margin="10" HorizontalAlignment="Left" Grid.Row="1" 
                TextWrapping="Wrap" Text="Duty Cycle" VerticalAlignment="Top" Width="357"/>
        <Slider x:Name="slDutyCycle" Margin="10" HorizontalAlignment="Left" Grid.Row="2" 
                Width="462" TickFrequency="25" ValueChanged="slDutyCycle_ValueChanged"/>
    </Grid>
</Page>

UI implementation - MainPage.xaml.cs

Plain text
using Microsoft.IoT.DeviceCore.Pwm;
using Microsoft.IoT.Devices.Pwm;
using System;
using Windows.Devices.Gpio;
using Windows.Devices.Pwm;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
 
namespace PiSoftwarePWM
{
    public sealed partial class MainPage : Page
    {
        private int _ledPin = 21;
        private PwmPin _redLed;
        private PwmController _pwmController;
 
        public MainPage()
        {
            this.InitializeComponent();
            SetupGPIO();
        }
 
        private async void SetupGPIO()
        {
            var gpioController = GpioController.GetDefault();
            var pwmManager = new PwmProviderManager();
            pwmManager.Providers.Add(new SoftPwm());
 
            var pwmControllers = await pwmManager.GetControllersAsync();
 
            //use the first available PWM controller an set refresh rate (Hz)
            _pwmController = pwmControllers[0];
            _pwmController.SetDesiredFrequency(240);
 
            _redLed = _pwmController.OpenPin(_ledPin);
            _redLed.Start();
            slDutyCycle.Value = 100;
        }
 
        private void slDutyCycle_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {           
            //base the duty cycle on the value from the slider value (0-100 mapping to 0-1.0 duty cycle)
            _redLed.SetActiveDutyCyclePercentage(e.NewValue/100);
        }
 
        private void pgMain_Unloaded(object sender, RoutedEventArgs e)
        {
            _redLed.Stop();
        }
    }
}

Credits

Carey Payette

Carey Payette

13 projects • 134 followers
Sr. Software Engineer at Independent

Comments