Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created November 16, 2018 23:19
Show Gist options
  • Save liz-miller/2a8b27cd7ae32dcde5516510a926f6fd to your computer and use it in GitHub Desktop.
Save liz-miller/2a8b27cd7ae32dcde5516510a926f6fd to your computer and use it in GitHub Desktop.
L298N Motor Controller Example
/*
* Overview of L298N controller for Robotics
* Controls how our robot moves.
* Developed for the Beginner Bots lesson series on www.learnrobotics.org/blog
* Written by Liz Miller
* 11/16/18
*/
//Left Motor
int enableA = 10;
int motorA1 = 9;
int motorA2 = 8;
//Right Motor
int enableB = 5;
int motorB3 = 7;
int motorB4 = 6;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(enableA, OUTPUT); //enabled + PWM
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(enableB, OUTPUT); //enabled + PWM
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
}
/*
* Translate the truth table to create moves
*/
void forward(int mSpeed1,int mSpeed2, int duration){
digitalWrite(enableA, HIGH);
digitalWrite(enableB, HIGH);
digitalWrite(motorA1, HIGH);
digitalWrite(motorB1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB2, LOW);
analogWrite(enableA, mSpeed1); //0-255
analogWrite(enableB, mSpeed2); //0-255
delay(duration);
}
void stopMotors(){
digitalWrite(enableA, HIGH);
digitalWrite(enableB, HIGH);
digitalWrite(motorA1, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorA2, LOW);
digitalWrite(motorB2, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
foward(127, 127, 3000);
stopMotors();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment