Main Site Documentation

PWM problem


Hi guys. Hope you can help.
I am trying to make a simple robot move in all directions. I have 5 possible combinations to make the robot manuver: forward, reverse, left, right and full stop.
There are 2 VEX motor modules. (3 wires: +, - and signal wire).
According to VEX site: 'The Vex Motor will require about 700us of low time on the control signal input before the next pulses rising edge. Therefore the total pulse time is about 1.7ms to 2.7ms yielding an input frequency rate of about 370 Hz to 588 Hz.'
That is straight forward and very easy to implement using the PWM class.
Problem: when in ‘reverse’ only one of the motors works. All other combinations work.
Is there a problem with the PWM class? Running latest firmware.
I really want to use the PWM class to take advantage of the hardware PWM.

private static PWM leftMotor = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);
private static PWM rightMotor = new PWM((PWM.Pin)FEZ_Pin.PWM.Di9);

Code to set robot direction:
private static void SetDirection(Direction newDirection)
if (newDirection == CurrentDirection)

        Debug.Print("new direction: '" + newDirection.ToString() + "'\r\n");

        CurrentDirection = newDirection;

        if (newDirection == Direction.None)
            leftMotor.SetPulse(0, 0);
            rightMotor.SetPulse(0, 0);
        else if (newDirection == Direction.Forward)
            leftMotor.SetPulse(1700 * 1000, 1000 * 1000);
            rightMotor.SetPulse(2700 * 1000, 2000 * 1000);
        else if (newDirection == Direction.Reverse)
            leftMotor.SetPulse(2700 * 1000, 2000 * 1000);
            rightMotor.SetPulse(1700 * 1000, 1000 * 1000);
        else if (newDirection == Direction.Left)
            leftMotor.SetPulse(2700 * 1000, 2000 * 1000);
            rightMotor.SetPulse(2700 * 1000, 2000 * 1000);
        else if (newDirection == Direction.Right)
            leftMotor.SetPulse(1700 * 1000, 1000 * 1000);
            rightMotor.SetPulse(1700 * 1000, 1000 * 1000);


Use the code button please when including code !!! (it’s the icon with the 0’s and 1’s)


All pwm channels share the same clock. All your setpulse calls need to set the same period then only change the high pulse time for each one

Please use code tags do we can read your code


Sorry about the formatting.
Not sure I understand ‘set same period and then change high pulse time’.
Each motor needs a diffrent period. How can I set one for both? Can you please show me a code sample ?


Servos are controled by the high pulse width and this is the only variable you need to change. Leave period set to something fixed


(link removed)


The 2 modules I’m using are motors not servos. From what I understand a servo can ‘stop’ at the angle specified by the pulse. The motors I’m using turn in the direction and at the speed specified by the pulse without stopping.
I’ll try to use the same period and modify the duty cycle to approximate the specs.
Or use OC or re-arrange motors so when going forward and reverse they use the pulse. Turning doesn’t have to be precise.
Thanks for the replies. Let me know if I’m wrong in my understanding of a servo vs a motor. (They both are dc motors but the circuits make turn to a precise angle vs control speed and direction)


Yes and they will stop at specific frequency.