Some unknow clone of an old esp32 devkit.
Do you have the 9V connected to the barrel connector?
If yes, that is OK but it won't have enough current to run a servo.
You should not use ledcAttachChannel or any ledc function if you use analogWrite.
Don't use pin 12 since it may affect booting.
both usb to pc and 9v the servo moves
the dc motor wont
i think i followed al of your instructions besides the last one,which i couldn't find
this the message in the serial monitor

but no movement still
...
What motors and motor driver are you using?
SG90 servo only needs 5V, you will burn it out with 9V.
the esp32 is supplied with 9v
its my understanding that even with 9v supply, the esp32 will output only 5v.
Is the power supply 1x9vdc PP3 or 6x1.5vdc AA or other?
4x1.5V AA
4 x 1.5 = 6... but the motor driver board will use 1.5vdc just to operate leaving 4.5vdc for motors. Maybe try only one motor... or add one AA...
sorry i confused you
its 4*6v AA
i have mentioned that in simple test code they move
its only in the "complete " configuration with servo and HC-sr04 that it wont
AA = 1.5vdc
4*1.5 = 6vdc
4*1.5 = 6vdc
that one
6vdc power supply MINUS 1.5 vdc for motor driver board = 4.5vdc. Remove one motor or add one AA battery and try again.
This sketch is working for me ( used an sr04 and checked motor pins are set/reset as expected, not connected any servo )
NOTE
- As your motor driver has an +5V out connect this to the +5V of the servo ( remove the connection to the esp32 +5V )
- Also all gnd should be connected togheter!!!!!!
Connect the motor controller gnd to the esp gnd too!!!!!! - Also esp32 i/o are not '5V tolerant' you have to use two resistors to reduce the 'echo signal' from sr04 from 5V to 3.3V ( roughly )
- do any wiring modification only with power off ( both power off )
//
// Obstacle avoidance car ( OAC )
//
#include <ESP32Servo.h>
Servo servo; // create servo object to control a servo
//mine
//#define trigger_pin 23
//#define echo_pin 35
// orig
#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin
// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;
//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;
// Setting PWM properties
//const int frequency = 3000;
//const int pwm_channel = 0;
//const int resolution = 8;
void setup() {
servo.attach(SERVO_PIN); // attaches the servo on pin 12 to the servo objectư
pinMode(trigger_pin,OUTPUT); // attaches the trigger pin to 5 pin
pinMode(echo_pin,INPUT); // attaches echo pin to 18 pin
//modding motor A
pinMode(enable1Pin, OUTPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
//modding Motor B
pinMode(enable2Pin, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);
// initaite serial monitor
Serial.begin(115200);
}
void loop() {
//default moving forward
MoveForward();
delay(200);
//centering servo
servo.write(90);
Serial.println("servo centering");
delay(200);
int CurrentDistance = DistanceMeasure();
if(CurrentDistance<20){
int CalculatedDistanceLeft = 0; //setting var for left and right distances
int CalculatedDistanceRight = 0;
delay(200);
//car will stop
StopCar();
delay(200);
//car backwards
MoveBackward();
delay(200);
//left distance calc
CalculatedDistanceLeft = ServoLookLeft();
delay(200);
//right distance calc
CalculatedDistanceRight = ServoLookRight(); //right distance calc
delay(200);
//calcauting shortest distance
if(CalculatedDistanceRight <= CalculatedDistanceLeft)
{TurnLeft();}
else
{TurnRight();}
}
}
//servo seek clear path funcs
//getting distance from hc-sr04
//sro4 calc distance
int DistanceMeasure(){
long t; //var time to calculate distance
float distance; //var to store diatnce
//setting hc-sro4
digitalWrite(trigger_pin,LOW);
delayMicroseconds(5);
digitalWrite(trigger_pin,HIGH);
delayMicroseconds(100);
digitalWrite(trigger_pin,LOW);
//time to singanl get back
t = pulseIn(echo_pin, HIGH);
//calculation of distance
distance = (t*0.034)/2;
Serial.print("Distance (cm): ");
Serial.print(distance);
Serial.print(" Duration (us): ");
Serial.println(t);
return distance;
}
//servo turn left
int ServoLookLeft(){
Serial.println("ServoLookLeft");
int distanceLeft = 0;
delay(500);
servo.write(180); //lefting servo and taking value
delay(2000);
distanceLeft = DistanceMeasure();
return distanceLeft;
}
//servo turn right
int ServoLookRight(){
Serial.println("ServoLookRight");
int ditanceRight = 0;
delay(500);
servo.write(0); //lefting servo and taking value
delay(2000);
ditanceRight = DistanceMeasure();
return ditanceRight;
}
int SeekClearPath(){
Serial.println("SeekClearPath");
int ditanceRight = 0;
int ditanceLeft = 0;
ditanceRight=ServoLookRight();
ditanceLeft=ServoLookLeft();
//lcd.clear();
//centering servo back
servo.write(90);
delay(1000);
}
//car movement functions
//move forward
void MoveForward(){
Serial.println("MoveForward");
//Set speed motor A and B
analogWrite(enable1Pin,255);
analogWrite(enable2Pin,255);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
//lcd.clear();
}
//move backward
void MoveBackward(){
Serial.println("MoveBackward");
analogWrite(enable1Pin,255);
analogWrite(enable2Pin,255);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, HIGH);
//lcd.clear();
delay(700);
}
//turn car left
void TurnLeft(){
Serial.println("TurnLeft");
//Set speed motor A and B
analogWrite(enable1Pin,255);
analogWrite(enable2Pin,255);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
//lcd.clear();
delay(850);
//Stop motor for half second
StopCar();
}
//turn car right
void TurnRight(){
Serial.println("TurnRight");
//turn right
analogWrite(enable1Pin,255);
analogWrite(enable2Pin,255);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, HIGH);
//lcd.clear();
delay(850);
//Stop motor for half second
StopCar();
}
// stop car
void StopCar(){
Serial.println("StopCar");
analogWrite(enable1Pin,0);
analogWrite(enable2Pin,0);
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3,LOW);
digitalWrite(motor2Pin4, LOW);
delay(700);
}
This is an extract of the debug log, when there is no near obstacle an when there is an obstacle:
21:36:37.549 -> Distance (cm): 158.01 Duratione (us): 9295
21:36:37.549 -> MoveForward
21:36:37.753 -> servo centering
21:36:37.956 -> Distance (cm): 156.30 Duratione (us): 9194
21:36:37.956 -> MoveForward
21:36:38.159 -> servo centering
21:36:38.362 -> Distance (cm): 158.01 Duratione (us): 9295
21:36:38.362 -> MoveForward
21:36:38.564 -> servo centering
21:36:38.767 -> Distance (cm): 156.30 Duratione (us): 9194
21:36:38.767 -> MoveForward
21:36:38.969 -> servo centering
21:36:39.205 -> Distance (cm): 215.82 Duratione (us): 12695
21:36:39.205 -> MoveForward
21:36:39.405 -> servo centering
21:36:39.608 -> Distance (cm): 217.11 Duratione (us): 12771
21:36:39.608 -> MoveForward
21:36:39.813 -> servo centering
21:36:40.017 -> Distance (cm): 215.83 Duratione (us): 12696
21:36:40.017 -> MoveForward
21:36:40.220 -> servo centering
21:36:40.425 -> Distance (cm): 156.71 Duratione (us): 9218
21:36:40.425 -> MoveForward
21:36:40.631 -> servo centering
21:36:40.832 -> Distance (cm): 7.89 Duratione (us): 464
21:36:41.033 -> StopCar
21:36:41.922 -> MoveBackward
21:36:42.836 -> ServoLookLeft
21:36:45.358 -> Distance (cm): 238.60 Duratione (us): 14035
21:36:45.561 -> ServoLookRight
21:36:48.075 -> Distance (cm): 155.89 Duratione (us): 9170
21:36:48.279 -> TurnLeft
21:36:49.127 -> StopCar
21:36:49.808 -> MoveForward
21:36:50.011 -> servo centering
21:36:50.212 -> Distance (cm): 155.46 Duratione (us): 9145
21:36:50.212 -> MoveForward
21:36:50.418 -> servo centering
21:36:50.623 -> Distance (cm): 155.46 Duratione (us): 9145
21:36:50.623 -> MoveForward
21:36:50.830 -> servo centering
21:36:51.031 -> Distance (cm): 155.46 Duratione (us): 9145
21:36:51.066 -> MoveForward
21:36:51.234 -> servo centering
21:40:31.013 -> Distance (cm): 155.50 Duratione (us): 9147
21:40:31.013 -> MoveForward
21:40:31.218 -> servo centering
21:40:31.421 -> Distance (cm): 156.79 Duratione (us): 9223
21:40:31.421 -> MoveForward
21:40:31.623 -> servo centering
21:40:31.826 -> Distance (cm): 10.63 Duratione (us): 625
21:40:32.031 -> StopCar
21:40:32.939 -> MoveBackward
21:40:33.816 -> ServoLookLeft
21:40:36.320 -> Distance (cm): 9.55 Duratione (us): 562
21:40:36.525 -> ServoLookRight
21:40:39.039 -> Distance (cm): 9.15 Duratione (us): 538
21:40:39.241 -> TurnLeft
21:40:40.087 -> StopCar
21:40:40.797 -> MoveForward
21:40:40.969 -> servo centering
21:40:41.174 -> Distance (cm): 8.60 Duratione (us): 506
21:40:41.378 -> StopCar
21:40:42.296 -> MoveBackward
21:40:43.180 -> ServoLookLeft
how come it moved using the second sketch?
Read post #6
but the esp32 ,servo and sr04 powered by 9v battery,
separated from the 4*1.5 to power the L298N
Remove all devices and wires.
Connect only the power supplies, motor driver, and ONE motor.
What are your results?
