L298 Hbridge meets Arduino mega

I’ve recently purchased a L298 Hbridge to help me help arduino help a remote controlled car think by itself and move.

Does it work? Why not test it I said to myself. Play around with low voltages, see if I can get it to switch polarities.

After some trying and testing, or just trial and error, as the datasheet didn’t help much or I just missed where it mentioned, I’ve figured out I need to disable the bridge every time I switch polarities.

[[Some more up to date code]]

Since code and pictures speak louder than words allow me to show just what I did:

void setup() {
Serial.begin(9600); // go go serial power

pinMode(6, INPUT); // this simulates the two power pins on
pinMode(7, INPUT); // the electric motor, like a virtual motor

pinMode(14, OUTPUT); //logic signal 1
pinMode(15, OUTPUT); //logic signal 2
pinMode(18, OUTPUT); // enable hbridge

digitalWrite(18,LOW); //turn off
digitalWrite(14,HIGH); //send signal1
digitalWrite(15,HIGH); //send signal2
digitalWrite(18,HIGH); //turn on
Serial.println(digitalRead(6)); //get output
Serial.println(digitalRead(7));
Serial.println();

digitalWrite(18,LOW); //turn off
digitalWrite(14,HIGH); //send signal1
digitalWrite(15,LOW); //send signal2
digitalWrite(18,HIGH); //turn on
Serial.println(digitalRead(6)); //get output
Serial.println(digitalRead(7));
Serial.println();

digitalWrite(18,LOW); //turn off
digitalWrite(14,LOW); //send signal1
digitalWrite(15,HIGH); //send signal2
digitalWrite(18,HIGH); //turn on
Serial.println(digitalRead(6)); //get output
Serial.println(digitalRead(7));
Serial.println();

digitalWrite(18,LOW); //turn off
digitalWrite(14,LOW); //send signal1
digitalWrite(15,LOW); //send signal2
digitalWrite(18,HIGH); //turn on
Serial.println(digitalRead(6)); //get output
Serial.println(digitalRead(7));
Serial.println();
}

void loop() {
// Please use common sense when trying ‘internet code’.
// I refuse to be held accountable for any
// damages this code may or may not cause.
// Shameless and removable author webpage inclusion:
// http://software.o-o.ro
}

A better than nothing picture of testing the L298 on an arduino mega

L298 and arduino mega

And the lovely output:

1
1

0
1

1
0

0
0

The wiring in this example is rather poorly made, I can think of better ways to do it now, but it works. Here’s a graphical representation of the connections, done with the aid of fritzing.

L298 HBridge and arduino wiring

L298 HBridge and arduino wiring

Was this useful at all?

VN:F [1.9.22_1171]
Rating: 5.3/10 (6 votes cast)
L298 Hbridge meets Arduino mega, 5.3 out of 10 based on 6 ratings
  • http://www.jdid.se JDesign

    Hi!
    it seems that you got it working ? Im having a hard time to connect all the wires, do you got a wiring diagram?

    best regards

    • http://software.o-o.ro Andrew

      Yes I got it working, took me a while to get it going too. Unfortunately I didn’t think to make a wiring diagram at the time, and now the project is disassembled waiting to be made into a new project. I’ll try to piece together my memories with the bits of code and pictures to make up a diagram for you. Should get it done today or tomorrow.

  • http://jdid.se/ JDesign

    ah, thanks!

    • http://software.o-o.ro Andrew

      I’ve uploaded a wiring diagram, looking back on it there’s room to improve, but it does work. If you have any more questions feel free to ask.

  • http://jdid.se/ JDesign

    WOW, great!
    thanks alot! the two white blocks in your photo, what are they for?

  • http://www.jdid.se JDesign

    hm, I think I need some help, agian :) ( noob :) )
    I’m using a noname (ebay) bipolar stepper motor with four wires. iv’e checked all the wires with a multimeter, and found two pair that belongs together. where should they go? my first guess was, the first coil, wire1 to out 1, and wire2 to out2 and the second coil ,to out3 and 4. but this was obviously wrong.

    • http://software.o-o.ro Andrew

      The small block is the Hbridge and the larger one is the arduino board, as seen in the picture.

      I’ve never worked with stepper motors before, so there’s not much advice I can give. From what I can gather, you should determine if it is unipolar or bipolar then check the arduino examples for help. The examples seem to have a different model bridge, but you should be able to identify what goes where by looking at the datasheet of the one you have.

      Arduino uniploar example
      Arduino bipolar example

      I’m not sure how much help this is, but I hope it’s at least a little bit useful.

  • Pingback: FOB Controlled H-Bridge Motor with Arduino | The Inner Qube

  • Bak_maarten

    Be sure to connect the sense pins to the ground! If you don’t the motor may not turn

  • Guest

    Hmm, I’ve done it twice now and my output reads:

    1
    1

    1
    1

    1
    0

    1
    1

    Is it obvious what could be going wrong?

    • Guest

      I found it, this is the result if the sense pin (1) isn’t grounded. That edge leg doesn’t like to cooperate with the breadboard : )

  • Jacobthomas007

    hey does the ic get heated up

  • Pingback: Controlling your trains with an Arduino « modelrail.otenko

  • Pingback: Ardunio L298 motor control | Software