I'm doing a wifi RC car project, have an L298N connected to my DOIT ESP32 DEVKIT V1 board that controls 4 6V motors, each 2 motors are connected to the same side, I control it using a webserver, sometimes when I drive in any direction it works for a second then the esp32 resets, sometimes it works fine if I disconnected one side of the motors, also it sometimes works fine on its own, I'm using 3 18650 batteries to run the motors and the motor driver, I'm using a separate 1 18650 battery connected to a power bank module to step up the voltage to 5 to run the esp32, it also doesn't work well if I'm powering the esp32 from my laptop, what am I doing wrong?
Well how about first you provide is with a full schematic.
You should never post in the ESP32 section unless you have a real Arduino brand ESP32 product. For all other versions post in a more relevant category.
Therefore I have moved your post here.
You might want to look at this How to get the best out of this forum before you proceed any further.
As to your problem this is the typical behavior of any processor to motors. The solution involves better supply decoupling.
See this:-
As well as cutting down the emissions from the motors. For example do they have flyback diodes on them?
We only know what you tell us, and without knowing what you have, we don't stand a chance.
This is the diagram I used to wire my components except that I added 2 motors which take the same signal, also I'm not using the L298N 5V to power the esp, the power bank is feeding it from its USB port.
and here's also the code running it
// An RC car via a simple webserver
#include <WiFi.h>
#include <WebServer.h>
//
#define ENA 21 // The ESP32 pin GPIO14 connected to the ENA pin L298N
#define IN1 16 // The ESP32 pin GPIO27 connected to the IN1 pin L298N
#define IN2 17 // The ESP32 pin GPIO26 connected to the IN2 pin L298N
#define IN3 32 // The ESP32 pin GPIO25 connected to the IN3 pin L298N
#define IN4 33 // The ESP32 pin GPIO33 connected to the IN4 pin L298N
#define ENB 22 // The ESP32 pin GPIO32 connected to the ENB pin L298N
// Access point credentials
const char* ssid = "AndroidAPF828";
const char* password = "11111111";
WebServer server(80);
// HTML Content for the web page
// HTML Content for the web page
const char* htmlPage = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arrow Joystick</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.joystick {
display: grid;
grid-template-areas:
"left-backward backward right-backward"
"left stop right"
"left-forward forward right-forward";
gap: 5px;
}
.button {
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
background-color: #000000;
color: white;
text-decoration: none;
border-radius: 10px;
font-size: 24px;
font-weight: bold;
border: none;
cursor: pointer;
}
.button:hover {
background-color: #0056b3;
}
.stop-button {
grid-area: stop;
}
.left {
grid-area: left;
}
.left-forward {
grid-area: left-forward;
}
.forward {
grid-area: forward;
}
.right-forward {
grid-area: right-forward;
}
.right {
grid-area: right;
}
.right-backward {
grid-area: right-backward;
}
.backward {
grid-area: backward;
}
.left-backward {
grid-area: left-backward;
}
</style>
<script>
function sendCommand(command) {
var xhr = new XMLHttpRequest();
xhr.open("GET", command, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log("Command sent: " + command);
}
};
xhr.send();
}
</script>
</head>
<body>
<div class="joystick">
<button class="button left-backward" onclick="sendCommand('/leftForward')">↖</button>
<button class="button backward" onclick="sendCommand('/forward')">↑</button>
<button class="button right-backward" onclick="sendCommand('/rightForward')">↗</button>
<button class="button left" onclick="sendCommand('/left')">←</button>
<button class="button stop-button" onclick="sendCommand('/stop')">Stop</button>
<button class="button right" onclick="sendCommand('/right')">→</button>
<button class="button left-forward" onclick="sendCommand('/leftBackward')">↙</button>
<button class="button forward" onclick="sendCommand('/backward')">↓</button>
<button class="button right-forward" onclick="sendCommand('/rightBackward')">↘</button>
</div>
</body>
</html>
)rawliteral";
void handleRoot();
void forward();
void backward();
void right();
void left();
void rightForward();
void leftForward();
void rightBackward();
void leftBackward();
void stopCar();
void setup() {
Serial.begin(115200);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// // Set ESP32 as AP
// WiFi.softAP(ssid, password);
// IPAddress IP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(IP);
// // Connect to WiFi network with fixed IP
// IPAddress ip(192, 168, 1, 99); // Desired fixed IP address
// IPAddress gateway(192, 168, 1, 1); // Your gateway IP address
// IPAddress subnet(255, 255, 255, 0); // Subnet mask
//
// WiFi.config(ip, gateway, subnet);
//
// Connect to WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print ESP32 IP address
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());
// Define routes
server.on("/", handleRoot);
server.on("/forward", forward);
server.on("/backward", backward);
server.on("/right", right);
server.on("/left", left);
server.on("/rightForward", rightForward);
server.on("/leftForward", leftForward);
server.on("/rightBackward", rightBackward);
server.on("/leftBackward", leftBackward);
server.on("/stop", stopCar);
server.begin(); // Starts the server
Serial.println("HTTP server started.");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
// esp_task_wdt_reset(); // Feed the watchdog timer
}
void handleRoot(){
server.send(200, "text/html", htmlPage);
}
void forward(){
int speed=225;
Serial.println("Forward");
// Add code to move the car forward
digitalWrite(IN2, HIGH);
digitalWrite(IN1, LOW);
digitalWrite(IN4, HIGH);
digitalWrite(IN3, LOW);
analogWrite(ENA,speed);
analogWrite(ENB,speed);
server.send(200, "text/plain", "Moving Forward");
}
void backward(){
int speed=225;
Serial.println("backward");
// Add code to move the car backward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA,speed);
analogWrite(ENB,speed);
server.send(200, "text/plain", "Moving backward");
}
void right(){
int speed=225;
Serial.println("right");
//forward movement
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
//reverse movement
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA,speed);
analogWrite(ENB,speed);
// Add code to move the car right
server.send(200, "text/plain", "Moving right");
}
void left(){
int speed=225;
Serial.println("left");
// Add code to move the car left
//backward movement
digitalWrite(IN2, LOW);
digitalWrite(IN1, HIGH);
//forward movement
digitalWrite(IN4, HIGH);
digitalWrite(IN3, LOW);
analogWrite(ENA,speed);
analogWrite(ENB,speed);
server.send(200, "text/plain", "Moving left");
}
void rightForward(){
int speed=225;
Serial.println("Right Forward");
// Add code to move the car Right Forward
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
analogWrite(ENA,speed);
analogWrite(ENB,(speed/2));
server.send(200, "text/plain", "Moving Right Forward");
}
void leftForward(){
int speed=225;
Serial.println("Left Forward");
// Add code to move the car Left Forward
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
analogWrite(ENA,(speed/2));
analogWrite(ENB,speed);
server.send(200, "text/plain", "Moving Left Forward");
}
void rightBackward(){
int speed=225;
Serial.println("Right Backward");
// Add code to move the car Right Backward
digitalWrite(IN2,LOW);
digitalWrite(IN1,HIGH);
digitalWrite(IN4,LOW);
digitalWrite(IN3,HIGH);
analogWrite(ENA,speed);
analogWrite(ENB,(speed/2));
server.send(200, "text/plain", "Moving Right Backward");
}
void leftBackward(){
int speed=225;
Serial.println("Left Backward");
// Add code to move the car Left Backward
digitalWrite(IN2,LOW);
digitalWrite(IN1,HIGH);
digitalWrite(IN4,LOW);
digitalWrite(IN3,HIGH);
analogWrite(ENA,(speed/2));
analogWrite(ENB,speed);
server.send(200, "text/plain", "Moving Left Backward");
}
void stopCar(){
Serial.println("Stop");
// Add code to move the car Left Backward
digitalWrite(IN2,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN4,LOW);
digitalWrite(IN3,LOW);
server.send(200, "text/plain", "Stop");
}
I'm new to electronics but no they don't have flyback diodes.
Two power sources require their GND to be connected all together.
Try to power everything from the same source (motor driver provides 5v) to see if it helps or not
That's good because it would never work if they did.
Hi, @gamala7med
Welcome to the forum.
Then can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Can you post some images of your project?
So we can see your component layout.
Thanks.. Tom...
![]()
You can fit fly back diodes to bi-directional motors but you require four of them for it to work correctly.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
