Skip to content

YorkURobotics/l298n-module-extended-lib

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

L298N Library

An easy to use L298N library to control DC Motors with Arduino.

NOTICE

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.

INSTALL THE LIBRARY

Download this repository as a .zip file and from the Arduino IDE go to Sketch -> Include library -> Add .ZIP Library

IMPORT

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>

INSTANTIATE THE MODULE

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

Same thing for L298NX2 variant

// 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);

L298N Methods

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.

Direction

Constant Int Value
L298N::FORWARD 0
L298N::BACKWARD 1
L298N::STOP -1

L298NX2 Methods

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

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.

Donations

Coffees are accepted! :coffee: PayPal Donations Here

paypal

About

Arduino library for L298N motor driver module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%