notes on controlling DC motors with Arduino and Motor Driver Shield L298NH

This blog post considers techniques for controlling DC motors using an Arduino and a motor driver shield module also designed for managing two motors with up to 2A current per channel featuring a dual full-bridge (H-bridge) driver design for bidirectional motor control. It explores essential components, including programming principles for pin setup and motor control logic. The post also provides an illustrative Arduino sketch to manage direction, speed, and braking functionality for two motor channels. 

Example


(Vin interrupted)

Materials

- Arduino UNO
- USB cable
Motor Driver Shield L298NH
- DC motors
- breadboard
- wires
- power source

Setup


Sketch

int directionPinA = 12;
int pwmPinA = 3;
int brakePinA = 9;

int directionPinB = 13;
int pwmPinB = 11;
int brakePinB = 8;

//boolean to switch direction
bool directionStateA;
bool directionStateB;

void setup() {
Serial.begin(9600);
//define pins
pinMode(directionPinA, OUTPUT);
pinMode(pwmPinA, OUTPUT);
pinMode(brakePinA, OUTPUT);
pinMode(directionPinB, OUTPUT);
pinMode(pwmPinB, OUTPUT);
pinMode(brakePinB, OUTPUT);
Serial.println("end setup");
}

void loop() {
Serial.println("start loop");
Serial.println("motor A");
delay(10);
directionStateA = !directionStateA;
if(directionStateA == false){
digitalWrite(directionPinA, LOW);
}
else{
digitalWrite(directionPinA, HIGH);
}
digitalWrite(brakePinA, LOW);
analogWrite(pwmPinA, 70);
delay(1000);
digitalWrite(brakePinA, HIGH);
analogWrite(pwmPinA, 0);
delay(500);
Serial.println("motor B");
delay(10);
directionStateB = !directionStateB;
if(directionStateB == false){
digitalWrite(directionPinB, LOW);
}
else{
digitalWrite(directionPinB, HIGH);
}
digitalWrite(brakePinB, LOW);
analogWrite(pwmPinB, 70);
delay(1000);
digitalWrite(brakePinB, HIGH);
analogWrite(pwmPinB, 0);
delay(500);
}

Comments