• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
  • YouTube
  • MakerWorld
  • Instagram
  • Threads
  • Bluesky
  • Mastodon
  • Facebook
makerhacks-logo

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • 3D Printing
  • Laser Cutting
  • CNC
  • Programming
  • Free Arduino Course
  • Free Robotics Course
  • Recommendations

Changing Direction with Tank Turning – L298n Breakout and Two Motors

You are here: Home / Robotics Course / Changing Direction with Tank Turning – L298n Breakout and Two Motors

In this lesson we add a second motor, which allows us to change direction using what are often called “tank turns”.

The way this works is, if you want to go forward or back then both motors work in the same direction. When you turn left, your right hand motor goes forward and the left one goes backward, and the opposite for turning right.

If you want to get extra fancy then instead of turning in place, you can make one side go slow and the other fast so you still have forward motion but one side overtakes the other.

Note that now we have more than just f and r that we make the string we get from serial into lower case. This means we don’t have to check for both ‘F’ and ‘f’ in our if statements!

As before, in my case I have both grounds plugged into the GND on the motor driver board, but you can also ensure the grounds are connected by using the breadboard.

Code

Serial Monitor settings: in the Arduino IDE, open the Serial Monitor, set the speed to 115200 baud and the line ending to “Carriage return”, then type f (forward), b (back), l (left) or r (right), a comma and a speed from 0 to 255, such as f,200, and press Enter. Anything else stops the motors.

/*

 In this sketch we control a motor using
 the L298N H-Bridge breakout.

 We use pins 3 to 8 for this example.

 */ 


// input pins

// these control direction for motor A
int pin1A = 4; 
int pin2A = 5;

// these control direction for motor B
int pin1B = 7; 
int pin2B = 8;

// set on/off and speed
int enablepinA = 3;
int enablepinB = 6;

// how much power to send
int power = 0; 

// set things up ...
void setup() {

 // start the serial connection
 Serial.begin(115200);

 // tell us we are starting
 Serial.println("Starting ...");

 // declare both pins for each motor
 // to be output:
 pinMode(pin1A, OUTPUT);
 pinMode(pin2A, OUTPUT);
 pinMode(pin1B, OUTPUT);
 pinMode(pin2B, OUTPUT);
 

 // enable pin is also output
 pinMode(enablepinA, OUTPUT);
 pinMode(enablepinB, OUTPUT);
}

// keep doing this forever:
void loop() {

 // play with the power setting to see
 // the effects
 while (Serial.available() > 0) {

   // 'F'orward, 'L'eft, 'R'ight or 'B'ackwards; anything else stops
   String direction = Serial.readStringUntil(',');
   direction.trim(); // ignore any stray newline from the Serial Monitor

   // Convert to lower case so we don't have to check
   // for upper and lower case commands
   direction.toLowerCase();
   
   // look for the next power number in the serial monitor
   // turn it into an integer and place in our power variable:
   int power = Serial.parseInt();
   
   // look for the carriage return (Enter)
   if (Serial.read() == '\r') {
  
     // report back the number we were provided:
     Serial.print(power);
     
     if(direction == "f") {
        forward(power);
     
     } else if (direction == "r") {
        right(power);

     } else if (direction == "l") {
        left(power);

     } else if (direction == "b") {
        back(power);

     } else {
        // anything else (a typo, say) stops rather than guessing
        stop();
     }
     // output text to say we got it
     Serial.println(" - Roger!");
   }
  }
}
// Drive both motors forward
void forward(int power)
{

  digitalWrite(pin1A, LOW);
  digitalWrite(pin2A, HIGH);
  digitalWrite(pin1B, HIGH);
  digitalWrite(pin2B, LOW);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);

}

// Drive A forward and B backward
void right(int power)
{

  digitalWrite(pin1A, LOW);
  digitalWrite(pin2A, HIGH);
  digitalWrite(pin1B, LOW);
  digitalWrite(pin2B, HIGH);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);

}

// Drive A backward and B forwards
void left(int power)
{

  digitalWrite(pin1A, HIGH);
  digitalWrite(pin2A, LOW);
  digitalWrite(pin1B, HIGH);
  digitalWrite(pin2B, LOW);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);

}

// Drive both motors backwards
void back(int power)
{
  digitalWrite(pin1A, HIGH);
  digitalWrite(pin2A, LOW);
  digitalWrite(pin1B, LOW);
  digitalWrite(pin2B, HIGH);
  
  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);

}

// Stop both motors
void stop()
{
  analogWrite(enablepinA, 0);
  analogWrite(enablepinB, 0);
}
Previous L298N BreakoutsCourse contentsNext Infrared Sensor

Robot Course Lessons

  • Introduction
  • Choosing a Brain for your robot
  • Motors and Actuators
  • Basic Motor Controllers
  • Sensors
  • Breadboards, Batteries, Chassis, Wheels and Wires
  • Introduction
  • Basic DC Motor Movement
  • Forward and Reverse with Servos
  • Controlling DC Motors with the L293D H-Bridge Chip
  • Using Motor Drivers, Shields and Breakouts
  • Stepper Drivers
  • L298N Breakouts
  • Changing Direction with Tank Turning – L298n Breakout and Two Motors
  • Infrared Sensor
  • IR Remote Control
  • Robot Chassis
Creative Commons Attribution-ShareAlike 4.0 licence

Free to use, adapt and share. This course by Chris Garrett, Maker Hacks, is licensed under Creative Commons Attribution-ShareAlike 4.0. Workshops, schools and clubs are welcome to use it, including in your own handouts and sessions. Just credit “Chris Garrett, Maker Hacks (makerhacks.com)” with a link to the lesson, and share any adapted versions under the same licence. Embedded videos and other people’s content keep their own terms.

Improved a lesson or spotted a mistake? I would love to hear about it: get in touch.

  • 3D Printing
  • CNC
  • Electronics and Hardware
  • Laser Cutting
  • News and Reviews
  • Software and Programming

arduino budget cad cnc diode laser glowforge Hacks Ideas laser cutter linux Makes making python raspberry pi resin Reviews technology tips xTool Lasers

  • YouTube
  • MakerWorld
  • Instagram
  • Threads
  • Bluesky
  • Mastodon
  • Facebook

.

Maker Hacks Blog Copyright © 2026 · All Rights Reserved · Privacy Policy