Motor

An interface for driving a standard DC motor with PWM and an H-Bridge

Hello World!

» Import this program

00001 // Sweep the motor speed from full-speed reverse (-1.0) to full speed forwards (1.0)
00002 
00003 #include "mbed.h"
00004 #include "Motor.h"
00005 
00006 Motor m(p23, p6, p5); // pwm, fwd, rev
00007 
00008 int main() {
00009     for (float s= -1.0; s < 1.0 ; s += 0.01) {
00010        m.speed(s); 
00011        wait(0.02);
00012     }
00013 }

Library

» Import this library into a program

Public Member Functions

  Motor (PinName pwm, PinName fwd, PinName rev)
  Create a motor control interface.
void  speed (float speed)
  Set the speed of the motor.

Example

This example show you how to wire up the L293D dual H bridge driver. Data sheet can be found at:

/media/uploads/chris/_scaled_l293d.png

some minor additions to the libary above, resulted in this libary. It has been tested on L298 motor drivers, but uses the same method of speed control.

» Import this library into a program

Public Member Functions

  Motor (PinName pwm, PinName fwd, PinName rev, int brakeable)
  Create a motor control interface.
float  speed (float speed)
  Set the speed of the motor.
void  coast (void)
  Set the the motor to coast.
float  stop (float duty)
  Set the motor to dynamicaly brake.
float  state (void)
  return the current state of the motor

in terms of use,

the speed function takes a value from 1 to -1. full foward to full reverse. however it will not switch from foward to reverse or reverse to foward without going through speed = 0, coast or stop. this stops the motor from going from full one direction to the other, and the large current draw associated with this. it returns the duty on the PWM line.

the coast function means the motor driver switches off, and does nothing.

the stop function dynamicaly brakes, which is supported on L298's but if dynamic braking develops more current than the motor drivers are rated for, things start to fail. read data sheet, and if in doubt, don't. it returns the duty on the PWM line, or -1 if you didn't set brakeable to 1.

main.cpp

// test code, this demonstrates working motor drivers. 
// full reverse to full stop, dynamicaly brake and switch off.
#include motordriver.h
Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can brake 
Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can brake
int main() {
    for (float s= -1.0; s < 1.0 ; s += 0.01) {
        A.speed(s); 
        B.speed(s); 
        wait(0.02);
    }
    A.stop();
    B.stop();
    wait(1);
    A.coast();
    B.coast();
}

data sheet for L298

conection diagram, note the current sense pins(1,15) should be connected to ground through a 0.5 Ohm resistor. attaching to analog inputs is not required.

/media/uploads/littlexc/_scaled_l298.png





32 comments:

10 Jun 2010

Hi, how can I get the "Motor.h" header file? Thanks

10 Jun 2010

Well...I see.

22 Jun 2010

Hi, how can I get the "Motor.h" header file? Thanks

22 Jun 2010

Seriously? Look above.

16 Sep 2010

How about a coast() and brake() feature for some of the newer motor driver ICs that support it. Might need a driver type that defaults to not supported (to avoid smoke)

http://www.pololu.com/file/0J51/vnh3sp30.pdf is one of the new ones that has it.

05 Nov 2010

I am confused (newbie to C++) I don't see where the controller is told to start the motor in reverse then go to forward. All I see is a for loop increasing speed. ??? The library class is very cryptic to me. Please explain....

06 Nov 2010

Hi Robert, I pulled in the API reference so you can see the interface better. -1.0 is full speed reverse, and 1.0 is full speed forwards, and the example sweeps between these.

18 Nov 2010

@Ali Talib "How can I get the "Motor.h" header file?" - what do you mean? Header file?? If you need to implement the lib in your sauce - right click your project folder in the compiler and import the lib (or wite a new). Then #include it to use it.

21 Dec 2010

Hi,

When looking at the datasheet for the STM L298 (Figure 6, Page 6/13, "Bidirectional DC motor control"), they seem to indicate that an external bridge of diodes is required. This is stated on page 7.

I don't see that bridge in the schematic above.

Where's the catch ? Has it been forgotten ? Isn't it needed ? Not always ?

Sorry for the electronic-newbie-style question, I'm only a poor software engineer trying to get at hardware matters ;-)

Thanks,

Olivier

13 Jan 2011

user Olivier Smeesters wrote:

Hi,

When looking at the datasheet for the STM L298 (Figure 6, Page 6/13, "Bidirectional DC motor control"), they seem to indicate that an external bridge of diodes is required. This is stated on page 7.

I don't see that bridge in the schematic above.

Where's the catch ? Has it been forgotten ? Isn't it needed ? Not always ?

Sorry for the electronic-newbie-style question, I'm only a poor software engineer trying to get at hardware matters ;-)

Thanks,

Olivier

Hi Olivier,

Using STM L289 you have to use fast recovering diode bridge. Not using them you could destroy L289.

Igor

19 Jan 2011

L293D include this diodes. the b model need external

12 Feb 2011

Hi, I look at this image:

http://mbed.org/media/uploads/chris/_scaled_l293d.png

And I've a few questions:

  • Should pin 4, 5, 12 and 13 be connected to the GND pin of the MBED?
  • What should be connected to pin 8 and 16?
12 Feb 2011

pwr for the motor dyrecly p8 (Vcc2) e.g. if your motor run on 24 V here will be the 24 V and Vcc1 is the signal voltage. If I am ambiguous with my answer please say and I will reformulate

12 Feb 2011

My motors run on 3.3V, so I have to connect an 3.3V battery to p8/VCC2? But a battery have a positive and a negative pole. What should be connected to the negative pole of the battery? Or do I thing wrong??

