The L298N library: a DC motor forward, back and at any speed
The L298N library by Andrea Lombardo wraps one channel of an L298N dual H bridge (or an L293D or L9110) in a small object. You give it the enable pin and the two direction inputs, and it does the bookkeeping: forward() sets IN1 high and IN2 low, backward() flips them, and setSpeed() takes 0 to 255 and writes it to the enable pin with analogWrite().
Built in on Mokxi: #include <L298N.h> and it builds, with nothing to install. Free, and no account needed.
What L298N does
The red L298N boards ship with a jumper on ENA and ENB that ties enable to 5 V, which means full speed always. To control speed, pull the jumper off and wire ENA to a PWM pin; built without an enable pin, the library鈥檚 setSpeed() has nothing to act on. L298NX2 is the same thing for both channels at once, as on a two-wheel robot.
Its timed calls are the useful extra: forwardFor(), backwardFor() and runFor() do not block. Called on every pass of loop(), they keep the motor running until the time is up, stop it and call your function once, so the sketch can read sensors while the motor runs.
| Library Manager name | L298N |
|---|---|
| Author | Andrea Lombardo |
| Include | #include <L298N.h>#include <L298NX2.h> |
| Boards | every board Mokxi compiles for but micro:bit V2 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Motor driver module, L298N, DC motor |
Install it
It builds on every board Mokxi compiles for but micro:bit V2.
In Mokxi it is built in: #include <L298N.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "L298N" by Andrea Lombardo from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add L298N to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.3, so a sketch written here builds there too.
ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 ATtiny85 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
L298N motor(EN, IN1, IN2)
One motor channel: the PWM enable pin, then the two direction inputs. L298N motor(IN1, IN2) is the form for a board with the enable jumper on.
motor.setSpeed(200)
Speed from 0 to 255, written to the enable pin. It takes effect on the next forward() or backward(), and at once if the motor is already running.
motor.forward()
Runs the motor one way: IN1 high, IN2 low.
motor.backward()
Runs it the other way: IN1 low, IN2 high.
motor.stop()
Both inputs low with enable on, which brakes the motor rather than letting it coast.
motor.forwardFor(3000, done)
Runs forward for 3000 ms without blocking, then stops and calls done() once. It must be called every pass of loop(), and reset() arms it again.
void done() { Serial.println("finished"); }
void loop() {
motor.forwardFor(3000, done);
}motor.isMoving()
True while the motor is running forward or backward.
L298NX2 motors(ENA, IN1, IN2, ENB, IN3, IN4)
Both channels as one object, with forwardA(), forwardB() and the same calls for each side.
Example: A DC motor on an L298N
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <L298N.h>
const unsigned int EN = 9; // a PWM pin
const unsigned int IN1 = 8;
const unsigned int IN2 = 7;
L298N motor(EN, IN1, IN2);
void setup() {
Serial.begin(9600);
motor.setSpeed(255);
motor.forward();
Serial.print("moving: ");
Serial.println(motor.isMoving());
}
void loop() {}Wiring
- Arduino Uno R3 9 to Motor driver module, L298N ENA
- Arduino Uno R3 8 to Motor driver module, L298N IN1
- Arduino Uno R3 7 to Motor driver module, L298N IN2
- Motor driver module, L298N OUT1 to DC motor 1
- Motor driver module, L298N OUT2 to DC motor 2
- Power VCC to Motor driver module, L298N 12V
- Arduino Uno R3 GND to Motor driver module, L298N GND
Common errors and how to fix them
setSpeed() changes nothing and the motor always runs flat out
The ENA (or ENB) jumper is still on the board, tying enable to 5 V. Remove it and wire the ENA pin to a PWM-capable Arduino pin, and pass that pin to the constructor.
The motor hums at low speed but does not start
The L298N drops about 2 V across its transistors and a small motor needs a push to get going. Use a higher setSpeed() (often 120 or more) to start, then lower it, or a higher motor supply.
The motor runs on USB power but the Arduino resets when it starts
The motor needs its own supply on the board鈥檚 12V terminal. Join the L298N鈥檚 GND to the Arduino鈥檚 GND so the control signals share a reference, but do not power the motor from the Arduino鈥檚 5V pin.
Keep going
Learn
Try L298N without installing anything
Open the example, change the sketch, and keep your version in a free account.