I need help with L289N motor driver

Hye my senior and fellow friends around the world
I need some help with my L289N motor driver


As u can see in this photo i already put
ENA to 10
IN1 to 8
IN2 to 7

However my motor only move in one direction

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 7;
int in2 = 8;

void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);

}
void demoOne()
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200);

// now change motor directions
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

delay(1000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

}
void demoTwo()
{
// this function will run the motors across the range of possible speeds
// note that maximum speed is determined by the motor itself and the operating voltage
// the PWM values sent by analogWrite() are fractions of the maximum speed possible
// by your hardware
// turn on motors
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

// accelerate from zero to maximum speed

for (int i = 0; i < 256; i++) {
analogWrite(enA, i);
delay(20);
}
// decelerate from maximum speed to zero
for (int i = 255; i > 0; --i)
{
analogWrite(enA, i);
delay(20);
}

// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

}
void loop()
{
demoOne();
delay(1000);

}

Here are the codes that i use for my arduino

You should stop your motors before changing direction.

You should use code tags when you post your code. Read the forum guide in the sticky post before you break any more rules.

You should use better batteries. PP3 size 9V batteries are a very poor choice for use with motors. 6 or 8 AA NiMH works be better, or maybe a 11.2V Li-Po battery pack, for example.

// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

Is it L289N or L298N?

I can't find anything about the L289N driver. But L298N has 4 input pins, not just 2.

L298N my bad

so i need to change my power supply ?

Implement the following Truth Table (Fig-1) in your sketch to control the speed/direction of DC Motor using L298N Driver and UNO:


Figure-1:

You may try the following untested sketch:

int enA = 10;
int in1 = 7;
int in2 = 8;

void setup()
{
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}

void loop()
{
  forwardMove();
  delay(5000);  //keep turning for 5-ssec
  stop();
  backwardMove();
  delay(5000);
  stop();
}

void forwardMove()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 200);   //arbitrary speed
}

void stop()
{
  analogWrite(enA, 0);
}

void backwardMove()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 200);   //arbitrary speed
}

First, please fix your post that is breaking the forum rules.

I'm still confused. L298N has 4 input pins, not just 2. Anyway, if this helps I'm using the following class to drive 5V NEMA 8 stepper with L298N driver.

/*

    Bipolar stepper motor NEMA 8 with L298N driver (connect NEMA 8 green and black wires to motor A pins of L298N and red blue wires to motor B pins of L298N)

    For ESP32

    Version 001, 12.8.2024, Bojan Jurca

*/

