Servos and ESP32 Constantly Resetting

Hi all, I'm a first time poster so apologies if anything in this post is off. I'm building a device that uses a servo motor to turn a door lock, however, I've been running into issues when using a version of my code that retrieves the time and date from the internet as well as allows for remote access. The device will idle fine, but when a position command is sent to the servo, it will rotate fully, then immediately reset the board. I've tried everything I can to stop it from resetting, but to no avail. I'm semi-confident its not an issue with the power consumption of the motor as everything works in another code version that doesn't have any changes except a lack of wifi connectivity. Does anyone have any thoughts as to what I could try?

Below are the functions used to move the servo. I've commented where I believe the resets are occurring:

void unlock(){
  myservo.write(unlocked);
  digitalWrite(greenLED, HIGH);
  delay(750);
//resets about here, green light does turn on before the reconnection process begins, not sure if the full delay command is executed
  myservo.write(rest);
  delay(100);
  digitalWrite(greenLED, LOW);
  status = "unlocked";
}

void lock(){
  myservo.write(locked);
  digitalWrite(greenLED, HIGH);
//resets around here, green light activates but turns off (due to reset) in less than .75s
  delay(750);
  myservo.write(rest);
  digitalWrite(redLED, HIGH);
  delay(750);
  digitalWrite(greenLED, LOW);
  delay(500);
  digitalWrite(redLED, LOW);
  status = "locked";
}

Welcome, and congratulations, most new members don't master the code tags until reminded. However, you're describing a system. The system includes hardware, so we'll need to see how it's wired; for that, a schematic would be very useful, a fritzing diagram less so. Please label all pins, power sources, modules, etc. in the schematic.

Thanks, and again, welcome!
Oh, and the problem is very likely in the code you didn't post, so complete code, please. In the IDE, press ctrl-T to format the code, then shft-ctrl-C to copy it for the forum. Paste it in a new message, don't update the first one.

For further info, please refer to

Finally, please move your post to Programming Questions, or Project Guidance, or some other section, as it's not about the IDE2.x.

Thanks, and sorry for the 'rules', we kinda live and die by them around here!

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

Thank you both for your responses. I was worried that a diagram would be needed as I am not very experienced with making them. I'll try my luck though!

Most servo problems reported on this forum result from an inadequate servo power supply. Power the servo separately, and be sure to connect the grounds. A 4xAA battery pack will work for 1 or 2 small servos like the SG90.

Example for Arduino Uno:

Identifying where in the code you think the problem is is one thing, but the full code is required. What's probably happening is your code is doing something that causes a sudden high current demand(like moving a servo, etc., as it does two lines before each of your flags), and the power isn't up to snuff, so the voltage dips, the cpu resets, and there you go. But a schematic, photo, and code would present the complete picture, so we don't have you 'chasing ghosts'.

I looked first at your setup() function, and did not see

  Serial.begin(115200);

Then I looked around all your code and did not see where you avail yourself of this basic diagnostic tool. Though primitive by some measure, liberal use of the serial monitor shoukd allow you to determine with some confidence the points at which the resets are occurring.

Next I looked at your power supply arrangements not.

I didn't notice in your schematic or pictures any obvious source of noises or other electrical interference caused by your lead dress and layout.

I tried to follow your investigation where you had divided the project into parts such that each could be tested independently, with proxy hardware and software for parts that are not being looked at closely.

The link to the wokwi simulation where everything seems to function correctly did not work for me.

Sry, it's been a long day. Rain, cold no fun.

a7

1 Like

@fkritzinger Apologies for that, @alto777 doesn't do well if he isn't sunburnt to a crisp.

You see, many of the usual helpers around here have either moved on(nothing to see), or are hanging back, waiting. Me, I'm moving on. You could have posted your full code in less than 30s. Here we are 3 hours later, with nothing to chew on. The fire is out, the wine is done, I'm off to bed. I might check in tomorrow, we'll see.

I appreciate all the advice! Sorry for the delay, I was troubleshooting a couple of methods that I thought of. Fortunately, I think I was finally able to identify the cause.

In case anyone else runs into a similar problem and they arrive at this post, I solved my issues by adding a capacitor (1000uF) in parallel with the servo's power and ground inputs and my board to ensure that the servo could weather a small brownout. I've heard that this is better done between the board and its power supply, however, my board does not contain a V_in port (that I am aware of). Additionally, I made sure not to include multiple servo commands too close together as I was worried that too small of a delay between them was causing problems.

I appreciate you taking the time to reply but I'm a little confused as to what code, schematics, or pictures you were examining? I'm no longer in need of assistance on this particular topic, but your response intrigued me.

I apologize for wasting your time. I did my best to learn schematic drawing after your initial response, however I am a student and soon got caught up in an academic commitment as we tend to do. Next time, I'll post all the code at least!