Pages: [1]   Go Down
Author Topic: Motors to H-Bridge Problem  (Read 858 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Chin Chillin'!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright, here is a simple program that isn't working as it should and I have no idea why.

I have hooked a motor to an H-Bridge and placed pins 5 (PD3) and 6 (PD4) to my ATmega328 (from the UNO) to the logic of the H-Bridge and placed pin 3 (PD1) to the enable of the H-Bridge. Now, the idea is to PWM the enable of the H-Bridge while the Arduino pins 5 & 6 control the direction the motor will spin.

The PWM works fine (the motor spins faster and slower over time), but it spins only in one direction and I have no idea why. Here is a simple program that I made for testing:

Code:
//Motor Through H-Bridge Test
void driveMotor(int);

void setup() {
  Serial.begin(9600);
  delay(100);

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}//end setup()

void loop() {
  for (int i = -250; i <= +250; i += 25 ) {
    driveMotor(i);
    delay(100);
  }//end for
}//end loop()

void driveMotor(int torque)  {
  //pins 5 and 6 go to logic of H-Bridge.
  if (torque >= 0)  {// drive motors forward
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
   
  } else if (torque < 0) { // drive motors backward
    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
   
    torque = abs(torque); //turn value pos.
  }//end if
   
  analogWrite(3,torque); //PWM to enable of H-Bridge
}//end driveMotor()

This is part of a balancing robot that I'm working on, so... help?

Thanks.
Brian
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it a bought H Bridge motor driver, or one you've made yourself?
What sort of motors are you using? Voltage, current rating etc?
Also, might help if you post a schematic of your circuitry.

I'm still new to all this myself to be honest, but providing the above information will make it easier for folk to help...

One thing I would say, have you tested your setup with a simpler program e.g. one that just brings the logic levels high and low for a period of time? Then you would know if the problem is in the program or in the wiring...
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9627
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hqave you tried operating the h-bridge without the arduino to see if it works?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Chin Chillin'!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know the problem isn't my H-Bridge, because when I physically put the bridge logic to HIGH and LOW, then the motors spin in a certain direction and when I reverse to LOW and HIGH the motors spin in the opposite direction (as it's suppose to). The problem is in the ATmega328 isn't sending the signal as I would expect and want to know if it's a coding error.

Thanks, though. I shall attempt to throw a schematic up there, too.

Oh, the H-Bridge is purchased and is the L298.
« Last Edit: March 15, 2011, 08:35:13 pm by BrianBot » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Chin Chillin'!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As requested, here is the schematic:
« Last Edit: March 15, 2011, 08:50:21 pm by BrianBot » Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5581
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm - the only thing that I see odd with your code is the "+250" in the comparison of the for loop; normally you would just use "250":

Code:
for (int i = -250; i <= 250; i += 25 ) {

...I am not sure if that is necessarily causing the issue. What happens if you change the code to:

Code:
for (int i = -250; i <= 0; i += 25 ) {

...also:

Code:
for (int i = 0; i <= 250; i += 25 ) {

I guess I am wondering which side of the zero point the issue is happening on.

I had something like this happen to me once on a 2n3055 TO-3 transistor h-bridge I had made, but that was due to a faulty alligator clip (needed clips to connect the collectors together); this isn't the issue in your case, though - obviously...

smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to check your port to pin assignments. In Arduino land, PD1 is digital pin 1 and PD4 is digital pin 4 (rather than the physical package pins 5 and 6 as used in your sketch).

Here's the full list for AtMega 168/328:

http://www.arduino.cc/en/Hacking/PinMapping168
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Chin Chillin'!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to check your port to pin assignments. In Arduino land, PD1 is digital pin 1 and PD4 is digital pin 4 (rather than the physical package pins 5 and 6 as used in your sketch).

Here's the full list for AtMega 168/328:

http://www.arduino.cc/en/Hacking/PinMapping168

Yes, you were very much correct; the problem was my pins. apparently when declaring:
     pinMode(5, OUTPUT);
the 5 refers to PD5 and not pin 5.

I'm shocked I made it this far without knowing this. Perhaps I did know it in the past and am just too stupid to remember. Either way, thank you very much for the help.
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5581
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to check your port to pin assignments. In Arduino land, PD1 is digital pin 1 and PD4 is digital pin 4 (rather than the physical package pins 5 and 6 as used in your sketch).

Here's the full list for AtMega 168/328:

http://www.arduino.cc/en/Hacking/PinMapping168

Yes, you were very much correct; the problem was my pins. apparently when declaring:
     pinMode(5, OUTPUT);
the 5 refers to PD5 and not pin 5.

I'm shocked I made it this far without knowing this. Perhaps I did know it in the past and am just too stupid to remember. Either way, thank you very much for the help.

Glad you sorted it out, but I wasn't aware that the labels on the diagram had anything to do with the code; from your description, you seemed fairly confident it was the code, not the hardware, so I made the (wrong) assumption that your code was configure properly for the hardware, and the hardware was hooked up properly. Hmm - next time you post something like this, you should post your -entire- schematic (all parts and connections) included, along with your complete code (which you did); it would save all of us time.

smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Pages: [1]   Go Up
Jump to: