8 years, 11 months ago.

How change the duty cycle of a pwm signal?

Hi, we are working on a project and we need to change the duty cycle, we did try different sintaxys but the motor keep runin' in the same rpm even when the values we set are totaly different. We are using a FRDM-KL25Z This is the code if it helps:

  1. include "mbed.h"

PwmOut PWMa1(PTC3);

PwmOut PWMa2(PTC4);

PwmOut PWMb1(PTC1);

PwmOut PWMb2(PTC2);

DigitalOut menable(PTE21);

int main() {

PWMa1.period(0.05); set PWM period to 5 ms

PWMa2.period(0.05);

PWMb1.period(0.05);

PWMb2.period(0.05);

while(true){

PWMa1.write(0.01); we change the values here

PWMa2.write(0.0);

PWMb1.write(0.01);

PWMb2.write(0.0);

wait(1);

menable=1;

}

}

if you have a simple code to show how to change the duty cycle it would be so helpful.

Thanks and regards

Using <<code>> and <</code>> around your code makes it easier readable, and makes you not have to add so many enters.

Your syntax is correct, so that should work, How are the motors wired?

posted by Erik - 17 May 2015

Thanks man, the motors are wired to a shield that contains an h-bridge, so they have to be working right, i think i'm going to check the signal in a osciloscopy.

posted by César Fierro 19 May 2015

That would be a good idea. Also try setting the PWM pin either with DigitalOut or by just moving a jumper for example to 0V and 3.3V, to make sure that indeed does modify the motor speed.

Also which values did you try out? If your motor driver is inverting, it would run between 100% and 99% speed with the code you have shown here, which you would obviously not notice to be different.

posted by Erik - 19 May 2015

We varied the value since 0.01 until 0.05, but if the driver would be inverted, the values enterig into the bridge h would be 1-1 in both of them wich is incorrect and the motor wouldn't be moving, am i wrong? Is something wrong with the values we are setting? Cheers

posted by César Fierro 21 May 2015
Be the first to answer this question.