The library is forked from L298n.
The original library is designed for Arduino board, using analogWrite
, which may cause some problem on ESP32.
Using ESP32_AnalogWrite or AnalogWrite_ESP32 can overwrite ledcWrite
of ESP32 to analogWrite
.
As pablomarquez76 saind, ESP32 has supported analogWrite
after 2.0.11. The official document is at here.The resolution is form 0 to 255, testing normal. So nothing needs change.
I added a method to get input from -255 to 255 to control the motor. It is for easier direction control.
I tested it with PID library. Sometimes PID always output 0. Sometimes it works. I didn't find the reason. It might because ESP32Encoder or this repo use the same timer with PID. I am not sure. Be careful.
The below is original readme.
An easy to use L298N library to control DC Motors with Arduino.
As you know, any L298N module has the ability to drive two motors at once, but you may not know that one instance of the library is intended to pilot only one motor. So if you need to drive two motors, you have to instantiate two L298N objects (one for each motor).
To instantiate both motors at once use the L298NX2 variant of the library.
L298NX2 is not a new version of module or IC, but it stands for a double implementation of L298N library.
Download this repository as a .zip file and from the Arduino IDE go to Sketch -> Include library -> Add .ZIP Library
You can import the library in your code using the Arduino IDE going to Sketch -> Include library -> L298N or directly writing the include statement in your code:
// For single motor instance
#include <L298N.h>
// For two motors instance at once
#include <L298NX2.h>
To drive a motor the first thing is to create an instance of the library.
// With Enable pin to control speed
L298N myMotor(EN, IN1, IN2);
// Without Enable pin with jumper in place
L298N myMotor(IN1, IN2);
- EN = is the Arduino pin (requires a PWM pin) connected to the Enable pin of the module
- IN1 and IN2 are the two digital pins connected to IN1 and IN2 pins of the module
// With Enable pin to control speed
motor A motor B
|------------------||------------------|
L298NX2 myMotors(EN_A, IN1_A, IN2_A, EN_B, IN1_B, IN2_B);
// Without Enable pin and jumper in place
motor A motor B
|------------||------------|
L298NX2 myMotors(IN1_A, IN2_A, IN1_B, IN2_B);
Method | Params | Description |
---|---|---|
setSpeed | unsigned short pwmVal | Set the PWM value used to determine the motor speed. Value from 0 to 255. |
getSpeed | none | Get the speed previously set. |
forward | none | Run motor in forward direction (depends on wiring). |
forwardFor | unsigned long delay | Run motor in forward direction for a time specified by delay. |
forwardFor | unsigned long delay, CallBackFunction callback | Run motor in forward direction for a time specified by delay, after moving execute a callback function. |
backward | none | Run motor in backward direction (depends on wiring). |
backwardFor | unsigned long delay | Run motor in backward direction for a time specified by delay. |
backwardFor | unsigned long delay, CallBackFunction callback | Run motor in backward direction for a time specified by delay, after moving execute a callback function. |
run | uint8_t direction | Move motor. To specify the direction use one of L298N::FORWARD, L298N::BACKWARD or L298N::STOP. |
runFor | unsigned long delay, L298N::Direction direction | Like forwardFor or backwardFor but more flexible. To specify the direction use one of L298N::FORWARD, L298N::BACKWARD or L298N::STOP. |
runFor | unsigned long delay, L298N::Direction direction, CallBackFunction callback | Like previous with the ability to execute a callback function. |
stop | none | Stop the motor. |
reset | none | Used to re-enable motor movements after the use of runFor, forwardFor or backwardFor methods. |
isMoving | none | Returns a boolean indicating if motor is running or not. |
getDirection | none | Returns the current L298N::Direction. |
Constant | Int Value |
---|---|
L298N::FORWARD | 0 |
L298N::BACKWARD | 1 |
L298N::STOP | -1 |
L298NX2 have the same methods of L298N identified by A or B suffix to drive each motor. For example setSpeed for motor A is setSpeedA and setSpeedB for motor B.
Methods without suffix, will affects both motors.
Method | Params |
---|---|
setSpeed | unsigned short pwmVal |
forward | none |
forwardFor | unsigned long delay, CallBackFunction callback |
forwardFor | unsigned long delay |
backward | none |
backwardFor | unsigned long delay, CallBackFunction callback |
backwardFor | unsigned long delay |
run | uint8_t direction |
runFor | unsigned long delay, L298N::Direction direction |
runFor | unsigned long delay, L298N::Direction direction, CallBackFunction callback |
stop | none |
reset | none |
Examples can be founds in the examples folder or if installed in Arduino IDE, by the Menu -> File -> Example -> L298N.
Wiring schema can be found in schema folder.
Coffees are accepted! :coffee: PayPal Donations Here ☕