Skip to content

Commit

Permalink
New version
Browse files Browse the repository at this point in the history
Added constructors for two pin configuration.
Added new flexible methods for callback usage.
Added new costant STOP.
Added class to initialize both motors at once.
Rewrited old examples.
Writed new examples.
Redrawed wiring schemas.
Created new folder for schemas.
  • Loading branch information
AndreaLombardo committed Apr 23, 2020
1 parent 5ea93e9 commit 7e7ce8c
Show file tree
Hide file tree
Showing 23 changed files with 2,153 additions and 163 deletions.
581 changes: 581 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

82 changes: 74 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# L298N Library
A easy to use L298N library to control DC Motors with Arduino.
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).
~~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*
Expand All @@ -11,18 +16,46 @@ Download this repository as a .zip file and from the Arduino IDE go to *Sketch -
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

## Methods
### Same thing for L298NX2 variant
```
// With Enable pin to control speed
motor A motor B
|------------------||------------------|
L298N myMotors(EN_A, IN1_A, IN2_A, EN_B, IN1_B, IN2_B);
```
```
// Without Enable pin and jumper in place
motor A motor B
|------------||------------|
L298N 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.
Expand All @@ -33,15 +66,48 @@ L298N myMotor(EN, IN1, IN2);
|**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 *L298N::FORWARD* or *L298N::BACKWARD*.
|**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 forwardFor and backwardFor methods.
|**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](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GRNEA99RCC3U4) :coffee:

[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GRNEA99RCC3U4)

*writing examples stay tuned*
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GRNEA99RCC3U4)
76 changes: 56 additions & 20 deletions examples/L298N-Callback/L298N-Callback.ino
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
/*
Author : Andrea Lombardo
Site : https://www.lombardoandrea.com
Source : https://github.com/AndreaLombardo/L298N/
Here you can see how to work with callback option.
Every five seconds callback function is called and encreasing the speed.
When maximum speed is reached then starts again from zero.
In the meaning time your loop cicle is never bloked.
Speed range go from 0 to 255, default is 100.
Use setSpeed(speed) to change.
Sometimes at lower speed motors seems not running.
It's normal, may depends by motor and power supply.
Wiring schema in file "L298N - Schema_with_EN_pin.png"
*/

// Include the library
#include <L298N.h>

//pin definition
#define EN 9
#define IN1 8
#define IN2 7
// Pin definition
const unsigned int IN1 = 7;
const unsigned int IN2 = 8;
const unsigned int EN = 9;

//create a motor instance
// Create one motor instance
L298N motor(EN, IN1, IN2);

//initial speed
// Initial speed
unsigned short theSpeed = 0;

void setup() {

//set the initial speed
void setup()
{
// Set initial speed
motor.setSpeed(theSpeed);

}

void loop() {

//move motor for 5 seconds and then execute the callback function
void loop()
{
// Move motor for 5 seconds and then execute the callback function
// Easy way
motor.forwardFor(5000, callback);

// More flexible way
// motor.runFor(5000, L298N::FORWARD, callback);
}

void callback() {
/*
Each time the callback function is called increase the speed of the motor or reset to 0.
This function can be named with any name.
*/
void callback()
{

//each time the callback function is called increase the speed of the motor or reset to 0
if (theSpeed <= 255) {
// If speed is over 255 then encrease
if (theSpeed <= 255)
{
theSpeed += 25;
} else {
}
else
{
theSpeed = 0;
}

//re-enable motor movements
/*
Each time a callback function is called
the motor is placed in "don't move" status
To restore it's capability use reset() method.
*/
motor.reset();
//set the new speed
motor.setSpeed(theSpeed);

// Set the new speed
motor.setSpeed(theSpeed);
}
Binary file removed examples/L298N-Callback/Schema_bb.png
Binary file not shown.
79 changes: 79 additions & 0 deletions examples/L298N-Fade/L298N-Fade.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Author : Andrea Lombardo
Site : https://www.lombardoandrea.com
Source : https://github.com/AndreaLombardo/L298N/
Based on Arduino Basic Fade example.
Speed range go from 0 to 255, default is 100.
Use setSpeed(speed) to change.
Sometimes at lower speed motors seems not running.
It's normal, may depends by motor and power supply.
Wiring schema in file "L298N - Schema_with_EN_pin.png"
*/

// Include the library
#include <L298N.h>

// Pin definition
const unsigned int IN1 = 7;
const unsigned int IN2 = 8;
const unsigned int EN = 9;

// Create one motor instance
L298N motor(EN, IN1, IN2);

int speedness = 0;
int speedAmount = 1;

void setup()
{
// Used to display information
Serial.begin(9600);

// Wait for Serial Monitor to be opened
while (!Serial)
{
//do nothing
}
}

void loop()
{
// Apply faded speed to both motors
motor.setSpeed(speedness);

// Tell motor A to go forward (may depend by your wiring)
motor.forward();

// Alternative method:
// motor.run(L298N::FORWARD);

//print the motor satus in the serial monitor
printSomeInfo();

// Change the "speedness" for next time through the loop:
speedness = speedness + speedAmount;

// Reverse the direction of the fading at the ends of the fade:
if (speedness <= 0 || speedness >= 255)
{
speedAmount = -speedAmount;
}

// Wait for 30 milliseconds to see the dimming effect
delay(30);
}

/*
Print some informations in Serial Monitor
*/
void printSomeInfo()
{
Serial.print("Motor is moving = ");
Serial.print(motor.isMoving() ? "YES" : "NO");
Serial.print(" at speed = ");
Serial.println(motor.getSpeed());
}
81 changes: 81 additions & 0 deletions examples/L298N-No-Enable/L298N-No-Enable.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Author : Andrea Lombardo
Site : https://www.lombardoandrea.com
Source : https://github.com/AndreaLombardo/L298N/
Here you can see how to work in a common configuration without the needed of Enable pin.
Make sure your module has a jumper in place.
When L298N has a jumper instead of Enable pin, the speed is always 255.
Wiring schema in file "L298N - Schema_without_EN_pin.png"
*/

// Include the library
#include <L298N.h>

// Pin definition
const unsigned int IN1 = 7;
const unsigned int IN2 = 8;

// Create one motor instance
L298N motor(IN1, IN2);

void setup()
{
// Used to display information
Serial.begin(9600);

// Wait for Serial Monitor to be opened
while (!Serial)
{
//do nothing
}
}

void loop()
{

// Tell the motor to go forward (may depend by your wiring)
motor.forward();

// Alternative method:
// motor.run(L298N::FORWARD);

//print the motor satus in the serial monitor
Serial.print("Is moving = ");
Serial.println(motor.isMoving());

delay(3000);

// Stop
motor.stop();

// Alternative method:
// motor.run(L298N::STOP);

Serial.print("Is moving = ");
Serial.println(motor.isMoving());

delay(3000);

// Tell the motor to go back (may depend by your wiring)
motor.backward();

// Alternative method:
// motor.run(L298N::BACKWARD);

Serial.print("Is moving = ");
Serial.println(motor.isMoving());

delay(3000);

// Stop
motor.stop();

Serial.print("Is moving = ");
Serial.println(motor.isMoving());

delay(3000);
}
Loading

0 comments on commit 7e7ce8c

Please sign in to comment.