搜索代码

Using an Arduino Motor Shield with an L298N chip

Using an Arduino Motor Shield with an L298N chip

This project demonstrates how to control two DC motors using an Arduino Uno and a motor shield based on the L298N motor driver chip. This versatile setup allows for independent control of each motor's speed and direction, along with braking functionality. The shield simplifies the wiring and provides a compact solution for driving motors in various projects.

Here are some project ideas using this setup:

  • Build a small robot with independent wheel control.
  • Create a motorized platform for a camera or sensor.
  • Design a simple conveyor belt system.
  • Control the movement of a small vehicle or toy.

Hardware/Components

  • Arduino Uno
  • L298N Motor Shield
  • Two DC motors
  • External power supply (for larger motors)
  • Connecting wires

Wiring Guide

The motor shield plugs directly onto the Arduino Uno. Connect the motors to the screw terminals labeled "Motor A" and "Motor B" on the shield (in video at 08:27). The polarity of the motor connections doesn't matter initially, as the code controls the direction. If the motor spins the wrong way, simply swap the wires. For small motors consuming less than 500mA, power can be drawn directly from the Arduino. Larger motors will require an external power supply connected to the shield's power terminals (in video at 09:03).

%%WIRING%%

Code Explanation

The code defines pin assignments for each motor's direction, speed, and brake. It's crucial to use the correct pins corresponding to the motor shield's design. The code also includes a helpful function, moveMotor(), for simplified control (in video at 13:21).


// Pin assignments for Motor A & B (DO NOT CHANGE)
const int MotorPinA = 12; 
const int MotorSpeedPinA = 3; 
const int MotorBrakePinA = 9;

const int MotorPinB = 13; 
const int MotorSpeedPinB = 11;
const int MotorBrakePinB = 8;

const int CW  = HIGH; // Clockwise direction
const int CCW = LOW; // Counter-clockwise direction

The moveMotor() function takes the motor ('A' or 'B'), direction (CW or CCW), and speed (0-255) as arguments (in video at 13:21). This function simplifies motor control within the loop() function.


void moveMotor(char motor, int dir, int speed) {
  // ... (implementation details omitted)
}

The brake() function allows you to apply or release the brake for a specific motor (in video at 14:06). It takes the motor ('A' or 'B') and brake state (1 for brake, 0 for release) as arguments.


void brake(char motor, int brk) {
  // ... (implementation details omitted)
}

Live Project/Demonstration

The video demonstrates controlling individual motors, adjusting speed and direction, and applying the brake. It also shows how to control two motors simultaneously with different parameters, including running a large motor alongside a smaller one (in video at 19:14).

Chapters

  • [00:00] Introduction to the Arduino Motor Shield
  • [00:59] Explanation of the Motor Shield
  • [06:41] Overview of the L298N Datasheet
  • [08:27] Hardware Demonstration and Motor Connections
  • [09:28] Code Explanation: Basic Motor Control
  • [12:36] Demonstration: Single Motor Control
  • [13:21] Code Explanation: Advanced Functions (moveMotor, brake)
  • [14:39] Demonstration: Advanced Motor Control
  • [17:43] Demonstration: Dual Motor Control
  • [19:14] Demonstration: Controlling Motors with Different Power Requirements
148-Basic code to control a DC motor using an Arduino motor shield
语言: C++
/*
 * Code to control 2 DC motors using Arduino Motor Shield (Basic Code)
 * Written by Ahmad Shamshiri for Robojax.com on August 28, 2018 at 21:33 in Ajax, Ontario, Canada
 * Watch video instruction for this code:https://youtu.be/kIgbjyqNrV8
 * Watch how to use current sensing with Motor Shield : https://youtu.be/-uQKBDTWHPM
 * 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 * 
 */
const int MotorPinA = 12; // for motor A
const int MotorSpeedPinA = 3; // for motor A
const int MotorBrakePinA = 9; // for motor A


const int MotorPinB = 13; // for motor B
const int MotorSpeedPinB = 11;// for motor B
const int MotorBrakePinB = 8;// for motor B

const int CW  = HIGH;
const int CCW = LOW;

void setup() {
  // motor A pin assignment
  pinMode(MotorPinA, OUTPUT);
  pinMode(MotorSpeedPinA, OUTPUT);
  pinMode(MotorBrakePinA, OUTPUT);

  // motor B pin assignment
  pinMode(MotorPinB, OUTPUT);
  pinMode(MotorSpeedPinB, OUTPUT);
  pinMode(MotorBrakePinB, OUTPUT); 


  Serial.begin(9600);//  serial monitor initialized 

}

void loop() {

  //start motor A at maximum speed
  digitalWrite(MotorPinB, CW);// set direction
      Serial.println("Direction CW"); 
  analogWrite(MotorSpeedPinB, 100);// set speed at maximum
      Serial.println("Speed 100");
  delay(5000);// run for 5 seconds
  digitalWrite(MotorBrakePinB, HIGH);// brake
      Serial.println("Brake applied");
  delay(2000);  
  digitalWrite(MotorBrakePinB,LOW);// release brake
      Serial.println("Brake removed");  
  analogWrite(MotorSpeedPinB, 70);// set to 1/3
      Serial.println("Speed at 70");  
  delay(5000);// 



}// loop end
149-Advanced code to control a DC motor using an Arduino motor shield
语言: C++
++
/*
 * Code to control 2 DC motors using Arduino Motor Shield (Advanced)
 * Using this code you can control 2 motors very easily
 * 
 * Written by Ahmad Shamshiri for Robojax.com on August 28, 2018 at 21:33 in Ajax, Ontario, Canada
 * Watch video instruction for this code:https://youtu.be/kIgbjyqNrV8
 * 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 * 
 */
const int MotorPinA = 12;
const int MotorSpeedPinA = 3;
const int MotorBrakePinA = 9;


const int MotorPinB = 13;
const int MotorSpeedPinB = 11;
const int MotorBrakePinB = 8;

const int CW  = HIGH;
const int CCW = LOW;

const int showComments = 1;// show comments in serial monitor

void setup() {
  // motor A pin assignment
  pinMode(MotorPinA, OUTPUT);
  pinMode(MotorSpeedPinA, OUTPUT);
  pinMode(MotorBrakePinA, OUTPUT);

  // motor B pin assignment
  pinMode(MotorPinB, OUTPUT);
  pinMode(MotorSpeedPinB, OUTPUT);
  pinMode(MotorBrakePinB, OUTPUT); 


  Serial.begin(9600);//  serial monitor initialized

}

void loop() {
  brake('A', 0); // release brake
    brake('B', 0); // release brake
  moveMotor('A', CCW, 100);// motor A rotate CCW at 100 PWM value
    moveMotor('B', CW, 145);// motor B rotate CW at 145 PWM value
  delay(3000);
  brake('A',1);
  brake('A',0);
    brake('B',1);
  brake('B',0);
  moveMotor('A', CW, 255);
      moveMotor('B', CCW, 145);  
  delay(5000);
  brake('A',1);


}// loop end



/*
 * 
 * Written by Ahmad Shamshiri August 29, 2018 at 20:59 in Ajax, Ontario, Canada 
 * moveMotor controls the motor
  @param motor is char A or B referring to motor A or B.
  @param dir is motor direction, CW or CCW
  @param speed is PWM value between 0 to 255

  Example 1: to start moving motor A in CW direction with 135 PWM value
  moveMotor('A', CW, 135);

  Example 2: to start moving motor B in CCW direction with 200 PWM value
  moveMotor('B', CCW, 200);  
 */

void moveMotor(char motor, int dir, int speed)
{
  int motorPin;
  int motorSpeedPin;
  
  if(motor =='A')
  {
    motorPin      = MotorPinA;
    motorSpeedPin = MotorSpeedPinA;  
  }else{
    motorPin      = MotorPinB;
    motorSpeedPin = MotorSpeedPinB;     
  }
   digitalWrite(motorPin, dir);// set direction for motor
   analogWrite(motorSpeedPin, speed);// set speed of motor   
}//moveMotor end

/*
 * brake, stops the motor, or releases the brake
 * @param motor is character A or B
 * @param brk if 1, brake; if 0, release brake
 * example of usage:
 * brake('A', 1);// applies brake to motor A
 * brake('A', 0);// releases brake from motor A
 */
void brake(char motor, int brk)
{
  if(motor =='A')
  {
    digitalWrite(MotorBrakePinA, brk);// brake
    delay(1000);
  }else{
    digitalWrite(MotorBrakePinB, brk);// brake
    delay(1000);
   
  }
}

资源与参考

文件📁

没有可用的文件。