Pololu Dc Motor, L298 driver change sense of rotation

Hi there,
I'm trying to control a solar panel using 2 Pololu DC Motors via a L298 motor driver.
Well, this morning I made some small tests related to the LDRs(photoresistors) serial readings and Pololu DC Motor driving and I figured out something...well, the Pololu DC Motor can't change the sense of rotation on the fly?
Well, I have my motors configured like this:
Each motor has 2 pins, each one responsible for speed and direction...I mean if I want to pwm the first motor in a clockwise direction I will have to send something like this: analogWrite(MotorPin1, 180);
If I want to pwm the same first motor in anti-clockwise direction I will have to send something like this: analogWrite(MotorPin2, 180)...this will work but if I'm doing this:
void loop()
{
analogWrite(MotorPin1, 180);
delay(1000);
analogWrite(MotorPin2, 180);

}

it won't do what's supposed to do...rotating for 1 second in a direction and then suddenly switch to the other direction...and do this over and over....it's my code or this kind of motor is not capable to change the direction of rotation on the fly? Thanks in advance :slight_smile:

Then you have made a mistake, the L298 can be driven however you like.

Often people PWM the enable pin for the motor and change its input pins for
direction. The two motors are entirely separate - there are two complete
H-bridges on the same chip, but they only share power, nothing else.

Well, It was the final delay I was missing before closing the void loop() function, like this. Still it doesn't work as I want it :slight_smile:
Take this code:
As you can see, I tried to drive only 1 motor as I will need it for a solar tracker.

int MOTOR1_PIN1 = 6;
int MOTOR1_PIN2 = 9;

void setup() {
pinMode(MOTOR1_PIN1, OUTPUT);
pinMode(MOTOR1_PIN2, OUTPUT);
Serial.begin(9600);
}

void loop() {
analogWrite(6,200);
analogWrite(9,0);
delay(2000);
analogWrite(6,0);
analogWrite(9,-200);
delay(2000);
}
Well, let's take it wisely.My motor needs voltages between 3-6V. I powered it from a battery of 9V(8,93...just measured it).
I have a question. 255 PWM is equal to these 8,93V right? I will post a photo with the wiring.

Well, it does rotate my motor in one sense for 2 seconds then on the fly change the sense but the speed of rotation is different. First it rotates it with 200 PWM which means 7,00V, properly big speed but then when it switches the direction of rotation it doesn't keep the same speed...the speed is very small...why is this happening?
This is how I wired everything :slight_smile:
Please tell me why the speed is not the same? I will need to drive motors to control a solar tracker so I will need to be able to apply any voltage positive or negative...so I thought 40 PWM would be the same speed as -40 PWM except the sense of rotation...am I wrong? :slight_smile: Thanks again :slight_smile:

It seems if I want to obtain any type of rotation I should not use negative values because it seems 200PWM is equal to -40 PWM as speed.
But, if I apply this code

analogWrite(6,60);
analogWrite(9,0);
delay(2000);
analogWrite(6,0);
analogWrite(9,60);
delay(2000);
it does what it's supposed to do but no negative values accepted :frowning:

Does it matter how I connect the orange and brown wires? I reconnected them so I can get a clockwise rotation by default, on this:
analogWrite(6,255);
analogWrite(9,0);

And now the driver no longer works when applying negative voltages but works well on applying diferent positive voltages ...I remember, I have 2 pins for each motor :slight_smile:

LAST EDIT:
The only way I can use positive and negative values is using if conditions and I don't know why!
This way works like charm!!

int MOTOR1_PIN1 = 6;
int MOTOR1_PIN2 = 9;

void setup() {
pinMode(MOTOR1_PIN1, OUTPUT);
pinMode(MOTOR1_PIN2, OUTPUT);

Serial.begin(9600);
}

void loop() {

go(-180);

}

void go(int speedLeft) {
if (speedLeft > 0) {
analogWrite(MOTOR1_PIN1, speedLeft);
analogWrite(MOTOR1_PIN2, 0);
}
else
if(speedLeft<0)
{
analogWrite(MOTOR1_PIN1, 0);
analogWrite(MOTOR1_PIN2, -speedLeft);

}

}
So I will just modify the parameter of the go function in order to get the speed and direction I need...but still don't know why it doesn't work this way:

analogWrite(MOTOR1_PIN1, 0);
analogWrite(MOTOR1_PIN1, -180);

It seems it works only when it's triggered by if condition...whyyy??

You don't seem to be using the motor driver correctly.

There are three inputs to each motor channel of the L298 motor driver, IN1, IN2 and EnableA for one motor and IN3, IN4 and EnableB for the other.

Together the three pins control direction, speed and braking action for one motor (see the L298 data sheet for details).

For one motor, two of the three inputs should be digital, controlled by digitalWrite() and for speed control, only one input (or Enable) should be used to control motor speed, with analogWrite(). See this example: Tutorial – L298N Dual Motor Controller Modules and Arduino | tronixstuff.com

It makes no sense to use negative values in analogWrite(). See analogWrite() - Arduino Reference

My motor driver is this :L298N and as you could have seen in the attached photo, it has 3 groups of 2 pins: 2 pins are Vin and GND and the other 2 pairs are for the 2 wires that go out of each motor...I think you didn't understand how my driver looks like...and this code that uses negative values is taken from here...

http://www.robofun.ro/shield-motoare-l298-v2

the whole code was intended for driving 2 Pololu DC motors in order to move some kind of robot...and this is the whole code:

int MOTOR2_PIN1 = 3;
int MOTOR2_PIN2 = 5;
int MOTOR1_PIN1 = 6;
int MOTOR1_PIN2 = 9;

void setup() {
pinMode(MOTOR1_PIN1, OUTPUT);
pinMode(MOTOR1_PIN2, OUTPUT);
pinMode(MOTOR2_PIN1, OUTPUT);
pinMode(MOTOR2_PIN2, OUTPUT);
Serial.begin(9600);
}

void loop() {
go(255,-255);
delay(1000);
go(-255,-255);
delay(1000);
go(-255,255);
delay(1000);
go(255,255);
delay(1000);
}

void go(int speedLeft, int speedRight) {
if (speedLeft > 0) {
analogWrite(MOTOR1_PIN1, speedLeft);
analogWrite(MOTOR1_PIN2, 0);
}
else {
analogWrite(MOTOR1_PIN1, 0);
analogWrite(MOTOR1_PIN2, -speedLeft);
}

if (speedRight > 0) {
analogWrite(MOTOR2_PIN1, speedRight);
analogWrite(MOTOR2_PIN2, 0);
}else {
analogWrite(MOTOR2_PIN1, 0);
analogWrite(MOTOR2_PIN2, -speedRight);
}
}

As you can see it works only with analog values...nothing that has to do with digitalWrite, nothing that has to do with 3 pins...before giving an answer, you'll better read it carrefully, otherwise you're misleading me...thanks anyway :slight_smile:

otherwise you're misleading me

You are doing quite well in misleading yourself. Don't bother to look at any other examples, as they will probably confuse you further!