I tried to rotate the servo motor180 degrees and go back to 00 but it turns off and turns on the arduino

hello, I am creating an animal feeder with an motor servo at a certain time defined by the user, but when it rotates 180 degrees it turns off and on the arduino and rotates to 0 degrees.
Does anyone know why they turn off and on the arduino?

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

#include <DS1302.h>
#include <Wire.h>
#include <Servo.h> 
int servoPin = 13;

Time t;
DS1302 rtc(6, 7, 8); //RST,DAT,CLK Pins of the DS1302 Module 


//parâmetros de horário que serão atualizados
int horaAtual, minutoAtual, segundoatual;
bool LedCycleDone = false; 
//parâmetros primeira alimentação
int horaAlimentacao1 = 16, minutoAlimentacao1 = 17, segundoAlimentacao1 = 30;
//parâmetros segunda alimentação
int horaAlimentacao2 = 16, minutoAlimentacao2 = 17, segundoAlimentacao2 = 0;

Servo Servo1;

void setup()
{
  rtc.halt(false);
  rtc.writeProtect(false);
  lcd.backlight();
  lcd.init();
  lcd.begin(16, 2);
  //rtc.setTime(16, 06, 00);  //Hour, Min, Sec 
  //rtc.setDate(27, 6, 2023); //Day, Month, Year
  Servo1.attach(servoPin); 
  Servo1.write(0); 


}

void loop()
{
  //determina o horário atual
  t = rtc.getTime();
  horaAtual = t.hour;
  minutoAtual = t.min;
  segundoatual = t.sec;
  lcd.setCursor(0,1);
  lcd.print("Hora:");
  lcd.setCursor(5, 1);
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0, 0);
  lcd.print("Data:");
  lcd.setCursor(5,0);
  lcd.print(rtc.getDateStr(FORMAT_SHORT, FORMAT_LITTLEENDIAN, '/'));
  if(!LedCycleDone) {
    if (horaAtual == horaAlimentacao1 && minutoAtual == minutoAlimentacao1 && segundoatual == segundoAlimentacao1) {
          Servo1.write(180); 
          delay(650); 
          Servo1.write(0); 
      }
    if (horaAtual == horaAlimentacao2 && minutoAtual == minutoAlimentacao2 && segundoatual == segundoAlimentacao2) {
          Servo1.write(180); 
          delay(650); 
          Servo1.write(0); 
      }
  if (LedCycleDone) {
    if (horaAtual != horaAlimentacao1 || minutoAtual != minutoAlimentacao1 || segundoatual != segundoAlimentacao1) { 
      LedCycleDone = false; // Reset the flag once the timing condition is over
    }
    if (horaAtual != horaAlimentacao2 || minutoAtual != minutoAlimentacao2 || segundoatual != segundoAlimentacao2) {
      LedCycleDone = false; // Reset the flag once the timing condition is over
    } 
   }
  }
}
  • which Arduino are you using and how is it powered ?

  • How is the servo powered ? From the Arduino 5V pin or from an external source ?

  • Is the servo capable of moving through 180 degrees ?

  • What happens if you run the Servo Sweep example ?

Misbehavior of servos very often results from following terrible advice, like trying to power the servo from the Arduino 5V output. Use a separate power supply instead, and connect all the grounds.

I quite agree with the above.

The reason the Arduino is turning off is that the servo is pulling so much current from the Arduino that the voltage drops so much that this causes a reset.

Not all servos will actually do 0 degrees to 180 degrees. If the srrvo hits an end stop before it gets to the commanded position it will stall. A stalled motor draws maximum current.

It is a good idea to experimentally determine the limits of a servo before using it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.