L298 Motor Driver Tutorial (with Arduino Code)

Brushed DC motors are the general-purpose workhorses of all things electronic. Without them, your robot would be a stationary lump of plastic and metal. The theory behind operating these is simple: apply a voltage between the terminals and it spins. More voltage = faster spinning and more torque. However, it can be a little tricky to control them digitally with a microcontroller, such as an Arduino. For starters, each pin can only give a voltage of either 0V or 5v (or anything in between using pulse width modulation – PWM). Also, the maximum current one can draw from each pin is something like 40mA, which is but a trickle compared to what motors typically draw. The solution? Transistors, of course! More specifically, a motor driver, which is an array of transistors (an H-bridge to be exact) that gives you full control over a motor.

Enter the L298, which is dual motor driver designed to accept standard TTL logic inputs. There are loads of other chips out there that do basically the same thing. The L298 just happens to be popular. What’s important is that:

  1. You can drive it using standard 0-5V logic levels.
  2. It can give LOTS (relatively speaking) of current. 4A, to be specific.

And the word “dual” here means it can control 2 separate motors! For around $5.00, that’s pretty wonderful! We haven’t even reached the best part yet: it’s dead-easy to control! Here’s how it works. There are 4 main inputs:

  1. IN1
  2. IN2
  3. IN3
  4. IN4

..and 4 main outputs:

  1. OUT1
  2. OUT2
  3. OUT3
  4. OUT4

The logic level applied to any of the inputs will result in a voltage of either 0V or your supply voltage (more on that later) being applied to the corresponding output. So, if you write a ‘high’ to IN3, you’ll get power on OUT3. You’re supposed to connect a motor between OUT1 and OUT2, and another motor between OUT3 and OUT4. Here’s a table illustrating the operation:

Input Pin Logic Applied Output Pin Voltage on Output Motor Action
IN1 0 OUT1 0 Motor A doesn't spin.
IN2 0 OUT2 0
IN1 0 OUT1 0 Motor A spins.
IN2 1 OUT2 Vs
IN1 1 OUT1 Vs  Motor A spins backwards.
IN2 0 OUT2 0
IN1 1 OUT1 Vs  Motor A doesn't spin.
IN2 1 OUT2 Vs
IN3 0 OUT3 0 Motor B doesn't spin.
IN4 0 OUT4 0
IN3 0 OUT3 0 Motor B spins.
IN4 1 OUT4 Vs
IN3 1 OUT3 Vs  Motor B spins backwards.
IN4 0 OUT4 0
IN3 1 OUT3 Vs  Motor B doesn't spin.
IN4 1 OUT4 Vs

Simple, right? Now, there’s 3 pins on the IC that are for power. One of them is ground, of course. The other is a logic supply, which should be the same voltage as your logical ‘high’ (typically 3.3V or 5V). The final power pin is a supply pin; this is the pin that connects directly to the power supply’s positive terminal. It will be the voltage used to drive the motors. According to the datasheet, the supply can go up to 46V – very high, especially for DC. For typical small robotics applications, you’re going to want to use a supply somewhere in the range of 6-12V.

There are two more input pins you need to know about; these are the enable pins, ENA and ENB. In order for the motor to work, you need to bring the appropriate enable pin high (ENA for motor A between OUT1 and OUT2, and ENB for motor B between OUT3 and OUT4). You can also use these to control the motor speed. How, you may ask? Using PWM, of course! Here’s how it works:

If you rapidly turn the enable pin on and off (on 50% of the time and off 50% of the time), the average voltage on the output will be 50% of the supply voltage. This is known as a 50% duty cycle. Likewise, if you have a 70% duty cycle (on 70% of the time and off 30% of the time), your output voltage will be 70% of the supply voltage. Since motors are an inductive load, this will translate fairly accurately into motor speed.

Finally (I promise, no more pins after this), there are two “current sense” pins that must be tied to ground for the IC to work. As the name suggests, you can use these pins to sense how much current is being drawn by the motors, via a current sense resistor and ohm’s law. I won’t go into too much detail, but if you want to know exactly how, look at the datasheet!

Here’s some code for Arduino to test out the chip. It uses a standard 16×2 LCD to tell you what it’s doing with the motor.