1

I have Arduino Uno and I attached it to 2 DC motors with L298N bridge. I want to control the speed of the motors so they don't move at maximum speed. I use analogWrite() but it does nothing,and the motors move at maximum speed

Here is my code:

//motor A
int enA = 9;
int in1 = 8;
int in2 = 7;

//motor B
int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {
  Serial.begin(9600);
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {

  analogWrite(enA, 0);

  analogWrite(enB, 0);

  digitalWrite (in1, HIGH);
  digitalWrite (in3, HIGH);
  digitalWrite (in2, LOW);
  digitalWrite (in4, LOW);

  delay(50);
}
7
  • Do you know that analogWrite(enA, 0); stops it? Or what exactly is the problem?
    – gre_gor
    Dec 16, 2017 at 17:58
  • With using analogWrite Do make sure you use the pwn pins of arduino to control the motors. Dec 16, 2017 at 18:03
  • yes i know that analogWrite(enA,0) stops the motor and that is what i want but it dosen't work and the motors are alwayes at maximum speed Dec 17, 2017 at 18:16
  • 2
    Please add a schematic of photo of your wiring setup. As you have described it thus far, your code should halt the motor, so perhaps the wiring is not correct. Dec 18, 2017 at 15:38
  • @josecanuc i did my wire base on video on youtube with my same electronics and it runs perfect with his device Dec 19, 2017 at 0:28

2 Answers 2

2

I did find solutions for the problem. It turned out that the bridge does not work as it should be and the pins of en(A,B) is replaced with the in pins ..now I can control the speed but can't reverse the direction of the motors

1
  • This was really frustrating to figure out, so thanks for your answer here! I had a similar bad module, and looks like just connecting my pwm pin to IN1 was all that was needed for speed control in a single direction. I just disconnected the enable jumper/pin and in2 pin, and measured a good pwm output. Seems like the enable pin does nothing on my module!
    – ehsanul
    Dec 20, 2020 at 18:01
1

analogWrite() can take values between 0 and 255 as its second parameter. Where 255 will give you the maximum motor speed and 0 will give you the lowest (motor will not run).

For example, below code will give you almost the half of max speed.

analogWrite(enA, 128);  
analogWrite(enB, 128);
3
  • i know what it does but it does not work the way it should and the motors are allwayes at maximum speed even if i put it to (0) and i am sure of the pins Dec 17, 2017 at 18:18
  • I'm looking at one of the L298N bridges on Amazon. What are those two jumpers on pins ENA and ENB? Try taking those off, because I wonder if they're pegging you at full "on". Dec 18, 2017 at 23:02
  • @LegitimateWorkUser these are the speed control pins(voltage) if you take them off the motor will not run at all Dec 19, 2017 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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