(Sorted) L298N Speed Control Issue

I have a L298N Dual H Bridge DC Stepper Motor Board and I've successfully connected a Meccano 6V reversable motor to it. Using the code below, the motor spins one way and then the other (so far, so good):

void setup() 
{  
  pinMode(13, OUTPUT);    // Set Pin 13 as output, this is the direction control
  delay(3000);            // Wait 3 seconds before we start
} 

void loop() 
{ 
  digitalWrite(13, HIGH); // Set direction as forward
  analogWrite(9, 255);    // Set the speed as full
  delay(1000);            // Wait a second

  analogWrite(9, 0);      // Set the speed to stop
  delay(1000);            // Wait a second

  digitalWrite(13, LOW ); // Set direction as reverse
  analogWrite(9, 127);    // Set the speed as half speed
  delay(1000);            // Wait a second

  analogWrite(9, 0);      // Set the speed to stop
  delay(1000);            // Wait a second
}

When I alter the speed to say 70 (from 255) in the first part of the void loop (),

  digitalWrite(13, HIGH); // Set direction as forward
  analogWrite(9, 70);    // Set the speed as full
  delay(1000);            // Wait a second

the motor still runs at a very high speed. I need the motor to run at low speeds in either direction.

Any help would be appreciated greatly.

try smaller numbers.

analogWrite(9, 10); analogWrite(9, 5); analogWrite(9, 1); analogWrite(9, 0); Do they slow it down, or make it stop?

Thanks for getting back to me.

When I change the analogWrite(); to lower numbers, there is no noticeable difference in the speed of the motor. Only when I change the delay(1000); to a lower figure do I notice a slower rotation. My understanding is that the motor should spin at the pre-determined speed, irrespective of the delay; the delay being there to determine the length of time the motor turns at the analogWrite(); speed.

so, analogWrite(9, 0); did not stop the motor? Sounds like then you can only control the direction. You can't turn it off?

What pins on the L298N do you connect arduinos pin 9 ? I would look at it again, as I sometimes get my wires crossed. LOL

Infact if you can easily show the schematic would be good.

If I run the following code, the motor runs at what I think is near full speed in one direction and then slower (half speed?) in the other direction (very strange as the first part should stop the motor)

void setup() 
{  
  pinMode(13, OUTPUT);    // Set Pin 13 as output, this is the direction control
  delay(3000);            // Wait 3 seconds before we start
} 

void loop() 
{ 
  digitalWrite(13, HIGH); // Set direction as forward
  analogWrite(9, 0);    // Set the speed as full
  delay(1000);            // Wait a second

  analogWrite(9, 0);      // Set the speed to stop
  delay(1000);            // Wait a second

  digitalWrite(13, LOW ); // Set direction as reverse
  analogWrite(9, 127);    // Set the speed as half speed
  delay(1000);            // Wait a second

  analogWrite(9, 0);      // Set the speed to stop
  delay(1000);            // Wait a second
}

So sounds like you do have speed control. Maybe the speed is inverted from what we think. Your last code did not send a high number, just a low number, so try sending a high number at one point.

No schematic easily available?

Hi

I have tried low and high numbers in the first part of the code (forward) with no discernable difference in the speed of the motor.

When I put in smaller numbers for the second part (reverse) this does reduce the speed.

Sorry, I don't have a means of posting a diagram, though the board that I have is the same as this one:

http://www.ebay.co.uk/itm/Dual-H-Bridge-DC-Stepper-Motor-Drive-Controller-Board-Module-Arduino-L298N-236-/200902260723?pt=UK_BOI_Industrial_Automation_Control_ET&hash=item2ec6b537f3#ht_6339wt_958

What is pin 9 connected to?

Pin 9 to IN 2 Pin 13 to IN 1

Pin 9 should go to Enable (speed can be changed by PWM the enable) The L298N looks for two signals to determine direction (status)

As follows

