Section outline

  • โš™๏ธ Using Motor Drivers โ€“ L298N and Alternatives

    Arduino alone cannot directly power motors, especially DC motors or stepper motors. This is where motor drivers come in. They act as intermediaries between the Arduino and the motors.

    • โ“ Why Motor Drivers Are Essential

      • Motors draw more current than an Arduino can safely supply.
      • Motor drivers allow you to control speed and direction using digital signals.
      • They protect your microcontroller from power surges or back EMF (reverse current from motors).

      ๐Ÿ”Œ Meet the L298N Dual H-Bridge Driver

      • Can control two DC motors independently.
      • Handles up to 2A per channel.
      • Can also control stepper motors.

      ๐Ÿ“ Pinout of L298N

      ย 

      Pin Name Description
      IN1, IN2 Control Motor A
      IN3, IN4 Control Motor B
      ENA, ENB Enable pins for motors A and B (can be connected to PWM for speed control)
      VCC Connect to motor power supply (e.g., 9V battery)
      GND Ground connection
      5V Optional regulated output for Arduino if jumper is used

      ๐Ÿงฐ Basic Wiring to Arduino

      • IN1 โ†’ D7, IN2 โ†’ D6 (Motor A)
      • IN3 โ†’ D5, IN4 โ†’ D4 (Motor B)
      • ENA โ†’ D9 (PWM control), ENB โ†’ D10 (PWM control)
      • VCC to 9V battery, GND to Arduino GND
    • ๐Ÿ’ป Sample Code for Direction and Speed Control

      int ENA = 9;
      int IN1 = 7;
      int IN2 = 6;
      
      void setup() {
        pinMode(ENA, OUTPUT);
        pinMode(IN1, OUTPUT);
        pinMode(IN2, OUTPUT);
      }
      
      void loop() {
        // Move forward
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        analogWrite(ENA, 150); // Set speed (0-255)
        delay(2000);
      
        // Stop
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        delay(1000);
      
        // Reverse
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        analogWrite(ENA, 150);
        delay(2000);
      }
      

      ๐Ÿ”„ Alternatives to L298N

      • L293D: Similar but with lower current rating (600mA)
      • TB6612FNG: More efficient and compact, preferred for battery-powered robots

      Choosing the right motor driver depends on the current requirement and available space in your robot. L298N is a great starting point for beginners due to its popularity and simplicity.