Driving 2 DC motors using L293D


36 posts   Page 1 of 2   1, 2
by cupcake » Sat Jul 07, 2012 9:32 pm
This thread can be considered an off-spring of this thread.

I want my RPi to drive the 2 5.4-6V DC motors that are provided with the Mr. Basic rover platform. I intend to do so by powering them by a separate 3xAA battery pack (provided with the Mr. Basic) and driving them using the L293D motor driver. It should allow me to drive 2 motors and be able to go forward/reverse. The RPi GPIOs will then simply act as controller signals. The setup will be similar to what is presented here.

In addition, I intend to throw in a few capacitors to stabilize current. As in the example found here, I'll be using something in the size of a 10 uF capacitor for the power source and 220nF for the motors.

Before I go ahead and order the components, I'd like to verify that I've understood things properly and that this will actually work with the RPi. Is there something I'm missing or something that can be done better/simpler?

I'd also like to be able to change the rover platform from the Mr. Basic to a bigger one later on. Is there something I should keep in mind for my setup to allow for different DC motors later on?
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm
by exartemarte » Sat Jul 07, 2012 10:08 pm
Personally I wouldn't use an L293D to drive those motors. If I remember rightly, The L293D is rated at about 600mA, with 1A peak. Your motors have a stated stall current of 1.8A, so you really need a driver which is okay with 2A at least. The L298N would be better, but the bare chip is not breadboard compatible so you would need a module like this (unless you're making your own pcb). Note that one controller can control two dc motors.

Apart from that you've got the right idea. Be very careful with your wiring, though - you don't want that 4.5-6v getting back to the Pi. The motor controller's low-level inputs should be connected to Pi outputs and there must be a common ground - that's all.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by cupcake » Sat Jul 07, 2012 10:29 pm
Good points about the amps, exartemarte. Didn't think of that.

Are there any valid alternatives to L298N for motor controllers that handle > 1.8 A?
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm
by exartemarte » Sat Jul 07, 2012 10:38 pm
There's a reasonably clear diagram here which should give you the general idea. Imagine a Pi in place of the Arduino, and ignore references to Bluetooth.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by exartemarte » Sat Jul 07, 2012 10:59 pm
cupcake wrote:Are there any valid alternatives to L298N for motor controllers that handle > 1.8 A?

L293 and L298 seem to be the standard one-chip solutions for small motors. You can certainly get motor controllers for much higher voltages and currents from specialist suppliers, such as 4QD, popular in the Robot Wars community, but be warned - the prices rise faster than the amperage!

If you Google something like diy motor controllers you'll find articles on how to build your own.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by cupcake » Sun Jul 08, 2012 10:50 pm
Ordered L298N. Thanks for your help!
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm
by robin1508 » Thu Jul 26, 2012 11:18 pm
Can you connect the L293D directly to a GPIO Pin? Well the datasheet tells me that it's input pins are at 5V and the GPIO is at 3,3?

How do you do that?
Posts: 4
Joined: Thu Jul 26, 2012 11:16 pm
by Gert van Loo » Fri Jul 27, 2012 7:29 am
Any 5V LOGIC input can be driven from 3V3.
The logic HIGH is defined as 2.7V which voltage can be reached with 5V or 3V3.
It is the main reason why 3V3 is so commonly used: It is the lowest supply voltage you can have and still interface with 'old' 5V logic.
In telephones and other mobile equipment you see more and more 1V8 as new I/O voltage.
User avatar
Posts: 2389
Joined: Tue Aug 02, 2011 7:27 am
by cupcake » Mon Jul 30, 2012 12:52 am
I now got the L298N extension board that extramarte suggested earlier.

I've got it somewhat working. By that I mean that I'm able to turn on/off 2 DC motors and change their directions by changing HIGH/LOW on 4 different GPIO ports.

However, I'm noticing that the motors are running much slower when powered through the L298N board. They are significantly slower than if I just power them directly with 3xAA batteries with nothing in between.

Here are 2 photos of how I've set it up:

Image

Image

Here's a link to the L298N board with close-up photos.

In short:
- Battery pack connected to L298N ports labeled "+12V" and "GND"
- I've left port "+5V" unused and "5V enable" is left as is
- One DC motor connected to OUT1/2 and the other to OUT3/4
- ENA and ENB are left as-is (will control these with PWM later)
- IN1-4 are connected to 4 RPi GPIOs
- RPi ground is connected to battery pack and L298N grounds

I'm no expert on this and the confusion of the 5V enable stuff on the ebay link is confusing me.

Anyone see what I may be doing wrong?
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm
by Grumpy Mike » Mon Jul 30, 2012 3:25 pm
cupcake wrote:However, I'm noticing that the motors are running much slower when powered through the L298N board. They are significantly slower than if I just power them directly with 3xAA batteries with nothing in between.

