Main Site Documentation

PWM Motors in TinyCLR


I read the tutorial, which addresss leds, sound, and servos

I am trying to drive 4 standard motors using two L298MotorDriver modules on a Fez Reaper.

This is my test function:

static void TestMotor()
    GHIElectronics.TinyCLR.Devices.Pwm.PwmController C1 = GHIElectronics.TinyCLR.Devices.Pwm.PwmController.FromId(GHIElectronics.TinyCLR.Pins.FEZReaper.PwmPin.Controller1.Id);
    GHIElectronics.TinyCLR.Devices.Pwm.PwmPin F1 = C1.OpenPin(GHIElectronics.TinyCLR.Pins.FEZReaper.PwmPin.Controller1.Socket4.Pin7);
    GHIElectronics.TinyCLR.Devices.Pwm.PwmPin F2 = C1.OpenPin(GHIElectronics.TinyCLR.Pins.FEZReaper.PwmPin.Controller1.Socket4.Pin8);

    GHIElectronics.TinyCLR.Devices.Pwm.PwmController C4 = GHIElectronics.TinyCLR.Devices.Pwm.PwmController.FromId(GHIElectronics.TinyCLR.Pins.FEZReaper.PwmPin.Controller4.Id);
    GHIElectronics.TinyCLR.Devices.Pwm.PwmPin R1 = C4.OpenPin(GHIElectronics.TinyCLR.Pins.FEZReaper.PwmPin.Controller4.Socket12.Pin7);
    GHIElectronics.TinyCLR.Devices.Pwm.PwmPin R2 = C4.OpenPin(GHIElectronics.TinyCLR.Pins.FEZReaper.PwmPin.Controller4.Socket12.Pin8);

    C1.SetDesiredFrequency(1 / 0.020);

    C4.SetDesiredFrequency(1 / 0.020);
    R1.Polarity = GHIElectronics.TinyCLR.Devices.Pwm.PwmPulsePolarity.ActiveHigh;

    R2.Polarity = GHIElectronics.TinyCLR.Devices.Pwm.PwmPulsePolarity.ActiveLow;

The two motor driver modules are connected to the reaper’s Socket 4 and Socket 12

I can indeed turn all 4 motors on with this function - however, two of the motors (R1 and R2, on socket 12) need to go in the reverse direction.

I can’t find any way to drive the motors in reverse. I’ve tried changing the “polarity” but it seems to have no effect. Duty cycle goes from 0 to 1, but it is always in one direction.

With gadgeteer, I was able to get the wheels going in either direction, but not with Tinyclr, so I’m not sure what the issue is.

Any ideas?


Maybe of some help. Not sure if solved. This was in NETMF


Hmm. I have played with various frequencies, but I can try some different ones.

The duty cycle’s range is from 0.0 to 1.0 - I can successfully change the overall speed of each motor, from stopped to full speed. Setting it negative throws an exception. The only way I can see that you are supposed to use to get it to go the other direction (based on method and property names) is maybe setting the polarity to ActiveLow, as in

R2.Polarity = GHIElectronics.TinyCLR.Devices.Pwm.PwmPulsePolarity.ActiveLow;

Does anyone know if that is the correct, intended approach?

This doesn’t really make sense to me - to me that would mean 0 was full speed and 1.0 was stopped - not reverse. In any case it isnt causing reverse motion.

Could it just be a TinyCLR bug that the Polarity value is not properly used? Or is there something obvious I am missing about how to specify speed in the opposite direction that I am not seeing?

These exact same motors have been used with these exact same motor control modules previously. In fact the only differences are a] Using a Fez Reaper now instead of a Raptor, and b] using TinyClr instead of Gadgeteer.

So it isnt the motors or the driver modules - it is either a] Im just doing something wrong (most likely) b] a bug in TinyCLR, I think.

It isn’t hardware unless there is an incompatibility between the reaper and the motor driver modules

Edit: The Polarity attribute is for what I thought it was - if you set the polarity to ActiveLow, then 0.0 is full speed and 1.0 is stopped. So that isnt what I needed.

There does not seem to be any way to specify reverse speed, like there was in .netmf/gadgeteer


it’s not a bug in tinyclr

You need to look at how the L298 driver chip expects to be controlled. Did you look at the old Gadgeteer module source to see what happens? Direction is controlled through digital output pins.


AH, good. I didn’t think it would be a bug.

SO pins 7 and 8 are for the two motors on the controller, and pins 6 and 9 are the associated directional pins?

If so, do I need to just open and write the associated pins 6 and 9 directly via GPIO, or are they somehow integrated into the PWM classes?


just read the driver, you’ll see everything you need