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.
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.
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.
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).
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!
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.
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.
}
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/