title: How to use MS-IoT Lightning to set/get PWM with Raspberry Pi2?
body: <p>I'd like to get Frequency and Duty Cycle of two PWM signals (i.e. PWM inputs) and set them for another one (i.e. PWM output) depending on inputs. These PWM signals have a Duty Cycle of 50%, while their Frequency range is from 1kHz to 20kHz.</p>
  
  <p>I checked the web a bit,  and I found the <a href="https://github.com/ms-iot/BusProviders" rel="nofollow noreferrer">Microsoft IoT Lightning</a> Library (i.e. Bus Providers) from Windows 10 IoT Core. This library seems to be what I need, even with the PWM Consumer example!<br>
  However, while I was testing my first example based on <a href="https://github.com/ms-iot/BusProviders/blob/develop/Microsoft.IoT.Lightning.Providers/PwmConsumer/StartupTask.cs" rel="nofollow noreferrer">PWM Consumer</a> one, I noticed that PWM Controller frequency range is limited from 40Hz to 1kHz. Hence, the first issue: the frequency range seems not supported.<br>
  Moreover, while PWM Controller property "ActualFrequency" returns the frequency setted via "SetDesiredFrequencyMethod", PWMPin objects provides only information about current Duty Cycle. </p>
  
  <p>Hence, I googled looking for some answer and I found <a href="https://stackoverflow.com/questions/32192412/raspberry-pi2-running-windows-10-no-pwm">this question</a> which confuses me even more than the two previous issues.</p>
  
  <p>Do you know if it is possible and how to use MS-IoT Lightning Library to set/get PWM signals from 1kHz to 20kHz on a Raspberry Pi2?</p>
  
  <p>Here, few rows of code from the example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    public async void Run(IBackgroundTaskInstance taskInstance)
      {
          if (!LightningProvider.IsLightningEnabled)
          {
              // Lightning provider is required for this sample
              return;
          }
  
          var deferral = taskInstance.GetDeferral();
  
          // Use the PAC9685 PWM provider, LightningPCA9685PwmControllerProvider
          pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[0];
          motorPin = pwmController.OpenPin(0);
          secondMotorPin = pwmController.OpenPin(1);
  
          //// To use the software PWM provider, LightningSoftwarePwmControllerProvider, with GPIO pins 5 and 6, 
          //// uncomment the following lines and comment the ones above
          //pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];
          //motorPin = pwmController.OpenPin(5);
          //secondMotorPin = pwmController.OpenPin(6);
  
          pwmController.SetDesiredFrequency(50);
          motorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth);
          motorPin.Start();
          secondMotorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth);
          secondMotorPin.Start();
  
          timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(500));
      }
  
      private void Timer_Tick(ThreadPoolTimer timer)
      {
          iteration++;
          if (iteration % 3 == 0)
          {
              currentPulseLength = ClockwisePulseLength;
              secondPulseLength = CounterClockwisePulseLegnth;
          }
          else if (iteration % 3 == 1)
          {
              currentPulseLength = CounterClockwisePulseLegnth;
              secondPulseLength = ClockwisePulseLength;
          }
          else
          {
              currentPulseLength = 0;
              secondPulseLength = 0;
          }
  
          double desiredPercentage = currentPulseLength / (1000.0 / pwmController.ActualFrequency);
          motorPin.SetActiveDutyCyclePercentage(desiredPercentage);
  
          double secondDesiredPercentage = secondPulseLength / (1000.0 / pwmController.ActualFrequency);
          secondMotorPin.SetActiveDutyCyclePercentage(secondDesiredPercentage);
      }
  </code></pre>
  
  <p>All the best, Lorenzo</p>