29 May 2009
L298 Hbridge meets Arduino mega
Filed under Sketch
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.
Since code and pictures speak louder than words allow me to show just what I did:
void setup() {
Serial.begin(9600); // go go serial powerpinMode(6, INPUT); // this simulates the two power pins on
pinMode(7, INPUT); // the electric motor, like a virtual motorpinMode(14, OUTPUT); //logic signal 1
pinMode(15, OUTPUT); //logic signal 2
pinMode(18, OUTPUT); // enable hbridgedigitalWrite(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
}

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
2009-05-29 :: Andrew


26 September 2009 @ 1:47 pm
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
[Reply]
Andrew Reply:
September 26th, 2009 at 2:40 pm
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.
[Reply]
26 September 2009 @ 3:01 pm
ah, thanks!
[Reply]
Andrew Reply:
September 26th, 2009 at 4:41 pm
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.
[Reply]
26 September 2009 @ 6:15 pm
WOW, great!
thanks alot! the two white blocks in your photo, what are they for?
[Reply]
26 September 2009 @ 7:40 pm
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.
[Reply]
Andrew Reply:
September 26th, 2009 at 8:04 pm
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.
[Reply]