Hello, I'm controlling a two-wheeled robot with two DC motors with an ESP32 and an L298N. Its operation is that it detects 3 photoresistors and at the moment of detecting any of them the motors move. At the time of loading the program only makes me function 1 motor. The other isn't answering. The curious thing happens with the ports ENA1, ENA2 and ENB1 and ENB2. If I connect ENA1 and ENB2 only does the function motor A but if I connect ENA2 and ENB1 only does the B. If I connect ENA1 and ENB1 or ENA2 and ENB2 does nothing. As I have researched those ports only have the function of enabling or disabling the engines and controlling it by PWM but whatever it is should not be a problem. I suspect the program is the problem, someone would know what the problem might be.
As a fact, I do not know if it is relevant, the ESP32 and the LN298N have separate power supply. The ESP32 with batteries (+12V) and the ESP32 with a power bank.
This is the program in Arduino IDE:
// Librería para el ESP32
#include <WiFi.h>
// Pines de las fotorresistencias
const int leftSensorPin = 34;
const int middleSensorPin = 35;
const int rightSensorPin = 36;
// Pines del puente H L298N
const int motor1Pin1 = 2;
const int motor1Pin2 = 4;
const int motor2Pin1 = 32;
const int motor2Pin2 = 33;
// Pines de habilitación de los motores (ENA y ENB)
const int enableAPin = 25; // Puedes asignar cualquier pin disponible
const int enableBPin = 26; // Puedes asignar cualquier pin disponible
void setup() {
// Configurar los pines de los motores como salida
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
// Configurar los pines de habilitación como salida
pinMode(enableAPin, OUTPUT);
pinMode(enableBPin, OUTPUT);
// Iniciar la comunicación serial
Serial.begin(9600);
// Habilitar PWM en los pines de habilitación de los motores
ledcSetup(0, 5000, 8); // Canal 0, frecuencia 5000Hz, resolución de 8 bits
ledcSetup(1, 5000, 8); // Canal 1, frecuencia 5000Hz, resolución de 8 bits
ledcAttachPin(enableAPin, 0); // Habilitar PWM en el pin de habilitación del motor A
ledcAttachPin(enableBPin, 1); // Habilitar PWM en el pin de habilitación del motor B
}
void loop() {
// Leer los valores de las fotorresistencias
int leftSensorValue = analogRead(leftSensorPin);
int middleSensorValue = analogRead(middleSensorPin);
int rightSensorValue = analogRead(rightSensorPin);
// Mostrar los valores en el monitor serial
Serial.print("Left Sensor: ");
Serial.print(leftSensorValue);
Serial.print(" | Middle Sensor: ");
Serial.print(middleSensorValue);
Serial.print(" | Right Sensor: ");
Serial.println(rightSensorValue);
// Determinar cuál sensor tiene el valor más alto
int maxValue = max(leftSensorValue, max(middleSensorValue, rightSensorValue));
// Control de los motores según las lecturas de las fotorresistencias
if (maxValue > 300) { // Activar movimiento solo si algún sensor detecta más de 300
if (leftSensorValue == maxValue) {
// Alumbrar fotorresistencia izquierda: gira a la izquierda
digitalWrite(motor1Pin1, HIGH); // Avance
digitalWrite(motor1Pin2, LOW); // Reversa
digitalWrite(motor2Pin1, LOW); // Avance
digitalWrite(motor2Pin2, HIGH); // Reversa
// Ajustar la velocidad de los motores
ledcWrite(0, 255); // Velocidad máxima para motor 1
ledcWrite(1, 255); // Velocidad máxima para motor 2
} else if (middleSensorValue == maxValue) {
// Alumbrar fotorresistencia del medio: avanza hacia adelante
digitalWrite(motor1Pin1, HIGH); // Avance
digitalWrite(motor1Pin2, LOW); // Reversa
digitalWrite(motor2Pin1, HIGH); // Avance
digitalWrite(motor2Pin2, LOW); // Reversa
// Ajustar la velocidad de los motores
ledcWrite(0, 255); // Velocidad máxima para motor 1
ledcWrite(1, 255); // Velocidad máxima para motor 2
} else if (rightSensorValue == maxValue) {
// Alumbrar fotorresistencia derecha: gira a la derecha
digitalWrite(motor1Pin1, LOW); // Avance
digitalWrite(motor1Pin2, HIGH); // Reversa
digitalWrite(motor2Pin1, HIGH); // Avance
digitalWrite(motor2Pin2, LOW); // Reversa
// Ajustar la velocidad de los motores
ledcWrite(0, 255); // Velocidad máxima para motor 1
ledcWrite(1, 255); // Velocidad máxima para motor 2
}
} else {
// Si ninguno de los sensores detecta más de 300, detener el robot
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
}