Servo sg 90 with ESP8266 refuses to move

I use the code and the circuit for this project

I tried to use just the pin9 for the servo signal,ground,and 5v but the servo refuses to do a single move.I thought that the problem might be the connection so I tried to change cables and I realized that if I take the power wire and connect it back (not full connection,just a simple touch) the servo reacts for a small amount of time and stops.I tried to connect and disconnect but the servo only vibrates a moment .Am I missing something?

#include <SoftwareSerial.h>       //Including the software serial library
#include <Servo.h>                //including the servo library
SoftwareSerial esp(4, 5);         
#define DEBUG true                //This will display the ESP8266 messages on Serial Monitor
#define servopin 9                //connect servo on pin 9
Servo ser;                        //variable for servo
int current_pos = 170;      
int v = 10;                 
int minpos = 20; 
int maxpos = 160;
void setup()
{
  ser.attach(servopin);
  ser.write(maxpos);
  ser.detach();
  Serial.begin(115200);
  esp.begin(115200);
  
  sendData("AT+RST\r\n", 2000, DEBUG);                      //This command will reset module
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG);                 //This will set the esp mode as station mode
  sendData("AT+CWJAP=\"wifi-name\",\"wifi-password\"\r\n", 2000, DEBUG);   //This will connect to wifi network
  while(!esp.find("OK")) {                                  //this will wait for connection
  } 
  sendData("AT+CIFSR\r\n", 1000, DEBUG);          //This will show IP address on the serial monitor
  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);       //This will allow multiple connections
  sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); //This will start the web server on port 80
}
void loop()
{
  if (esp.available())     //check if there is data available on ESP8266
  {
    if (esp.find("+IPD,")) //if there is a new command
    {
      String msg;
      esp.find("?");                           //run cursor until command is found
      msg = esp.readStringUntil(' ');          //read the message
      String command = msg.substring(0, 3);    //command is informed in the first 3 characters "sr1"
      String valueStr = msg.substring(4);      //next 3 characters inform the desired angle
      int value = valueStr.toInt();            //convert to integer
      if (DEBUG) {
        Serial.println(command);
        Serial.println(value);
      }
      delay(100);
      //move servo to desired angle
      if(command == "sr1") {
         //limit input angle
         if (value >= maxpos) {
           value = maxpos;
         }
         if (value <= minpos) {
           value = minpos;
         }
         ser.attach(servopin); //attach servo
         while(current_pos != value) {
           if (current_pos > value) {
             current_pos -= 1;
             ser.write(current_pos);
             delay(100/v);
           }
           if (current_pos < value) {
             current_pos += 1;
             ser.write(current_pos);
             delay(100/v);
           }
         }
         ser.detach(); //dettach
      }
  }
  }
  }
String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  esp.print(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (esp.available())
    {
      char c = esp.read();
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}

tester11:
Am I missing something?

Yes.
Your code. Post it using the code tags.
Your wiring diagram. Pencil, paper and a camera will do if you include relevant detail.

I updated the post,I can try just

  ser.attach(9);
  ser.write(0);

and will not work

tester11:
I updated the post

Don't do that. It breaks the chronological order of the thread, confusing everyone. Post it in a reply instead.
This preserves the order of the comments.

I see that you posted an image of a f*ing thing. This is not very helpful. Maybe if it were in a large enough resolution we could determine which pin each wire is connected to. But at the resolution you posted, it is entirely unclear what is connected to where. That is why so many here hate fritzing. That is why I suggested pencil, paper and a camera.

One of the issues that your f*ing image does reveal is that your servo does not have an independent power source. This is the number 1 reason for failure using servos. Some times, under some conditions, a servo can be powered by the regulator on the arduino. But it is never a good idea. Often, what did work in a test suddenly fails. Perhaps the servo is given a load. Perhaps other peripherals like wifi cards are added, increasing the load on the regulator. But the first thing you should try is giving the servo its own power supply (with the ground connected to the arduino ground)

and will not work

This is not very helpful.
What does it do?
How does that differ from your expected results?

I see that you are detaching the servo. Why?
Usually I attach the servo once in the setup and then leave it attached forever.

Keep plugging away at this. We will get you there! :slight_smile:

I have read in a few places that SoftwareSerial and Servo libraries conflict and cannot be used together. Never had that problem myself but maybe you could try replacing SoftwareSerial.h with a different serial library, see this post on arduino.stackexchange

As well as sorting the power out!

Steve

The ServoTimer2 library is often recommended in the case of a conflict between software serial and Servo libraries.

1 Like