LED Pulse Examples for SDK 4.2

Are there any examples of pulsating a LED with 4.2 code? My previous 4.1 code no longer compiles and the methods between the GHI PMW and Microsoft PMW is quite different. Also is there a replacement for Chapter 12 - PWM from the 4.1 Beginners Guide in the 4.2 Guide?

Here is the code I’m using from Chapter 12 in the 4.1 Beginners Guide:

using System;
using Microsoft.SPOT;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace MFConsoleApplication1
public class Program
   public static void Main()
      sbyte dirr = 1;
      byte duty = 40;
      PWM pwm = new PWM((PWM.Pin)FEZ_Pin.PWM.LED);
      while (true)
         pwm.Set(10000, duty);
         duty = (byte)(duty + dirr);
         if (duty > 90 || duty < 10)
            dirr *= -1;

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

It’s way past my bed time and this is untested but this should be very close…

First, add a reference to Microsoft.SPOT.Hardware.PWM

Then change your loop to look like this…

            while (true)
                pwm.Period = 10000;
                pwm.DutyCycle = duty;
                duty = (byte)(duty + dirr);

That may work however I’m unable to define my COBRA LED pin. The original code is

     PWM pwm = new PWM((PWM.Pin)FEZ_Pin.PWM.LED);

And the new code is

      PWM pwm= new PWM((Cpu.PWMChannel)EMX.Pin.IO48, 10000, 100, false);

But this throws an exception using EMX.Pin.IO48. I’ve tried all the Cpu.PWMChannel.PWM_x but to no avail.

Update - It looks like for the COBRA board Microsoft’s PWM throws an exception for any PWMChannel > 5. PWMChannels 0-5 are accepted but do not light up the COBRA LED. I guess this means I’m never going to be able to test this code as I don’t have another means to hook up a LED. Bummer …

Update - Now it looks like PWMChannel.PMW_4 and EMX.Pin.IO48 are the same since I tried to instantiate a PWM using PWMChannel.PMW_4 and then tried to instantiate an OutputPort using EMX.Pin.IO48 and the OutputPort threw an exception. When I changed the PWM Channel to 0, the OutputPort worked. Therefore I’m concluding that the above code does not work for pulsating a LED since PMWChannel.PWM_4 should be the COBRA LED. I think I might start a new thread on this if nobody responds.

@ SkierHiker

Microsoft PWM does not convert the actual pin to Cpu.PWMChannel. It actually incorporates an enumerated channel for the PWM Pins predefined in the source code, meaning that EMX.Pin.IO48 may be like channel Cpu.PWMChannel.PWM_0 or Cpu.PWMChannel.PWM_7. However, the Microsoft implementation does not account for channels above 7 so you will have to type cast any number above 7, example: (Cpu.PWMChannel)8 for channel 8.

For any further help can you repost any new code that you are trying to use?

@ SkierHiker -

So, it seems your problem is your board LED is on one pin that is not a PWM ?

Try to put an external LED and resistor to the PWM defined pins and tell us.

I’ve done a days worth of testing and here are my results.

First, for the Cobra II, the on-board LED is not on a PWM port so there’s no testing I can do with that board.

For the Cobra board, the following code turns on and off the LED (PWM4) with MF 4.1 using a duty cyle of 100 for on and a duty cycle of 0 for off.

PWM pwm = new PWM((PWM.Pin)FEZ_Pin.PWM.LED);
pwm.Set(10000, 100); // turn on
pwm.Set(10000, 0); // turn off

The following code does nothing in MF 4.2.

PWM pwm = new PWM(Cpu.PWMChannel.PWM_4, 10000, 100, false); // turn on (duty cycle = 100)
PWM pwm = new PWM(Cpu.PWMChannel.PWM_4, 10000, 0, false); // turn off (duty cycle = 0)

I tried all possible PWM channels (0-6) and different ways to set the duty cycle (pwm.DutyCycle = 100) but to no avail. Unless I’m missing something here, either the Microsoft PWM code does not work or the GHI implementation for 4.2 on the Cobra is faulty.

Can somebody else try this on a Cobra board and confirm it?

@ SkierHiker -

Is the following reference the right for COBRA, or the generic NETMF one ? :


Maybe you know that Channel and PIN is somehting different : PWM4 is not PIN4…