11 years, 2 months ago.

Deactivate PWM

Hi,

I have two mbeds connected at their PWM pin p21 to one motor. In some situations one of the mbeds is supposed to run the motor and in others the other mbed. Is there a way to deactivate the PWM of one of the mbeds while the other one is sending? Just connecting them and setting the pulsewidth to 0 does not seem to work -> the motor does not move at all.

Thanks you for your support.

Best Regards

Arasch

3 Answers

11 years, 2 months ago.

Really disconnecting one mbed with some kind of switch is one option, something else you can consider is the following code:

#include "mbed.h"

int main() {
    while(1) {
        PwmOut *pwm = new PwmOut( LED1 );
        for (float f = 0; f<1; f=f+0.01) {
            pwm->write(f);
            wait(0.02);
        }
        for (float f = 0; f<1; f=f+0.01) {
            pwm->write(1-f);
            wait(0.02);
        }
        delete pwm;
        DigitalIn *ncpwm = new DigitalIn( LED1 );
        ncpwm -> mode(PullUp);
        wait(0.5);
        ncpwm -> mode(PullDown);
        wait(0.5);
        ncpwm -> mode(PullUp);
        wait(0.5);
        ncpwm -> mode(PullDown);
        wait(0.5);
        delete ncpwm;
    }
}

It dynamically creates and deletes the PWM/input pin. When it is an input the other mbed can control it. Of course you need to communicate between them which one is running it, and they both need to switch correctly then between input (high-Z) and PwmOut.

That code I used to test it to be sure it works correctly. When in PullUp mode there is a slight current through the LED, so if you watch closely you should see it flashing a little bit.

Accepted Answer
11 years, 2 months ago.

What if you use a transistor as a switch (between mbed and motor), to cut off the output of the mbed when the other is sending? Seems easy to implement.

11 years, 2 months ago.

Thanks Sergio and Eric,

following Sergio's idea, I was trying different NPN transistors and diodes. But the transistors and Diodes (even if I just use one signal over a diode while the other is disconnected) the signal gets too much changed (it is no square signal anymore) and the motor does not move.

I will try now Eric's way.

Best Regards

Arasch

Think Eric's ideia is good. Since I haven't explorer many of the mbed's capacity, wasn't sure if possible to simple deactivate that way.

The way I was saying would be with MOSFET/JFET, with drain and source to mbed and motor, while the gate would be controlled by the other mbed. Anyway, since they may need to be correctly biasing, by software is way more simple.

posted by Sergio Parreira 10 May 2013

Thanks. Erics way works pretty good. I have following two functions to activate and deactivate on mbed #1 and on mbed #2 the motor control:

void DriveMotor::deactivatePWM(){
    delete pwmservoL;
    delete pwmservoR;
    DigitalIn *ncpwmservoL = new DigitalIn(p22);
    DigitalIn *ncpwmservoR = new DigitalIn(p21);
}
//----------------------------------------------------------------------
void DriveMotor::SetupPWM(){
    PwmOut *pwmservoL = new PwmOut( p22 );
    PwmOut *pwmservoR = new PwmOut( p21 );
    pwmservoL->period(0.020);
    pwmservoR->period(0.020);
}

At the beginning I set for both mbeds the ports as PWM with

    PwmOut *pwmservoL = new PwmOut( p22 );
    PwmOut *pwmservoR = new PwmOut( p21 );

//using namespace ns1;

//constructor
DriveMotor::DriveMotor() {
//start location in the memory matrix in in the middle of the matrix
    pwmservoL->period(0.020);
    pwmservoR->period(0.020);
    LeftSpeed = 0;   //midval;             //Start left with stop pulsewidth...
    RightSpeed = 0;  //midval;            //Start right with stop pulsewidth...
}

once one of the mbeds is called by user to drive forward the other takes the command to deactivate its PWM port.

posted by Arasch Lagies 10 May 2013