L298N Not Changing Speed

Hi all,

I am using a L298N with an Arduino to power a DC motor used in a baby’s mobile.
We want to control this motor to move forward, backward, and at different speeds.

As seen in the wiring diagram, I have a bench power supply connected to the L298N and commonly grounded with the Arduino.
I cannot see the rating for the DC motor, but it was originally supplied 3 AA batteries. When hooked up to the bench power supply with 4.5V, it drew 0.08A.

Below is the code I am trying to run the motor with:

int dir1PinA = 5; // direction
int dir2PinA = 6; // direction
int speedPinA = 4;  // 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, 150); // 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
}

The motor runs just fine when supplied this code, but when I change the speed from 150 to 255 or some other number, the speed of the motor is exactly the same. What’s more, when I enter a number below 130, the motor does not run.

Lastly, when I connect the motor directly to the power supply and modulate the voltage, the motor speed changes.

Any ideas would be much appreciated!

Have you checked whether speedPinA is a PWM pin?

jremington:
Have you checked whether speedPinA is a PWM pin?

This was it! Sorry I thought all pins on the Digital line were PWM. Switched it to pin 6 (PWN pin) and it works as intended.