-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);
}
  • Do you know that analogWrite(enA, 0); stops it? Or what exactly is the problem? – gre_gor Dec 16 '17 at 17:58
  • With using analogWrite Do make sure you use the pwn pins of arduino to control the motors. – Mukhar Jain Dec 16 '17 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 – Mustafa Mohamed Dec 17 '17 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. – jose can u c Dec 18 '17 at 15:38
  • @josecanuc i did my wire base on video on youtube with my same electronics and it runs perfect with his device – Mustafa Mohamed Dec 19 '17 at 0:28
0

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);
  • 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 – Mustafa Mohamed Dec 17 '17 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". – LegitimateWorkUser Dec 18 '17 at 23:02
  • @LegitimateWorkUser these are the speed control pins(voltage) if you take them off the motor will not run at all – Mustafa Mohamed Dec 19 '17 at 0:25
0

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

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.