EnA IN 1 IN 2 Status 1 1 0 Forward 1 0 1 Reverse 1 0 0 Brake 0 X X Stop

The ENA 1 (A Enable) enables the IN 1 and IN 2 to be enabled i.e Motor 1. My motor only has 2 wires and they are attached to OUT 1 and OUT 2 (output A) so that the current can run one way or the other (H Bridge board does this).

Just talking about side A

IN1 and IN2 combined will control the direction. Enable, can be pulsed (PWM) to control the speed.

Does your board have two enables, one for side A, and one for side B ? Does it have IN1, IN2 for side A? Does it have IN3, IN4 for side B?

You are just using side A now, right?

Yes, just talking about side A

The board has two enables, one for side A, and one for side B It has IN1, IN2 for side A and it has IN3, IN4 for side B

Yes, I am just using side A now

Great, we are getting close now.

You need to use 3 outputs from the arduino. How about: D9 to enable A (use PWM to adjust speed here) D12 to IN 1 ( look at the chart below to set D12 (IN 1), and D13( IN 2) to get the Status you want ) D13 to IN 2 "

EnA IN 1 IN 2 Status 1 1 0 Forward 1 0 1 Reverse 1 0 0 Brake 0 X X Stop

On the board. the Enable has (for the want of a better phrase) a plastic jumper. I thought this has to be in place to enable the outputs to the motor.

There are 2 of these jumpers, one for IN2, IN2 (for motor A) and one for IN2, IN4 (for motor B). Can you see these on the images on the link that I posted?

Yes, I can see those jumpers. That is a product of the board, not the L298N IC. I suspect they wanted to allow full enable onboard. You want to do speed control, so, If I was to guess about that board, I would think you should disconnect the jumper, and apply your PWM to one of those pins. (Which one?). Look closely at the board. Can you tell which one goes to the IC, and which one goes to Vcc (or ground). Hook to the one that goes to the IC. Disclaimer: If it blows up, don’t hold me responsible!

Hi Jack

I don't have access to the board (or anything else) until tomorrow. I will try and dig out a link from where I based the wiring that I used. Thanks for your help and advice. I'll post the link and anything else that I can find tomorrow.

Thanks again

Rob

Great Rob, I will be anxious to see how this turns out.

Jack

I used the same board as yours to control a brushed motor (rc toy). You need 3 wires: - Remove the jumper at ENA (there are 2 pins) then connect a wire at ENA (next to IN1) to pin 9 (arduino) : this pin controls the speed of the motor. - Connect IN1 to pin 10 (arduino) : this pin controls the direction - Connect IN2 to pin 11 (arduino) : this pin controls the direction

Here is my code:

int dir1PinA = 10; // direction int dir2PinA = 11; // direction int speedPinA = 9; // pin ENA on the motor shield

unsigned long time; int speed; int dir;

void setup () { pinMode (dir1PinA, OUTPUT); pinMode (dir2PinA, OUTPUT); pinMode (speedPinA, OUTPUT); // SPEED A }

void loop () { analogWrite (speedPinA, 100); // change the number from 1-255 to get the speed you want digitalWrite (dir1PinA, LOW); // pin 10 Low, pin 11 High digitalWrite (dir2PinA, HIGH); // change between LOW and HIGH delay(3000); // stop for 3 seconds so you can see which direction when motor starts again. }

Hope this help.

Hi Jack and muangau. Thanks for the suggestions. I'm a bit reluctant to remove the jumper in case I blow something. I am getting the motor to go both ways with the present configuration; it's the speed that is an issue. I got the wiring/code from the internet at Babelduck Cybernetics but the link no longer works. Here it is: http://www.babelduck.com/l298n-dual-dc-motor-driver-control-module/

I also found more info on the board here:

http://www.oddwires.com/l298n-dual-h-bridge-dc-stepper-motor-controller-module-for-arduino/

muangau, can you assure me that your wiring won't blow my board or MC?