Esp32 reseting when running servo

hello, so im making a robosoccer car controlled by esp32 and ps4 controller

/*

    
      
      
      
      
      
      
      
          front
      m1 |    |m2  
         |    |
      m4 |    |m3
      
*/


#include <PS4Controller.h>
#include <L298N.h>
#include <ESP32Servo.h>
int servoPin = 32;
Servo myservo;
const unsigned int ENA = 19;
const unsigned int IN1 = 18;
const unsigned int IN2 = 5;

const unsigned int IN3 = 23;
const unsigned int IN4 = 22;
const unsigned int ENB = 15;

const unsigned int ENC = 12;
const unsigned int IN5 = 14;
const unsigned int IN6 = 27;

const unsigned int ENE = 33;
const unsigned int IN7 = 26;
const unsigned int IN8 = 25;

L298N motor1(ENA, IN1, IN2);
L298N motor2(ENB, IN3, IN4);
L298N motor3(ENC, IN5, IN6);
L298N motor4(ENE, IN7, IN8);

void setup() {
  
  Serial.begin(115200);
myservo.attach(servoPin);
  PS4.begin();
  
}

void loop() {
  int xAxis = PS4.RStickX();
  int yAxis = PS4.LStickY();
  int Rotate = PS4.LStickX();

  if (Rotate < -100) {
   motor1.setSpeed(190);
   motor2.setSpeed(190);
    motor3.setSpeed(190);
    motor4.setSpeed(190);
   motor1.backward();
    motor2.backward();
    motor3.backward();
    motor4.backward();
    Serial.println("ROTATE CCW 50%");
  }
  else if (Rotate > 100) {
 motor1.setSpeed(190);
   motor2.setSpeed(190);
    motor3.setSpeed(190);
    motor4.setSpeed(190);
    motor1.forward();
    motor2.forward();
   motor3.forward();
    motor4.forward();
    Serial.println("ROTATE CW 50%");
  }

  else if (yAxis > 100 && PS4.R2()) {
    motor1.setSpeed(255);
    motor2.setSpeed(255);
    motor3.setSpeed(255);
    motor4.setSpeed(255);
    motor1.forward();
    motor2.backward();
    motor3.backward();
    motor4.forward();
    Serial.println("Forward at 100%");
  }
  else if (yAxis > 100) {
    motor1.setSpeed(190);
    motor2.setSpeed(190);
    motor3.setSpeed(190);
    motor4.setSpeed(190);
    motor1.forward();
    motor2.backward();
    motor3.backward();
    motor4.forward();
    Serial.println("Forward at 75%");
  }
  else if (yAxis < -100 && PS4.R2()) {
    motor1.setSpeed(255);
    motor2.setSpeed(255);
    motor3.setSpeed(255);
    motor4.setSpeed(255);
   motor1.backward();
    motor2.forward();
    motor3.forward();
    motor4.backward();
    Serial.println("Backward at 100%");
  }
  else if (yAxis < -100) {
    motor1.setSpeed(190);
    motor2.setSpeed(190);
    motor3.setSpeed(190);
    motor4.setSpeed(190);
    motor1.backward();
    motor2.forward();
    motor3.forward();
    motor4.backward();
    Serial.println("Backward at 75%");
  }
  else if (xAxis > 100 && PS4.R2()) {
    motor1.setSpeed(255);
    motor2.setSpeed(255);
    motor3.setSpeed(255);
    motor4.setSpeed(255);
    motor1.forward();
    motor2.forward();
    motor3.backward();
    motor4.backward();
    Serial.println("Right sideway at 100%");
  }
  else if (xAxis > 100) {
    motor1.setSpeed(190);
    motor2.setSpeed(190);
    motor3.setSpeed(190);
    motor4.setSpeed(190);
    motor1.forward();
    motor2.forward();
    motor3.backward();
    motor4.backward();
    Serial.println("Right sideway at 75%");
  }
  else if (xAxis < -100 && PS4.R2()) {
   motor1.setSpeed(255);
    motor2.setSpeed(255);
    motor3.setSpeed(255);
    motor4.setSpeed(255);
    motor1.backward();
    motor2.backward();
    motor3.forward();
    motor4.forward();
    Serial.println("Left sideway at 100%");
  }
  else if (xAxis < -100) {
   motor1.setSpeed(190);
    motor2.setSpeed(190);
    motor3.setSpeed(190);
    motor4.setSpeed(190);
   motor1.backward();
    motor2.backward();
    motor3.forward();
    motor4.forward();
    Serial.println("Left sideway at 75%");
  }
  else if (PS4.R1()){
   myservo.write(60);
   Serial.println("ball trapped");
   delay(500);
  }
  else if (PS4.L1()) {
   myservo.write(90);
   Serial.println("ball Released");
   delay(500);
  }
  else {
    motor1.setSpeed(0);
    motor2.setSpeed(0);
    motor3.setSpeed(0);
    motor4.setSpeed(0);
    motor1.stop();
    motor2.stop();
    motor3.stop();
    motor4.stop();
    Serial.println("Stop");
  }

  delay(100);
}

so when I press R1 to trap ball it moves the servo and resets the esp32

How is the servo powered ?

