Hi,
I have a 11.1 v LiPo battery, L298N motor driver, two DC motors.
I found the following Arduino library for L298N. As I mentioned earlier that I have never worked with Arduino before, the code below doesn't make much sense to me.
info about motor controller:
https://www.handsontec.com/dataspecs/L298N Motor Driver.pdf
Arduino library given below:
https://www.arduinolibraries.info/libraries/l298-n
If I was working with some other compiler this is how I will proceed.
1: Set a PWM frequency for both motors, say 20 k Hz
2: Adjust the duty cycle for both motors individually in order to change speeds
Q1:
Perhaps, the library below does the same thing but how do I implement it from scratch, and do you think it'd be a good idea to implement it without using a library?
Q2:
I found a couple of other libraries for L298N in Arduino IDE as shown below; not sure if some other library is better than the one mentioned below in the code.
Q3:
In the code below, there is this line " Serial.begin(9600)". I have seen such lines in some other sample codes as well, what's the purpose of this line? Is it used to display information?
C++:
/*
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.
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);
void setup()
{
// Used to display information
Serial.begin(9600);
// Wait for Serial Monitor to be opened
while (!Serial)
{
//do nothing
}
// Set initial speed
motor.setSpeed(70);
}
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
printSomeInfo();
delay(3000);
// Stop
motor.stop();
// Alternative method:
// motor.run(L298N::STOP);
printSomeInfo();
// Change speed
motor.setSpeed(255);
delay(3000);
// Tell the motor to go back (may depend by your wiring)
motor.backward();
// Alternative method:
// motor.run(L298N::BACKWARD);
printSomeInfo();
motor.setSpeed(120);
delay(3000);
// Stop
motor.stop();
printSomeInfo();
delay(3000);
}
/*
Print some informations in Serial Monitor
*/
void printSomeInfo()
{
Serial.print("Motor is moving = ");
Serial.print(motor.isMoving());
Serial.print(" at speed = ");
Serial.println(motor.getSpeed());
}