Yes they will be.

Using 3 AA batteries will give you 3 X 1.5 = 4.5V.
There is a voltage loss in the switching transistors of that chip of about 2V so in effect you will only be driving the motor with 2.5V.
The answer is to feed the chip with a higher voltage.
User avatar
Posts: 866
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
by cupcake » Mon Jul 30, 2012 5:40 pm
Using 3 AA batteries will give you 3 X 1.5 = 4.5V.
There is a voltage loss in the switching transistors of that chip of about 2V so in effect you will only be driving the motor with 2.5V.
The answer is to feed the chip with a higher voltage.


Interesting. I thought I measured the voltage on the motors when fed through the L298N and that I got pretty much the same results (i.e. around 4.5V). I didn't know the L298N would leak that much.

But thanks for the response. I'll try that.
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm
by exartemarte » Mon Jul 30, 2012 6:18 pm
cupcake wrote:I didn't know the L298N would leak that much.

It's not leakage, exactly.

If you use a resistor or a transistor to limit or control the current through a device there will be a voltage drop across the resistor or transistor (which will get warm - that's why the chip on your controller has a heatsink). In an h-bridge motor controller there will be two transistors in series with the motor (which two depends on direction), and the voltage drop is typically 1.5 - 2v.

If you were using, say, 48v motors (which is not practicable on a small robot) then the drop wouldn't matter too much, but with 4.5 - 6v motors it clearly does.

When I use 6v motors with pwm I use a 9v supply (or 8.4v with rechargeables). If you're not using pwm, just switching the motors on and off and reversing direction, then 7.5 volts would be kinder to your motors.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by cupcake » Fri Aug 03, 2012 6:03 pm
Follow-up question:

The L298N allows me to control the speed of each separate motor using PWM. However, the RPi only allows one of the GPIOs to be used for PWM (GPIO 18). I see in the wiki that there may be plans to add a second PWM GPIO, but AFAIK there's only one available now.

So I can use the single PWM GPIO to control the speed of both motors at the same time. However, for turning, I'll have to make a full stop on one of the motors and run the other motor to have the rover rotate around its own axis. In other words: It can't move forward and turn at the same time.

Is there any way to control the speed of the two motors separately with only one PWM?
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm
by exartemarte » Fri Aug 03, 2012 11:24 pm
cupcake wrote:Is there any way to control the speed of the two motors separately with only one PWM?

The short answer is no. The long answer is possibly, but it's complicated and messy and it would be simpler to use an additional microcontroller (Arduino, etc) to provide the pwm.

However, you probably wouldn't have much joy trying to steer by driving the two sides at different rates anyway, nor by stopping one side and driving the other, so you might as well just stick with the Pi. Either of those techniques works with a three-wheel configuration (two driving wheels and a castor) but not with four wheels. With four-wheel drive on a small robot you can drive forward or reverse, or turn on the spot by driving forward on one side and reverse on the other, and that's about it.

Image

You can vary the speed of course, using pwm, but if you try to drive one side more slowly the other side will just drag it along. The little beastie above travels in an almost straight line even when only one side is driven. To make it do anything else I have to put one side into reverse.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by nvhs » Mon Dec 31, 2012 5:47 am
hello you shoul use my motor controller ù_ù

http://nvhs.wordpress.com/project/catsp ... ontroller/

you can make it whit a relay board ù_ù
Posts: 12
Joined: Thu Dec 06, 2012 1:07 am
by nvhs » Mon Dec 31, 2012 5:55 am
hello you shoul use my motor controller ù_ù

http://nvhs.wordpress.com/project/catsp ... ontroller/
Image

you can make it whit a relay board ù_ù
Posts: 12
Joined: Thu Dec 06, 2012 1:07 am
by nvhs » Tue Feb 19, 2013 5:32 pm
helo thise is my robot from with l9110 motor controller an java client

http://nvhs.wordpress.com/project/catspberry-2/

Image
Posts: 12
Joined: Thu Dec 06, 2012 1:07 am
by exartemarte » Tue Feb 19, 2013 5:59 pm
Nice project. I would be interested to know how your camera link to the pc works.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by idem » Tue May 14, 2013 12:27 am
Hi all, hi cupcake,

I got the same board with L298N.
Have you solved your problem?
Have you some python code?

Thanks for all

http://www.lo-geek.fr
Posts: 12
Joined: Mon Feb 18, 2013 3:23 pm
by jdawgaz » Wed Aug 14, 2013 10:39 pm
RE: the pwm question.

the RPi.GPIO module for python now supports software pwm.
I have used it successfully to blink/fade an led on a regular GPIO pin (not the pwm designated one)

I just started working with a L293D today. Have written the program, but have yet to wire up the breadboard.
Will get to that maybe tomorrow or the day after and let you know.

I will post my code when I get it working.
Posts: 5
Joined: Sat Apr 06, 2013 10:36 pm
by LaWFuLEviL » Fri Dec 20, 2013 2:38 am
exartemarte wrote:
cupcake wrote:Is there any way to control the speed of the two motors separately with only one PWM?

The short answer is no. The long answer is possibly, but it's complicated and messy and it would be simpler to use an additional microcontroller (Arduino, etc) to provide the pwm.

However, you probably wouldn't have much joy trying to steer by driving the two sides at different rates anyway, nor by stopping one side and driving the other, so you might as well just stick with the Pi. Either of those techniques works with a three-wheel configuration (two driving wheels and a castor) but not with four wheels. With four-wheel drive on a small robot you can drive forward or reverse, or turn on the spot by driving forward on one side and reverse on the other, and that's about it.

Image

You can vary the speed of course, using pwm, but if you try to drive one side more slowly the other side will just drag it along. The little beastie above travels in an almost straight line even when only one side is driven. To make it do anything else I have to put one side into reverse.


How do you use the L298N H-Bridge to control two motors from one PWM pin on RPi ????
Posts: 5
Joined: Fri Dec 20, 2013 2:32 am
by exartemarte » Fri Dec 20, 2013 2:42 pm
LaWFuLEviL wrote:How do you use the L298N H-Bridge to control two motors from one PWM pin on RPi ????

I probably wouldn't - see below. The little buggy in the picture is using an Arduino Nano, with multiple pwm's, and an L293D. What I had in mind when I answered the question above was just to combine the enable inputs for the two motors and connect them both to the one available PWM o/p on the Pi. I haven't actually done this, but I can't see why it shouldn't work. In principle both motors will run at the same speed: in reality they're unlikely to be perfectly matched so there will a slight difference.

Nowadays, if I needed to control two motors from the Pi without additional hardware (apart from the motor driver) I'd try Joan's pigpio. I used it to control multiple servos and it was fine.

I am currently playing with a PI/Arduino combo (using spi, but that could change), which I hope will serve for most of my robotics projects.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by LaWFuLEviL » Fri Dec 20, 2013 10:18 pm
exartemarte wrote:
LaWFuLEviL wrote:How do you use the L298N H-Bridge to control two motors from one PWM pin on RPi ????

I probably wouldn't - see below. The little buggy in the picture is using an Arduino Nano, with multiple pwm's, and an L293D. What I had in mind when I answered the question above was just to combine the enable inputs for the two motors and connect them both to the one available PWM o/p on the Pi. I haven't actually done this, but I can't see why it shouldn't work. In principle both motors will run at the same speed: in reality they're unlikely to be perfectly matched so there will a slight difference.

Nowadays, if I needed to control two motors from the Pi without additional hardware (apart from the motor driver) I'd try Joan's pigpio. I used it to control multiple servos and it was fine.

I am currently playing with a PI/Arduino combo (using spi, but that could change), which I hope will serve for most of my robotics projects.


I came across this post which shows L293D with two motors and one PWM : http://roverpi.blogspot.com/2013/07/con ... -with.html
Posts: 5
Joined: Fri Dec 20, 2013 2:32 am
by exartemarte » Sat Dec 21, 2013 9:46 am
LaWFuLEviL wrote:I came across this post which shows L293D with two motors and one PWM : http://roverpi.blogspot.com/2013/07/con ... -with.html

Yes, that's the sort of thing I was suggesting.
User avatar
Posts: 359
Joined: Sat Mar 03, 2012 3:51 pm
Location: Middle England
by mikronauts » Sat Dec 21, 2013 12:50 pm
WiringPi also has a soft PWM that can control multiple pins; I used it last week with a L293D - it would also work with the L298

exartemarte wrote:
LaWFuLEviL wrote:How do you use the L298N H-Bridge to control two motors from one PWM pin on RPi ????

I probably wouldn't - see below. The little buggy in the picture is using an Arduino Nano, with multiple pwm's, and an L293D. What I had in mind when I answered the question above was just to combine the enable inputs for the two motors and connect them both to the one available PWM o/p on the Pi. I haven't actually done this, but I can't see why it shouldn't work. In principle both motors will run at the same speed: in reality they're unlikely to be perfectly matched so there will a slight difference.

Nowadays, if I needed to control two motors from the Pi without additional hardware (apart from the motor driver) I'd try Joan's pigpio. I used it to control multiple servos and it was fine.

I am currently playing with a PI/Arduino combo (using spi, but that could change), which I hope will serve for most of my robotics projects.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi
User avatar
Posts: 2567
Joined: Sat Jan 05, 2013 7:28 pm