i powred it using 3.3v esp32

Servos usually run at 5V and, in any case, the ESP board cannot supply enough current to run a servo, hence the reset

You need to power the servo with an external power supply that can provide sufficient current for the servo

i tried using 5 from VIN pin of esp32 but is 5v and more than 1200mah and the servo is not working

Try running one of the Servo example sketches on the board

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald

 modified for the ESP32 on March 2017
 by John Bennett

 see http://www.arduino.cc/en/Tutorial/Sweep for a description of the original code

 * Different servos require different pulse widths to vary servo angle, but the range is 
 * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
 * sweep 180 degrees, so the lowest number in the published range for a particular servo
 * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
 * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
 * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
 * degrees.
 * 
 * Circuit: (using an ESP32 Thing from Sparkfun)
 * Servo motors have three wires: power, ground, and signal. The power wire is typically red,
 * the ground wire is typically black or brown, and the signal wire is typically yellow,
 * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
 * considerable power, we will connect servo power to the VBat pin of the ESP32 (located
 * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. 
 * 
 * We could also connect servo power to a separate external
 * power source (as long as we connect all of the grounds (ESP32, servo, and external power).
 * In this example, we just connect ESP32 ground to servo ground. The servo signal pins
 * connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.
 * 
 * In this example, we assume a Tower Pro MG995 large servo connected to an external power source.
 * The published min and max for this servo is 1000 and 2000, respectively, so the defaults are fine.
 * These values actually drive the servos a little past 0 and 180, so
 * if you are particular, adjust the min and max values to match your needs.
 */

#include <ESP32Servo.h>

Servo myservo;  // create servo object to control a servo
// 16 servo objects can be created on the ESP32

int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 
// Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42
// Possible PWM GPIO pins on the ESP32-S3: 0(used by on-board button),1-21,35-45,47,48(used by on-board LED)
// Possible PWM GPIO pins on the ESP32-C3: 0(used by on-board button),1-7,8(used by on-board LED),9-10,18-21
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
int servoPin = 17;
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
int servoPin = 7;
#else
int servoPin = 32;
#endif

void setup() {
	// Allow allocation of all timers
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
	myservo.setPeriodHertz(50);    // standard 50 hz servo
	myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
	// using default min/max of 1000us and 2000us
	// different servos may require different min/max settings
	// for an accurate 0 to 180 sweep
}

void loop() {

	for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
		// in steps of 1 degree
		myservo.write(pos);    // tell servo to go to position in variable 'pos'
		delay(15);             // waits 15ms for the servo to reach the position
	}
	for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
		myservo.write(pos);    // tell servo to go to position in variable 'pos'
		delay(15);             // waits 15ms for the servo to reach the position
	}
}


I tried this example code and it is working fine

Does the board reset if you comment out the Servo.write() for the R1 detection ?

no, it does not reset I removed the delay and when I press L1 to release the ball the motor comes at one spot and stops but when I press R1 to trap the ball it keeps rotating 360 degrees

edit-it and now it resets sometimes and sometimes it doesn't

Apart from any other problem. if the servo keeps rotating 360 degrees then you have a not got a servo at all, you have an electronically controlled motor. Such a motor will not allow you to set its position, only its speed and direction

Such a motor will also not do what is intended when you run the Sweep example. When you ran it, what did you observe the "servo" doing ?

Hi, @malharja123

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.. :smiley: :+1: :coffee: :australia:

Is this an SG90 servo?


This is the circuit diagram, Sorry in the Battery section I accidentally write 18560 instead of 18650

components used-
1)l298n Motor driver x 2
2)Mecanum Wheels x 4
3)esp32 x 1
4)18650 3.7v 1200mah x 1
5)18650 3.7v 2500mah x 2
4)12v Gear Motor 500rpm x 4

These are my motors-
https://robu.in/product/500rpm-12v-low-noise-dc-motor-with-metal-gears-grade-a/?gad_source=1&gclid=Cj0KCQiA_qG5BhDTARIsAA0UHSL12n1OStm3ghaxiTP1IMFaxfGUQP3lrxt9ILoeZ0Mi0pLBYu8fWf8aAnlfEALw_wcB

pin-

ENA = 19;
IN1 = 18;
IN2 = 5;
IN3 = 23;
IN4 = 22;
ENB = 15;
ENC = 12;
IN5 = 14;
IN6 = 27;
ENE = 33;
IN7 = 26;
IN8 = 25;

motor configuration-

      front
  m1 |    |m2  
     |    |
  m4 |    |m3

And yes I'm using sg90 pro servo motor
https://robu.in/product/towerpro-sg90-9g-mini-servo-9-gram/

how can i upload videos here

There are many copies and fakes of the SG90. Many people on the forum have reported receiving continuous rotating servos when they ordered 180 degree.

I think you need a new servo.

Hi, @malharja123
Thanks for the diagram, but it doesn't tell us pin numbers etc.

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.

You will find it easier to draw and annotate.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

the pin diagram is given in the discription but ill try to draw

Thank you, pen(cil) and paper would be clearer.

Tom.. :smiley: :+1: :coffee: :australia:

That's fine but you need a new servo

sure sir ill try with different servo