Shop OBEX P1 Docs P2 Docs Learn Events
How To: Use a L298N Dual H-Bridge with a Microcontroller (QuickStart Board) — Parallax Forums

How To: Use a L298N Dual H-Bridge with a Microcontroller (QuickStart Board)

Duane DegnDuane Degn Posts: 10,588
edited 2015-01-08 11:24 in Robotics
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.

attachment.php?attachmentid=109421&d=1404146906

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.
«13

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-09 17:02
    Program "TwoPwmCounters140709c" uploaded July 9, 2014.
    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.
    CON ' pin assignments
    
      ENABLE_PIN_A = 0             '' user changable
      IN1_PIN = 1                  '' user changable
      IN2_PIN = 2                  '' user changable
      ' Set IN1_PIN to -1 if no direction pin is used (when using a transistor).
      ' Set IN2_PIN to -1 if using a single direction pin.
      
      ENABLE_PIN_B = -1             '' user changable
      IN3_PIN = -1                  '' user changable
      IN4_PIN = -1                  '' user changable
      ' Set ENABLE_PIN_B to -1 if no direction pin is used (when using a transistor).
      ' Set IN3_PIN to -1 if no direction pin is used (when using a transistor).
      ' Set IN4_PIN to -1 if using a single direction pin.
      
    

    Note the enable and direction pins for the second motor are not set. The second motor is disabled with the present settings.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-09 17:02
    Projects using the L298N.

    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
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-09 17:03
    Last one reserved.
  • lardomlardom Posts: 1,659
    edited 2014-07-09 17:41
    Great tutorial. I saw the encoder installed on the inner side of the motor shaft and the 'light' went on. Up until that point I didn't realize the opposite side of the motor shaft had a purpose. "Maybe I could hack the 'break beam' scroll wheel encoders in a mouse."
  • ercoerco Posts: 20,244
    edited 2014-07-09 18:27
    A meaty subject deftly handled by my forum hero, Duane Degn! Wherever do you find the time?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-09 21:32
    erco wrote: »
    Wherever do you find the time?

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-09 21:34
    I attached a PWM program to post #2. It uses the Parallax Serial Terminal as input. Here's a sample screen after motor A has been set to a speed of 2.
    programName = TwoPwmCounters140709cThe method "PwmCounter" as been started in cog # 2
    
    
    The current PWM frequency is 200 Hz.
    The current PWM resolution is 100.
    Target Speeds:
    targetSpeed[0] = 0
    targetSpeed[1] = 0
    Enter one of the following characters:
    a) to change speed of motor A
    b) to change speed of motor B
    f) to change pwm Frequency
    r) to change pwm Resolution
    c) to Clear window
    0 or x) to stop both motors
    
    
      *** Enter Speed ***
    Please input the desired speed of motor A
    Enter a number between -100 and +100.
    The motor speed was successfully changed.
    
    
    The current PWM frequency is 200 Hz.
    The current PWM resolution is 100.
    Target Speeds:
    targetSpeed[0] = 2
    targetSpeed[1] = 0
    Enter one of the following characters:
    a) to change speed of motor A
    b) to change speed of motor B
    f) to change pwm Frequency
    r) to change pwm Resolution
    c) to Clear window
    0 or x) to stop both motors
    

    The program uses counters to generate the PWM signal.
  • Bill HenningBill Henning Posts: 6,445
    edited 2014-07-10 06:41
    He connected 5V supply or 5V output to a Pi GPIO directly???? OUCH!

    I started watching his video to find it, but stopped it... did not want to wait up to 18 minutes.
  • PublisonPublison Posts: 12,366
    edited 2014-07-10 06:43
    Nice video Duane. Looks like a labor of love.

    Jim
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-07-10 07:48
    Well done, Duane!

    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.)
  • Bill HenningBill Henning Posts: 6,445
    edited 2014-07-10 10:14
    Good video Duane!

    Love the meccanum wheeled Rover 5.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-10 10:43
    He connected 5V supply or 5V output to a Pi GPIO directly???? OUCH!

    I started watching his video to find it, but stopped it... did not want to wait up to 18 minutes.

    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:
    If you "understand my concern" why haven't you fixed or removed the video? My concern is people are frying their RPi boards from following the example of your video.

    The longer the video remains in its current state the more Raspberry Pi boards (and possible other microcontrollers) will be destroyed.


    I can't understand your lack of action on this very serious matter.

    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.

    Publison wrote: »
    Nice video Duane. Looks like a labor of love.

    Jim

    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.
    mindrobots wrote: »
    Well done, Duane!

    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!

    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.
    mindrobots wrote: »
    I hope you do a series,

    And I hope you break a leg. :smile:
    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.
    mindrobots wrote: »
    if I ever have a robotics class for 4-H kids (or others), I'll certainly include your videos as mandatory viewing!

    I'm not sure how I feel about being "mandatory viewing". How about "suggested viewing"?

    I personally avoid as many "mandatory" things as possible.
    mindrobots wrote: »
    (Thanks to the uncredited camera person! They did a great job of following you.)

    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).
  • PublisonPublison Posts: 12,366
    edited 2014-07-10 10:51
    Duane Degn wrote: »


    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-10 11:14
    Good video Duane!

    Love the meccanum wheeled Rover 5.

    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.
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-10 19:07
    Hi Duane,
    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.
  • GenetixGenetix Posts: 1,740
    edited 2014-07-10 19:33
    Duane, this would make a great magazine article.

    QuickStart from Radio Shack
    Motor and Controller from Ebay.
    Breadboard???

    Motor control on a budget - Priceless!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-10 19:42
    whiteoxe wrote: »
    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

    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.
    whiteoxe wrote: »
    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 !

    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.
    whiteoxe wrote: »
    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.

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-10 19:48
    Genetix wrote: »
    Duane, this would make a great magazine article.

    QuickStart from Radio Shack
    Motor and Controller from Ebay.
    Breadboard???

    Motor control on a budget - Priceless!

    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. :smile:
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-12 00:35
    I was finally able to convince The Raspberry Pi Guy to make his video private.

    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.
  • Bill HenningBill Henning Posts: 6,445
    edited 2014-07-12 07:52
    Well done!
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-13 18:18
    yep, well done frm me as well. sounds like it was a battle but worth it.
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-13 18:24
    I saw a comment fom u to another, don't go the Arduino C , stay or was it learn the prop as its going to be so much better in the long run. for all kinds of valid reasons. I will try to start using the QuickStart this week, my sensors should be arriving soon. ill try spin an C. Love your work ;)
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-13 18:31
    ive not been sure how to setup/build my simple robot that pic of your beginner robot with the breadboard on it, ive got a few of those that helped me think about how to do it all neater. :)
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-07-13 19:18
    think about how to do it all neater
    Myself I usually worry about this once I have a working product!!
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-13 20:34
    Duane, I did some experimenting with close up video recording, I expected using my Sony A35 Dslr would be best for close up video. So far to my surprise my logitect video webcam does a much better job at video ???? but the sony is great for single photos when set to macro. I could make a video one day using both cams at the same time but then have to spend over an hour video editing. The best free video editor is VSCD I THINK , you can drop pics into it and set how many seconds you want the pic to show. its also good for green screening, if you wanted to make you rover look like it was on the moon ???? but in Linux, open shot is equally good. but there are so many, best to stick with what you know.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-13 21:14
    whiteoxe wrote: »
    The best free video editor is VSCD I THINK , you can drop pics into it and set how many seconds you want the pic to show. its also good for green screening, if you wanted to make you rover look like it was on the moon ???? but in Linux, open shot is equally good. but there are so many, best to stick with what you know.

    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.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-13 21:26
    Well done!
    whiteoxe wrote: »
    yep, well done frm me as well. sounds like it was a battle but worth it.

    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.
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-13 21:41
    Duane Degn wrote: »
    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.

    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 ?
  • whiteoxewhiteoxe Posts: 794
    edited 2014-07-13 21:54
    I was about to do a quick 1 min close up using VSCD (I hope I ot those letters right ! but my windows partition is downloading 133 updates) or id have already done it and posted it.


    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 !
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-13 21:58
    whiteoxe wrote: »
    Do u want to edit videos in widows or Linux ?

    Windows for the time being (7.0).
    whiteoxe wrote: »
    I was about to do a quick 1 min close up using VSCD

    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.
Sign In or Register to comment.