Driving motors with mbed using the L293D motor controller

Importing the Library

First off, if you are just looking to import and use an abstraction for the L293D, you can import it into your

If you want to use this abstraction, you can import it into your project from :

Getting something moving

The circuit is wired using pin 20 as an analogue input connected to the potentiometer used to control the speed and direction of the motor, and using pin 21 as a PWM output to the enable port 1 on the L293D motor controller chip. Ports 16 and 17 are used as digital outputs to switch the direction of the motor, and they are connected to inputs 1 and 2 of the motor controller. The motor is connected to the output pins 1 and 2 of the motor controller, with the chip power going to the Vss pin (16) and the motor power going to the Vc pin (8). the two middle pins are ground pins, these are connected to the ground rail.

The code that i wrote to control it is displayed below:

#include "mbed.h"

DigitalOut leftfwd(p16);
DigitalOut leftrev(p17);
PwmOut pwm(p21);
AnalogIn pot(p20);
int main(){
    pwm.period_ms(1);
    while(1){
        float potmod = pot - 0.5;
        if (potmod >= 0){
            leftfwd = 1;
            leftrev = 0;
            pwm=((1.8*potmod)+0.1);
        } else {
            leftfwd = 0;
            leftrev = 1;
            pwm = fabs((1.8*potmod)-0.1);
        }
    }
}

Great, that works. Now split out the motor functionality, sit has a nice clean interface.

Motor class

The natural interface for the motor class is a single function, speed(float), where -1.0 to 1.0 gives the range full speed reverse to full speed forward, and 0.0 being "stop".

#include "Motor.h"
#include "mbed.h"


/* 
 * Constructor
 */
Motor::Motor(PinName pwm, PinName fwd, PinName rev): 
    _pwm(pwm), _fwd(fwd), _rev(rev) {    
    
    // Set initial condition of PWM
    _pwm.period(0.001);
    _pwm = 0;

    // Initial condition of output enables
    _fwd = 0;
    _rev = 0;

}

    
/* 
 * Set the speed
 */
void Motor::speed(float speed) {
        _fwd = (speed > 0.0);
        _rev = (speed < 0.0);
        _pwm = abs(speed);
}

That looks good. Now for a test program for the class.

#include "mbed.h"
#include "Motor.h"

Motor m(p23, p6, p5);

int main() {

    // wind up to full speed
    for (float speed=0.0; speed < 1.0 ; speed += 0.01) {
       m.speed(spped); 
       wait(0.02);
    }


    while(1) {

        // Go from full speed forward, to full speed reverse
        for (float speed=1.0; speed > -1.0 ; speed -= 0.01) {
        m.speed(speed); 
        wait(0.02);

        // Go from full speed reverse, to full speed forward
        for (float speed=-1.0; speed < 1.0 ; speed += 0.01) {
        m.speed(speed); 
        wait(0.02);
    }
}


Yup, that works nicely.

Development Log

found http://www.me.umn.edu/courses/me2011/robot/technotes/L293/L293.html, useful resource for the L293D chip, good circuit diagram http://hades.mech.northwestern.edu/wiki/index.php/Pulse_width_modulation - good, if rather technical explanation of Pulse Width Modulation

got it running one way by plugging the negative wire into the ground rail and the positive in pin 3

potentiometer now works, made using the schematics in the handbook under 'AnalogIn'. slight scaling factor needed to allow the use of the full potentiometer to give the full range of 0 to full speed, this was found using an oscilloscope.

now have the motor running in both directions, using the enable as the pwm input and setting the two input pins to either 1/0 or 0/1 depending on the direction of travel, this is currently set by a toggle switch hardwired to the motor controller chip

The switch is now an input to the mbed board, using the switch as digital input, and outputting the values from the chip to a digital output pin to then go to the input pins on the motor controller. Some initial problems with the motor either running backwards or not running, but this was due to the wires being connected to the wrong pins on the mbed board.

Have attached a second potentiometer and switch to control a second motor in the same way as the first.