CONTROLLING DC MOTOR WITH KEYBOARD

DC motor setup

The code can be downloaded at :

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/DC_MOTOR_VIA_L298N.zip

// Motor 1

int dir1PinA = 2;

int dir2PinA = 3;

int speedPinA = 9; // Needs to be a PWM pin to be able to control motor speed

// Motor 2

int dir1PinB = 4;

int dir2PinB = 5;

int speedPinB = 10; // Needs to be a PWM pin to be able to control motor speed

void setup() { // Setup runs once per reset

// initialize serial communication @ 9600 baud:

Serial.begin(9600);

//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);

pinMode(dir2PinA,OUTPUT);

pinMode(speedPinA,OUTPUT);

pinMode(dir1PinB,OUTPUT);

pinMode(dir2PinB,OUTPUT);

pinMode(speedPinB,OUTPUT);

}

void loop() {

// Initialize the Serial interface:

if (Serial.available() > 0) {

int inByte = Serial.read();

int speed; // Local variable

switch (inByte) {

//______________Motor 1______________

case ‘1’: // Motor 1 Forward

analogWrite(speedPinA, 255);//Sets speed variable via PWM

digitalWrite(dir1PinA, LOW);

digitalWrite(dir2PinA, HIGH);

Serial.println(“Motor 1 Forward”); // Prints out “Motor 1 Forward” on the serial monitor

Serial.println(” “); // Creates a blank line printed on the serial monitor

break;

case ‘2’: // Motor 1 Stop (Freespin)

analogWrite(speedPinA, 0);

digitalWrite(dir1PinA, LOW);

digitalWrite(dir2PinA, HIGH);

Serial.println(“Motor 1 Stop”);

Serial.println(” “);

break;

case ‘3’: // Motor 1 Reverse

analogWrite(speedPinA, 255);

digitalWrite(dir1PinA, HIGH);

digitalWrite(dir2PinA, LOW);

Serial.println(“Motor 1 Reverse”);

Serial.println(” “);

break;

//______________Motor 2______________

case ‘4’: // Motor 2 Forward

analogWrite(speedPinB, 255);

digitalWrite(dir1PinB, LOW);

digitalWrite(dir2PinB, HIGH);

Serial.println(“Motor 2 Forward”);

Serial.println(” “);

break;

case ‘5’: // Motor 1 Stop (Freespin)

analogWrite(speedPinB, 0);

digitalWrite(dir1PinB, LOW);

digitalWrite(dir2PinB, HIGH);

Serial.println(“Motor 2 Stop”);

Serial.println(” “);

break;

case ‘6’: // Motor 2 Reverse

analogWrite(speedPinB, 255);

digitalWrite(dir1PinB, HIGH);

digitalWrite(dir2PinB, LOW);

Serial.println(“Motor 2 Reverse”);

Serial.println(” “);

break;

default:

// turn all the connections off if an unmapped key is pressed:

for (int thisPin = 2; thisPin < 11; thisPin++) {

digitalWrite(thisPin, LOW);

}

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s