0

I am using arduino uno to control dc motor speed with L298N. But the problem is that the motor is only going at max speed. PWM pin doesn't work for some reason. I heard many people had some problem but haven't found the solution yet.

code:-

#include <L298N.h>

#define ENA 9
#define IN1 8
#define IN2 7

L298N motor(ENA, IN1, IN2);

void setup() {
// Setup pin mode

//used for display information
  Serial.begin(9600);

}

void loop() {
//Drive Motor Forward
  motor.setSpeed(10);
  motor.forward();
  delay(500);
  motor.setSpeed(100);
  motor.forward();
  delay(500);
  motor.setSpeed(255);
  motor.forward();
  delay(500);

}

I only see the motor move at full speed every time. I connected 5V of Arduino to +12V pin of motor driver and Arduino is powered with my laptop USB.

I am using 4.5V toy dc motor for test - DC motor

What could be wrong here?

  • The l298n output to the motor has a large voltage drop, and because of that it will not work with 5v. The 5v pin of the arduino can not supply enough current for a motor. What motor is it? Can you give a link to the motor? – Jot Apr 9 at 9:30
  • added dc motor link, I am using those toy dc motor for the test. It doesn't need much voltage so I though it will be fine. – suu Apr 9 at 9:39
  • That is a small motor. With a mosfet driver (no voltage drop) it might work with the 5v pin. – Jot Apr 9 at 10:37
  • this motor is just for test. Later I want to control 12V high torque motor so for that, I am using this module. – suu Apr 9 at 10:49
  • Using 5V pin (!) of an USB-powered Arduino (!!) as a motor power supply (!!!) can easily kill your Arduino. – AnT May 10 at 1:06
1

Try to use this code, without the library, and be shure to connect every pin to the correct arduino output! If everything is ok it should work! Check a tutorial here

int IN3 = 5;    
int IN4 = 4;     
int ENB = 3;    
void setup()
{
 pinMode (ENB, OUTPUT); 
 pinMode (IN3, OUTPUT);
 pinMode (IN4, OUTPUT);
}
void loop()
{

  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);   
  analogWrite(ENB,55);
  delay(2000);
  analogWrite(ENB,105);
  delay(2000);
  analogWrite(ENB,255);
  delay(2000);
  analogWrite(ENB,0);
  delay(5000);
}
  • I did try that code also but didn't work. I think there is problem in the enable pin of driver itself. – suu Apr 11 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.