Hello People, My problem with l298n is only controlling the direction for 1 motor only and motor2 is going only CW using esp8266. Even tho it says in the serial monitor its changing the direction but its actually not. Please check the connections and the programing and if you found how to fix the problem please tell me.
everything is inside here: Download - 0 MB
Please post your code and schematic here to avoid teh need to visit another site
Hi, @memind
Welcome to the forum.
Thanks.. Tom....
Hello!
schematic:
code:
robojax_speed_start_stop_control_page.h (2.0 KB)
S20-08-ESP8266-DC_motor_L298N_full_control.ino (4.5 KB)
dc motor library: (here is the code i couldnt send it): Robojax_L298N_DC_motor.h
/**
* File: Robojax_L298N_DC_motor.h
Written by Ahmad Shamshiri (Robojax.com) on Dec 24, 2019
* Description: This libray allows you to control 1 or 2 DC motors using L298N Dual H-bridge motor driver module
* Author: Ahmad Shamshiri
* Contact: robojax.com
* Copyright: 2019 Ahmad Shamshiri
* Version: 1.00 Dec 24, 2019
Robojax_L298N_DC_motor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Robojax_L298N_DC_motor_H
#define Robojax_L298N_DC_motor_H
#include "Arduino.h"
class Robojax_L298N_DC_motor
{
public:
//Arduino board single motor
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA);
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA, boolean debug);
//ESP32 single motor
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA, int CHA);
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA, int CHA, boolean debug);
//Arduino dual motor- ESP
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA,int IN3,int IN4,int ENB);
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA,int IN3,int IN4,int ENB, boolean debug);
//ESP32 dual motor
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA, int CHA, int IN3, int IN4,int ENB, int CHB);
Robojax_L298N_DC_motor(int IN1,int IN2,int ENA, int CHA, int IN3, int IN4,int ENB, int CHB, boolean debug);
/**
* control motor
*/
void rotate(int motor, int value, int dir);
/**
* Initializes the L298D motor driver
*/
void begin();
/*
* brake the motor
*/
void brake(int motor);
/*
* single motor demo
*/
void demo(int motor);
private:
/*
* debug print
*/
void printDebug1(int motor, int p1, int v1, int p2, int v2, int dir, int speed);
void printDebug2(int motor1, int p1, int v1, int p2, int v2, int dir1, int speed1, int motor2, int p3, int v3, int p4, int v4, int dir2, int speed2) ;
void printDebug3(int motor, int p1, int v1, int p2, int v2, int dir, int speed, int duty);
void printDebug4(int motor1, int p1, int v1, int p2, int v2, int dir1, int speed1, int duty1, int motor2, int p3, int v3, int p4, int v4, int dir2, int speed2, int duty2) ;
int _in1, _in2, _enA, _in3, _in4, _enB, _pwmBit, _chA, _chB;
boolean _debug = false;
boolean _dual;
int _resulotion =12;
int _frequency = 5000;
int _bits = pow(2,_resulotion);
int toPWM(int v);
};
#endif
Hi, @memind
Sorry but you need to post a proper schematic.
Posting an image of a hand drawn circuit would be preferable.
Please include power supplies, component names and pin labels.
Thanks.. Tom..
Your "schematic" is hard to read.
The voltage connected to Vin must be a minimum of 7V.
Please post the code that you are having trouble with, not the library that you are using.
What is the rated voltage of your motors? The L298 will drop 2V to over 4V of the motor supply voltage so the motors are seeing, at best, about 2.8V. TheL298 is ancient and inefficient technology. Modern drivers are much more efficient. Pololu has a good line of DC motor drivers.
here is a better schematic:
I couldn't make any better schematic.
The 2nd motor is going only CW. The code am having trouble with:
#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 D6
#define IN2 D7
#define ENA D8
#define IN3 D2
#define IN4 D3
#define ENB D4// this pin must be PWM enabled pin
const int CCW = 2; // do not change
const int CW = 1; // do not change
int motorDirection = CW;
int motorDirection2 = CW;// defaule direction
#define motor1 1
#define motor2 2// do not change
// use the line below for single motor
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN3, IN4, ENB, true);
String accessName ="robot";
const int changeStep = 5;// 10 is 10% every time button is pushed
int outPutValue = 40;// variable holding the light output vlaue (initial value) 40 means 40%
const int motorMinimumSpeed=40;
int motor1StopState=HIGH;
int motor2StopState=HIGH;//Stope state of motor HIGH=STOP
#include "robojax_speed_start_stop_control_page.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "My wifi ssid"
#define STAPSK "my wifi password"
#endif
const char *ssid = STASSID;
const char *password = STAPSK;
ESP8266WebServer server(80);
void handleRoot() {
String HTML_page = speed_control_page_part1;
HTML_page.concat(outPutValue);
HTML_page.concat(speed_control_page_part2);
HTML_page.concat(outPutValue);
HTML_page.concat(speed_control_page_part3);
server.send(200, "text/html", HTML_page);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
Serial.begin(115200);
motor.begin();
//L298N DC Motor by Robojax.com
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin(accessName)) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/speed", HTTP_GET, handleMotorSpeed);
server.on("/stop", HTTP_GET, handleMotorBrake);
server.on("/direction", HTTP_GET, handleMotorDirection);
server.on("/right", HTTP_GET, handleMotorRight);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
if(motor1StopState, motor2StopState ==HIGH)
{
motor.brake(motor1), motor.brake(motor2);
}else{
motor.rotate(motor1, outPutValue, motorDirection);//run motor1 at 60% speed in CW direction
motor.rotate(motor2, outPutValue, motorDirection2);//run motor1 at 60% speed in CW direction
}
delay(100);
}
/*
* handleMotorSpeed()
* Slows down or speeds up the motor
* returns nothing
* written by Ahmad Shamshiri
* on Nov 03, 2019 in Ajax, Ontario, Canada
* www.robojax.com
*/
void handleMotorSpeed() {
if(server.arg("do") == "slower")
{
outPutValue -=changeStep;
if(outPutValue < motorMinimumSpeed)
{
outPutValue = motorMinimumSpeed;
}
}else{
outPutValue +=changeStep;
if(outPutValue > 100)
{
outPutValue =100;
}
}
handleRoot();
}//
/*
* handleMotorBrake()
* applies brake to the motor
* returns nothing
* Written by Ahmad Shamshiri on Nov 02, 2019
* www.Robojax.com
*/
void handleMotorBrake() {
if(server.arg("do") == "START")
{
motor1StopState=LOW, motor2StopState=LOW;
}else{
motor1StopState=HIGH, motor2StopState=HIGH;
}
handleRoot();
}//
void handleMotorDirection() {
if(server.arg("dir") == "CW")
{
motorDirection =CW;
motorDirection2 =CW;
}else{
motorDirection =CCW;
motorDirection2 =CCW;
}
handleRoot();
}//
void handleMotorRight() {
if(server.arg("move") == "right")
{
motorDirection =CW;
motorDirection2 =CCW;
}else{
motorDirection =CCW;
motorDirection2 =CW;
}
handleRoot();
}//
am using a 3v mini motor and also I really cant buy pololu motor drivers so yea, How do I find the rated voltage?
Here is what my 2 motors look like:
Hello, Check the reply I sent to @groundFungus So I will avoid spamming
Hi, just two hints...
You didnt comment out the only one motor option...look at the code
I sometimes have problems with certain pins....D4 is one of them...just try another pin...it may help.
Did you write this code in stages?
If so, have you got a simple code that JUST tests your motors, NOTHING else in the code?
This is important as you need to pinpoint your problem and first lets make sure it is not hardware.
Have you used the example code that comes with the library?
Thanks.. Tom..
This problem was solved. how do i close a topic?. Thanks.
Don't close it
Instead, click on the "Solution" icon under the post that provided the solution
There isn't provided solution, I found the problem later in an other post.
Can I suggest that you explain the solution here or if appropriate post a link to the other topic
What did YOU do, please explain rather than get us to go and look through another thread.
Was is as simple as D2 not being connected or what?
A simple hand drawn schematic would also help.
Forget using a colourful drawing app.
Thanks.. Tom..
It looks like pin D2 for me wasn't turning on. Pin D2 Is used to make the l298n driver tell the motor to go CCW. So I just used another library and everything seemed to work fine. note: pin D2 was working. the library just didn't work correctly. :)))
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.