0

I am currently using the arduino Uno connected with L298N(green pcb) connected to a dc motor rated 12V. I provided dedicated 12v power supply to the dc motor and the arduino board's power is supplied by usb connected to my pc. I connected Pin 9 of arduino uno to 'ENA' on L298N , Pin 7 & 8 (UNO) to 'INA' & 'INB' of L298N. Connected '-' polarity of Motor power supply to GND of L298N, while also connecting the 'GND' of L298N to the GND of Arduino UNO. Last but not least, connecting the 'OUT B'to the dc motor. This is the picture of how L298N(green) look like https://sea.banggood.com/Green-Board-L298N-DC-Motor-Driver-Board-Stepper-Motor-p-974549.html?cur_warehouse=CN

Can i check why my motor isn't running? Thanks in advance.

char data = 0;
int enB = 9;
int InC = 8;
int InD = 7;


void setup()
{
    Serial.begin(9600);                   
    Serial.println("Motor test !");
  pinMode(enB, OUTPUT);
   pinMode(InC, OUTPUT);
  pinMode(InD, OUTPUT);

}


void loop()
{Serial.println("Entered loop, should start moving now !");
  digitalWrite(InC, HIGH);
  digitalWrite(InD, LOW);
   digitalWrite(enB, HIGH);
   analogWrite(enB, 255);
 }
1
  • the code that is inside the loop() block needs to run only once ... you can move it into the setup() block
    – jsotola
    Aug 21, 2019 at 4:05

1 Answer 1

1

Well, your wiring is OK, but I think the

'digitalWrite(enB, HIGH);

in the loop() code is causing your problem. You don't want to try to use the same pin for both digital and analog functions.

I wired up a Uno here with your setup and a L298N, and got a motor to run fine with the following sketch that doesn't use the loop() at all - it does everything in setup().

char data = 0;
int enB = 9;
int InC = 8;
int InD = 7;


void setup()
{
    Serial.begin(9600);
    Serial.println("Motor test !");
    pinMode(enB, OUTPUT);
    pinMode(InC, OUTPUT);
    pinMode(InD, OUTPUT);

    Serial.println("motor should start moving now !");
    digitalWrite(InC, HIGH);
    digitalWrite(InD, LOW);
    //digitalWrite(enB, HIGH);

    for (size_t i = 0; i < 10; i++)
    {
        analogWrite(enB, 20*i);
        Serial.print("motor speed = "); Serial.println(20 * i);
        delay(2000);
    }

    Serial.println("quitting!");
    while (true)
    {

    }

}


void loop()
{
    //Serial.println("Entered loop, should start moving now !");
    //digitalWrite(InC, HIGH);
    //digitalWrite(InD, LOW);
    //digitalWrite(enB, HIGH);
    //analogWrite(enB, 255);
}

If you can't get your motor working with the above program, then you most likely have a connection or power supply or component problem that you'll need to troubleshoot.

Hope this helps,

Frank

3
  • Hi Frank, thanks for your help, after using the code you have shared, it still doesnt work. I'm suspecting that this green L298N is different from the regular "red" ones, On my L298N board only shows "5v" led lit up, all "A" "B" "C" "D" aren't. Aug 21, 2019 at 3:00
  • Bypass everything and MOMENTARILY connect +12V to the motor Does the motor run? Do you have a multimeter? If so, you can ohm out the motor to see if you maybe have an open winding. If the motor runs when connected directly to 12V, Try a different PWM output pin instead of pin 9. Try connecting the motor to the other L298N output. If you have a DVM, measure the DC voltage at the L298N motor output; it should read almost the full supply voltage when the program runs. If you don't have a DVM, you should consider getting one - they are invaluable tools for troubleshooting ;-)
    – starship15
    Aug 21, 2019 at 11:50
  • @user10748500 can you add another picture (the existing link doesn't work)? I only know the green one, not the red...
    – Sim Son
    Aug 21, 2019 at 14:26

Your Answer

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

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