And what is 'the signal voltage'??

24 Feb 2011

Hi,

I'm currently trying to drive small motors with an mbed and L293D chips. The motors run under 6.8V. They eat about 160mA under normal condition. When putting the motor power supply straight to the motors, they run perfectly. When connecting the motor to through the L293D, they don't start. In that condition, they consume 20-30mA. If I physically connect the motor after enabling the motor driver, sometimes it starts, sometimes not. Playing with the plug sometimes causes it to start.

I'm really wondering what I can do to have the motors and the drivers to work together.

Any suggestion is more than welcome. Thanks in advance.

24 Feb 2011

Can you post how did you connect the motor, mbed and l293d to the power source? do you have on the l293d enought power from the source to motor suply?(6.6V?) how about the logic suply?

15 Jul 2011

I'm really wondering what are the "current sensing pins A - B" (pins 1 and 15) used for? How do they work? or what have you used them for ?

Richy

15 Jul 2011

@Richy: You are talking ablout the L298, not the L293? The current sense pins allow you to measure the current through the motor coils. When you also use an L297 its use is straight forward, look at the apnotes. Normally you connect these pins to ground via resistors of less than 1 Ohm. You can filter and measure the voltage across these resistors with the analog inputs of the MBED. You can then decide to adjust the duty cycle or decide that the motor is stalled and cut the power, reset the position, whatever.

26 Oct 2011

If you need a small low-cost dual H-bridge on a breakout board for 1A DC motors, here is a nice option from Pololu that I have used with mbed and it works with the motor code. Also has the brake feature. It uses Toshiba’s TB6612FNG dual motor driver that has MOSFETs. MOSFET-based H-bridges are much more efficient than the BJT-based H-bridges used in older drivers such as the L298N.

/media/uploads/4180_1/_scaled_magician_mt.jpg

http://www.pololu.com/catalog/product/713

http://mbed.org/users/4180_1/notebook/sparkfun-magician-robot-base-kit/ has an example using it to control two DC drive motors on a robot.

05 Nov 2011

If you notice a hum noise on the motors and would like to reduce it, try decreasing the PWM period (increases the frequency) in the motor library code. The default value seems to be 1Khz. Most H-bridges will switch ranging from 10Khz to 100Khz, so you can go faster and that will keep the frequency of most of the harmonics above the human audio range. Check the H-bridge data sheet to be sure.

10 Nov 2011

Connection Diagram for mbed with L298 are not very clear. can anyone help how can i have a better view??

11 Nov 2011

Found the unscaled versions in their upload directories:

http://mbed.org/media/uploads/littlexc/l298.png



http://mbed.org/media/uploads/chris/l293d.png

25 Jan 2012

An external decoupling capacitor of a few hundred uf might be a good idea near the H-bridge power pins to smooth out the power supply voltage drop when motors first switch on (i.e., a short duraction large inrush current spike occurs on DC motors). Some H-bridge breakout boards already have this capacitor, but most do not. This can be critical when the same supply runs both the mbed module and the motors.

26 Mar 2012

I used the code above in Motor_HelloWorld with my Dagu Rover tracked chassis. I seem to have a power problem as I can stop the track wheels with the lightest touch of my hand. The two motors have a 7.2v rating and they are certainly getting this from a pack of 6 NiMh 2400 mA/h cells and also six duracell AA. What frequency and duty cycle are produced by this code? I'm using an SN754410 driver chip which is pin compatible with L293. I cant understand why the crawler stalls at the slightest resistance.( I know the voltage is too high but I'm running this only for a few seconds to verify my circuit). Any ideas ?

26 Mar 2012

The hello world code spins the motor up to full "speed" fowards, from full reverse. It does this by altering the avalible torque, making the motors have very little torque in the middle section of the code. Change the code to

{ m.speed(1); while(1){} }

and then see if it stalls, also note that that makes your robot drive fowards until the end of time.

The speed function alters duty between 0-1, and at 1kHz frequency. This can be modified to whatever suits the motor, up to the maximum switching frequency of the motordriver, however for the things I've played with 1kHz works fine.

27 Sep 2012

my compiler isn't recognizing the "Motor.h" instruction, it says no such file or directory..... anybody know the reason for this?

27 Sep 2012

Ali,

Instead of copy and pasting the code click "Import this program", it will import all of the code into the compiler for you including motor.h

Steve

30 Sep 2012

thankyou steve, do you have the schematics for connecting a dc motor to the mbed by any chance

30 Sep 2012

@ jordan . You can use schematics provided by Jim Hamblen , a few posts above . The idea is you use an interface ( a bridge driver ) to control your motor(s) like L298 . Microcontroller instructs the bridge driver ( using 3.3 V output ) to power the motor(s) using 12V or more ... http://www.sparkfun.com/datasheets/Robotics/L298_H_Bridge.pdf

29 Nov 2012

hi,I try to build the circuit as above, but the motor did not work. so i want to confirm the pwm is working. do u know how to detect the pwm? thank you very much.

29 Nov 2012

If you dont have measurement equipment, easiest way is to switch the PWM output (first argument in the constructor) to one of the mbeds LEDs. So just instead of p23 in this example use LED4 for example.

29 Nov 2012

You can detect the PWM with an oscilloscope. The pwm that you are send you have to send trough pwm ports. I made for PWM an H-bridge to control the thrusters. because the high current consumption (aprox 50A at full load) I had to use certain components (IRF) that in load at 3.3V from the mbed open the port and block the thruster, so I have to build a bridge between. Thank to a friend this is the scheme for it.

https://mbed.org/cookbook/AUV-Robot#c3299 With this the PWM works fine.