How To: Use a L298N Dual H-Bridge with a Microcontroller (QuickStart Board)
Duane Degn
Posts: 10,588
After seeing TheRaspberryPiGuy's video titled "Raspberry Pi - How to control motors..." and seeing the Pi's I/O pin being connected to a 5V pin, I decided to make my own tutorial video. The TRPG's video has likely contributed to many burned out Raspberry Pi computers. Even 5V microcontrollers would likely be damaged if used the way the video suggests.
First off (as I say in the video) the L298N quad half bridge is a really poor choice as an h-bridge. Several people I consider robot experts (Gordon McComb and Russell Cameron) have mentioned their dislike of the chip on several occasions.
There are lots of better h-bridges than the L298N. Parallax sells the MC33926 dual h-bridge board made by Pololu which would be a much better alternative to the L298N.
The main (only?) advantage the L298N boards have is they are dirt cheap on ebay. You could probably purchase ten or more L298N boards (shipped) for the price of a single MC33926 board. At these prices, it's hard to resist purchasing a few of these inexpensive h-bridges.
The video I made is long. Over 14 minutes (though it's shorter than TheRaspberryPiGuy's how to fry pi video). I show how one can control the h-bridge by setting the enable and direction pins directly without using a microcontroller. I hope by seeing how the pins need to be set, a newcomer to hobby robotics will have a better understanding of what the software needs to do to control the h-bridge.
Setting the control pins high and low is something one could also do with more expensive h-bridges. IMO, it's very helpful to get straight how the pins need to be set before attempting to use a h-bridge with a microcontroller.
Here's the video:
Here's a photo to which I added some diagrams on how the board should be connected to a microcontroller.
The battery high limit of 14V is to keep the 5V regulator from being burned out. The L298N can use voltages as high as 46V but the regulator can't take this high of a voltage. Apparently higher voltages can also cause trouble for the microcontroller attached to the board (as erco found out for himself).
Here's a table of how the pins should be set to achieve forward and reverse motor control.
Forward and reverse are pretty much chosen arbitrarily. If a motor is spin in the opposite direction of the desired direction, there are several ways to reverse the direction of the motor.
By switching with wire is connected to which screw terminal, the motors will be reversed. The same is true for the "IN" pins. These can be either physically swapped, or the I/O pins can be switched in the program. Of course only one method (or an odd number of methods) should be used to reverse the direction of the motor. If one switched both the motor wires and the "IN" wires, you'd be back where you started.
I plan to add some additional material to this thread. I'll reserve a few posts (3 more) to use for software and other information.
Please wait to comment until I've make three additional posts to this thread.
First off (as I say in the video) the L298N quad half bridge is a really poor choice as an h-bridge. Several people I consider robot experts (Gordon McComb and Russell Cameron) have mentioned their dislike of the chip on several occasions.
There are lots of better h-bridges than the L298N. Parallax sells the MC33926 dual h-bridge board made by Pololu which would be a much better alternative to the L298N.
The main (only?) advantage the L298N boards have is they are dirt cheap on ebay. You could probably purchase ten or more L298N boards (shipped) for the price of a single MC33926 board. At these prices, it's hard to resist purchasing a few of these inexpensive h-bridges.
The video I made is long. Over 14 minutes (though it's shorter than TheRaspberryPiGuy's how to fry pi video). I show how one can control the h-bridge by setting the enable and direction pins directly without using a microcontroller. I hope by seeing how the pins need to be set, a newcomer to hobby robotics will have a better understanding of what the software needs to do to control the h-bridge.
Setting the control pins high and low is something one could also do with more expensive h-bridges. IMO, it's very helpful to get straight how the pins need to be set before attempting to use a h-bridge with a microcontroller.
Here's the video:
Here's a photo to which I added some diagrams on how the board should be connected to a microcontroller.
The battery high limit of 14V is to keep the 5V regulator from being burned out. The L298N can use voltages as high as 46V but the regulator can't take this high of a voltage. Apparently higher voltages can also cause trouble for the microcontroller attached to the board (as erco found out for himself).
Here's a table of how the pins should be set to achieve forward and reverse motor control.
MotorA ENA IN1 IN2 Forward High High Low Reverse High Low High Coast Low X X "X" means it can be high or low Brake High Low Low (If both IN1 and IN2 are high, it will also brake)
Forward and reverse are pretty much chosen arbitrarily. If a motor is spin in the opposite direction of the desired direction, there are several ways to reverse the direction of the motor.
By switching with wire is connected to which screw terminal, the motors will be reversed. The same is true for the "IN" pins. These can be either physically swapped, or the I/O pins can be switched in the program. Of course only one method (or an odd number of methods) should be used to reverse the direction of the motor. If one switched both the motor wires and the "IN" wires, you'd be back where you started.
I plan to add some additional material to this thread. I'll reserve a few posts (3 more) to use for software and other information.
Please wait to comment until I've make three additional posts to this thread.
Comments
Program "TwoPwmCounters140709c" deleted September 4, 2014 (20 views).
There was a bug in the program "TwoPwmCounters140709c", which prevented it from working correctly if any of the pins were set to -1.
If any of you are using this previous version, make sure and switch to the latest code.
Program "TwoPwmCounters140904a" uploaded September 4, 2014.
Program "TwoPwmCounters140904a" deleted September 5, 2014 (0 views).
See post #67 for more information.
Program "TwoPwmCounters140905a" uploaded September 5, 2014.
This latest version included hookup diagrams for the ebay L298N PCBs shown in the tutorial video.
The code attached to this post uses the following I/O pin assignments.
Note the enable and direction pins for the second motor are not set. The second motor is disabled with the present settings.
Mecanum Wheeled Robot (Rover 5)
Parallax Project Forum Thread
Let's Make Robots Page
Rover 5 with Treads
Parallax Robot Forum Thread
Let's Make Robots Page
The time was pretty well hidden this time. The video took a surprising about of time to make. I'm not sure how many more I'll make like this one.
Knowing so many people were watching TheRaspberryPiGuy's video was a strong motivation to get this video made. I'm really annoyed at TRPG for not issuing any sort of correction even though he has known about his severe mistake for over four months. It scares me to think about how many RPi boards he has contributed in killing.
BTW, I'm pretty sure TheRaspberryPiGuy is really TheRaspberryPiKid. I think he's about 15 years old.
The program uses counters to generate the PWM signal.
I started watching his video to find it, but stopped it... did not want to wait up to 18 minutes.
Jim
Great for a tutorial on the motor controllers. I like how you have things laid out and labeled. Demonstrating how the controller work without a micro is a super learning tool. Then showing how the micro can do the same things with the pins - super learning tool for beginners!
I hope you do a series, if I ever have a robotics class for 4-H kids (or others), I'll certainly include your videos as mandatory viewing!
(Thanks to the uncredited camera person! They did a great job of following you.)
Love the meccanum wheeled Rover 5.
He starts the connection to which he is calling the "the second ENA pin" at about 10:41. The dirty deed is done at about 11:02.
He replied to my email requesting he fix or remove the video by saying "I understand your concern with my video" and then went on to tell me about a video he is working on. He didn't say anything about what he was going to do to fix the problem. I replied:
It really puzzles me why he would leave his video up. There are reports of RPi "getting really" hot and not surprisingly burnt out on the Raspberry Pi Forums.
Thanks Jim.
I'm not sure if I'd call it a labor of love. More like a "labor of civic duty". After posting my L298N video I saw there are actually lots of other similar videos so I may have been able to find a well done video and just linked to it.
Thanks Rick.
When I was new to microcontrollers a few years back, I would do the "wire only" trick to make sure I understood how the h-bridge worked. IMO, by testing the h-brdige with just wires, one can eliminate many errors which would be hard to identify once the h-bridge is connected to the microcontroller.
I also think it helps to understand why the program is doing what it's doing by having experience controlling the h-bridge directly.
And I hope you break a leg.
I'll probably do a few more but it's surprising what a pain it is to do this sort of thing. I'm pretty sure part of my problem is not having software to handle high res video. I have to use YouTube's online video editing which is a huge pain.
I'm not sure how I feel about being "mandatory viewing". How about "suggested viewing"?
I personally avoid as many "mandatory" things as possible.
Didn't my wife do a great job? I was really pleased with how she used clues from what I was saying and a few gestures to anticipated where the camera should be pointed. I made sure she knew I thought she did a fantastic job (I also just read her your comment over the phone).
Excellent job!
We need prizes for wives, husbands, sons and daughters, (OK twins too), that help in all the videos produced here.
Thanks Bill.
I added some links to my projects which use the L298N to post #3. I'm still really pleased with how well the Mecanum wheeled robot behaves.
Since I disregarded the PiGuy and had both motors running successfully on your posted instructions I took the steps to see for myself what the contraversial ENa ,ENb and the +5 Volt screw terminal were doing while motors were running. Just connected a wire to +5 Screw terminal and another into the ground Screw terminal and it was outputting just shy of 4.5 Volts on the multimeter.
Got the same + 4.5 voltage reading of the back pins of ENa,ENb and nothing off the front that you used to turn High with the Microcontroller for Enabling the motors. The PiGuy said it didn't matter which Enable pin was used but then goes ahead and uses both front and back, guess its lucky for him he included the front pin !
So if I had not taken off the jumpers that would have then left both motors permanantly enabled and saved two lines of code But I know you didn't say remove them for nothing so they are not going back on.
The 4AA battery pack i used was +5.7 volts.
I wasn't being a Thomas , but I did want to see with my own eyes what you had been saying, thanks for the video.
Mike.
QuickStart from Radio Shack
Motor and Controller from Ebay.
Breadboard???
Motor control on a budget - Priceless!
The 5V regulator needs about 7V to output 5V. You're seeing only 4.5V because you're using a battery pack with too low of voltage to produce 5V from the regulator.
4.5V is still high enough to get the L298N chip to work.
I have a hard time figuring out what the heck TRPG is doing. Why he would think he needed two enable pins for one motor is a mystery. How his Raspberry Pi survived the abuse he put it through is another mystery. Yet another mystery is why he leaves his video up when there have been repeated reports of the people burning out the RPi boards from following the instructions in the video.
More important than saving two lines of code would be saving two I/O pins. It's possible (though not recommended) to pulse the direction pins ("IN" pins) instead of the enable pins. If the direction pins are pulsed then the enable pins could be left pulled high with the jumpers.
If you leave the enable pin connected to the 5V pin, then every time you pulse a "IN" pin, the motor will brake. Erco made a little video showing the difference between pulse/coast and pulse/brake in the cheap L298N thread.
There's a better way of driving the motor with just two pins than pulsing a direction pin. This better way is to use an inverter on one of the direction pins so when one pin is set high, the other one is automatically set low and vice versa.
An inverter can be made using a transistor. I've done this for a pair of my L298N boards. I plan to add details of how to use control the motors with just two I/O pins instead of three I/O pins soon.
I'm not seeing it. This is a step up from lighting a LED with a microcontroller but not really a big one.
My Mecanum wheeled Rover 5, now that's a possible story.
I had made several comments on the video page pointing out the error, I sent him several emails on the subject (getting progressively harsh). But it wasn't until I started listing the Raspberry Pi boards which had been burnt out as a result on his video on the Raspberry Pi forum that he finally took action.
I searched Google for the video's URL and I saw there were a bunch of links to the video in the Raspberry Pi forum. Wouldn't you know it, most of those links turned out to be questions concerning burnt out Pi boards from following the directions on the video. It was really depressing reading the various threads. I'm having a hard time not feeling very angry when I think of all the money and time wasted buy this kid's refusal to fix his video. This issue had been raised many times previously. I still don't understand why he wouldn't take action earlier.
So VSCD is Windows? Can you edit 1080p with it?
All I know is an old version of Adobe Premiere but it won't edit high res stuff. I don't think it would even open the file produced by my Sony NEX-3N if it's done in a lower resolution. As I mentioned earlier in the thread (IIRC) I ended up using YouTube's online editor with is extremely slow.
I think my L298N video could be greatly improved with a few macro shots of the connections.
Thanks guys.
Edit: Rant about a kid deleted. I'll keep my rantings on the now pulled L298N video limited (mostly) to the R Pi forum.
The Best was for vwindows the 2.6 vista version. So easy tom add audio and effects and pics, but that is a lot of trouble, si if im in widows, im starting to take a hard look at the free VSCD but if you want to try Openshot in Liux I think its just as good. Let me play around with the two for a bit and I will give you my best diagnosis PS. Do u want to edit videos in widows or Linux ?
ypoll have to click on the video link as im not sure or haven't looked how to embed a youtube vid into a post yet !
Windows for the time being (7.0).
Thanks for the offer but I know uploading a video is a big pain. Now that I know what to look for (VSCD right?), I should be able to find some info on the web.