Windows IoT Core: Software PWM

1207 VIEWS

In my opinion, the lack of PWM when working with Windows IoT Core on Raspberry Pi is a gaping hole. There are two approaches to solving this problem—one is with additional hardware to provide PWM functionality, the other is to achieve PWM with software. This article will focus on Software PWM.

A Convenient Software PWM Library

When poking around the Web, I stumbled across this convenient library for Windows IoT Core. One of the implemented functions in this library is support for Software PWM. This library is also available as a package through NuGet. To demonstrate software PWM in this article, we will be modifying the brightness of an LED.

Hardware Requirements

Wiring Details

  • Pi Cobbler GND to Breadboard Ground Rail
  • Pi Cobbler Pin 21 to LED Positive Leg (Anode)
  • Resistor from Ground Rail to LED Negative Leg (Cathode)

Hardware Diagram

Software

Create a new blank Windows Universal application. I’ve named mine “PiSoftwarePWM.” Once the project is created, add a reference to the extension “Windows IoT Extensions for the UWP.” Finally, manage NuGet packages for the project, and install “Microsoft.IoT.Devices.”

Open MainPage.xaml and replace the listing with the following to define the user interface:



	
		
			
			 
			 
		 
		
		 
		 
	

Then, implement the functionality of the UI by opening MainPage.xaml.cs and replacing the code with the following (documented inline):

using Microsoft.IOT.DeviceCore.Pwm; 
using Microsoft.IOT.Devices.Pwm; 
using System; 
using Windows.Devices.Gpio;
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 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(); 
	} 
   }
}

Running the Application

Build and deploy the application to Raspberry Pi. You will now see that the LED brightness will change as you change the slider value. The higher the duty cycle, the brighter the LED.


Carey Payette is a Senior Software Engineer with Trillium Innovations, as well as an ASPInsider and Progress Developer Expert. She has an interest in IoT and is a member of the Maker community. Carey is also a wife and mom to three fabulous boys. She has a 2nd degree black belt in TaeKwonDo and enjoys coding for fun!


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu