Using Arduino Motor Shield with L298P chip
Code to use Arduino Motor Shield L298N
This video shows you how to use Arduino Motor Shield to control two DC motors. If you want to measure the current and do something Current Senseing with L298N code and video
- Current Senseing with L298N
- Arduino Motor Shield (official web page)
- L298 Full Bridge Motor Driver Data Sheet
- Leran Arduino in 30 Minuetes (video)
- Robojax Arduino Course on Udemy
- Get Early Access to my videos via Patreon
Basic Code to control DC motor using Arduino Motor Shield
/*
* Code to control 2 DC motors using Arduino Motor Shield (Basic Code)
* Written by Ahmad Shamshiri for Robojax.com on Aug 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 download 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);// seial 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
Advanced Code to control DC motor using Arduino Motor Shield
/*
* 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 Aug 28, 2018 at 21:33 in Ajax, Ontario, Canada
* Watch video instruction for this code:https://youtu.be/kIgbjyqNrV8
*
* This code has been download 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);// seial monitor initialized
}
void loop() {
brake('A', 0); // release brake
brake('B', 0); // release brake
moveMotor('A', CCW, 100);// motot A rotate CCW at 100 PWM value
moveMotor('B', CW, 145);// motot 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 refering to motor A or B.
@param dir is motor direction, CW or CCW
@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);
}
}