#ifndef __L298N_NEMA8__
    #define __L298N_NEMA8__


    // TUNNING PARAMETERS

    #define __L298N_NEMA8_STEP_MODE__ 8 // 4 (1.8 ° / step) or 8 (0.9 ° / step)

    #define __L298N_NEMA8_STEPS_PER_AXIS_ROTATION__ (50 * __L298N_NEMA8_STEP_MODE__) // 200 for __L298N_NEMA8_STEP_MODE__ == 4, 400 for 8


    class L298N_NEMA8_t {

        public:

            L298N_NEMA8_t (byte pinIn1, byte pinIn2, byte pinIn3, byte pinIn4) { 

                // remember pins connected to ULN2003 controller
                __pinIn__ [0] = pinIn1;
                __pinIn__ [1] = pinIn2;
                __pinIn__ [2] = pinIn3;
                __pinIn__ [3] = pinIn4;

                // set pin mode to OUTPUT
                for (byte i = 0; i < 4; i++) {
                    pinMode (__pinIn__ [i], OUTPUT);
                    digitalWrite (__pinIn__ [i], 0);
                }
            }


            // ----- speed (speed or rather slowness) -----
            
            unsigned long microsToStayInStep = 1000; // 700 is the lovest value (the fastest speed) possible with no load (for __L298N_NEMA8_STEP_MODE__ == 4, 300 for 8)


            // ----- relative movement -----

            // (relative) move number of steps from current position
            void doSteps (long stepsToDo) {
                __lock__ ();

                __stepsToDo__ = stepsToDo;
                __absoluteStep__ += stepsToDo;
                
                __unlock__ ();
            }


            // ----- absolute movement -----

            // to be called only once - at initialization
            void defineInitialStep (long initialStep) {
                __lock__ ();

                __absoluteStep__ = initialStep;
                
                __unlock__ ();
            }


            // (absolute) move to target step
            void moveToStep (long targetStep) {
                __lock__ ();

                // correct __absoluteStep__ if the previous move has not cmpleted yet
                __absoluteStep__ -= __stepsToDo__;
                // do relative movement from here
                doSteps (targetStep - __absoluteStep__);

                __unlock__ ();
            }

            // get current step
            inline long getCurrentStep () __attribute__((always_inline)) { return __absoluteStep__; }


            void stop () {
                __lock__ ();

                __stepsToDo__ = 0;
                for (byte i = 0; i < 4; i++) 
                    digitalWrite (__pinIn__ [i], 0);       

                __unlock__ ();
            }

            inline bool isStopped () __attribute__((always_inline)) { return __stepsToDo__ == 0; }
            

            // call this function form loop ()
            void doThings () {
                __lock__ ();

                    unsigned long currentMicros = micros ();
                  
                    if (currentMicros - __lastMicros__ >= microsToStayInStep) { // time to take the next step
                        __lastMicros__ = currentMicros;
                        if (__stepsToDo__ > 0) { // do a step in clockwise direction
                        
                            __step__ = (__step__ + 1) % __L298N_NEMA8_STEP_MODE__; // 4 or 8
                            for (byte i = 0; i < 4; i ++) digitalWrite (__pinIn__ [i], __state__ [__step__][i]); 
                            __stepsToDo__ --;
                        
                        } else if (__stepsToDo__ < 0) { // do a step in anti-clockwise direction
                        
                            __step__ = (__step__ + (__L298N_NEMA8_STEP_MODE__ - 1)) % __L298N_NEMA8_STEP_MODE__; // 4 or 8
                            for (byte i = 0; i < 4; i ++) digitalWrite (__pinIn__ [i], __state__ [__step__][i]); 
                            __stepsToDo__ ++;

                        } else { // __stepsToDo__ == 0
                            
                            stop ();
                        
                        }
                    }      

                __unlock__ ();
            }


        private:

            // locking mechanism
            SemaphoreHandle_t __semaphore__ = xSemaphoreCreateRecursiveMutex (); 
            void __lock__ () { xSemaphoreTakeRecursive (__semaphore__, portMAX_DELAY); }
            void __unlock__ () { xSemaphoreGiveRecursive (__semaphore__); }


            long __stepsToDo__ = 0; // down-counter of steps yet to be done

            volatile byte __pinIn__ [4]; // pins connected to Uln2003 stepper controller

            byte __step__ = 0; // operational variable


            #if __L298N_NEMA8_STEP_MODE__ == 4

                volatile byte __state__ [4][4] = { // [state][pin]
                    {1, 0, 0, 0},   // coil A current direction 1
                    {0, 0, 1, 0},   //                              coil B current direction 1
                    {0, 1, 0, 0},   // coil A current direction 2
                    {0, 0, 0, 1}    //                              coil B current direction 2
                };

            #else

                volatile byte __state__ [8][4] = { // [state][pin]
                    {1, 0, 0, 0},   // coil A current direction 1
                    {1, 0, 1, 0},   // coil A current direction 1 + coil B current direction 1
                    {0, 0, 1, 0},   //                              coil B current direction 1
                    {0, 1, 1, 0},   // coil A current direction 2 + coil B current direction 1
                    {0, 1, 0, 0},   // coil A current direction 2
                    {0, 1, 0, 1},   // coil A current direction 2 + coil B current direction 2
                    {0, 0, 0, 1},   //                              coil B current direction 2
                    {1, 0, 0, 1}    // coil A current direction 1 + coil B current direction 2
                };

            #endif

            unsigned long __lastMicros__ = 0;

            long __absoluteStep__ = 0;

    };

#endif

You can use it like this:

#include "L298N-NEMA8.hpp"
L298N_NEMA8_t stepper (40, 38, 36, 34); // pins connected to L298N

void setup () {
        stepper.moveToStep (-80);
}

void loop () {
        stepper.doThings (); 
}

In the IDE click on Edit then Copy for Forum. That will copy your code so you can paste it here on the Forum.
A plain copy and paste does not work.

You can use the 9V battery for now but you will find that it won't last long.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.