0

I have ESP32 connected to TZT servo SG90. This is the code

#include <ESP32Servo.h>
#define TURN_TIME 175


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 
#define servoPin  17
#define MOTOR_1_PIN 22

void setup() {
  pinMode(servoPin, OUTPUT);  
  Serial.begin(9600);
    // Allow allocation of all timers
    ESP32PWM::allocateTimer(0);
    ESP32PWM::allocateTimer(1);
    ESP32PWM::allocateTimer(2);
    ESP32PWM::allocateTimer(3);
    myservo.setPeriodHertz(50);    // standard 50 hz servo
    // 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() {


  myservo.attach(servoPin, 700, 2500); // attaches the servo on pin 18 to the servo object
  myservo.write(0);
  delay(TURN_TIME);
  for(int i =0; i<180; i++)
  {
    myservo.write(i);
    delay(TURN_TIME);
  }
  delay(3000);
  for(int i =180; i>0; i--)
  {
    myservo.write(i);
    delay(TURN_TIME);
  }
  myservo.detach();
  delay(3000);
 
}

My servo starts rotating one way and reverse for few degrees, then after 5 seconds the servo starts rotating at one way forever..

I had no problems with arduino and nodemcu board. What should I change in the code?

EDIT: I have connected the servo with battery, they have common ground. The pin on esp32 that I am using is G17. "#define servoPin 17" in the code

5
  • 1
    There is no need to detach the servo in the loop. Just attach it in setup and never detach it. Detaching will stop the pwm signal, but a servo needs a continuous pwm signal even when stationary.
    – Sim Son
    Oct 18, 2020 at 20:42
  • Without detach() the result is the same. Oct 18, 2020 at 20:45
  • I didn't expect it to be the root cause... rotating at one way forever - is it actually an angle-controlled servo that you are using and do you use the exact same hard- and software like you did on the arduino/nodeMCU?
    – Sim Son
    Oct 18, 2020 at 20:52
  • It's servo which can be rotated in 360 degrees. But when I control it with other microcontroller I can control it only 0-180. Here I have random moves and at some point rotation to infinity.. Oct 18, 2020 at 20:59
  • 1
    Can you figure out, at which point in your code the servo behaves unexpectedly?
    – Sim Son
    Oct 18, 2020 at 21:15

1 Answer 1

0

I used other servo library and it worked.. Then i changed the pulse width with this line of code. The default is 1000-2000. But it rotates only 95 degrees. When it's pushed to around ~2850us the servo rotates up to 181-185 degrees. servo1.attach(servoPin,CHANNEL_NOT_ATTACHED, 0,180,544,2840/);

#include <Servo_ESP32.h>

static const int servoPin = 16; //printed G14 on the board
int delayTime = 20;
Servo_ESP32 servo1;

int angle =0;
int angleStep = 5;

int angleMin =0;
int angleMax = 180;
#define CHANNEL_NOT_ATTACHED -1 
void setup() {
    Serial.begin(115200);
    servo1.attach(servoPin,CHANNEL_NOT_ATTACHED, 0,180,544,2840/);
    
}

void loop() {
    for(int angle = 0; angle <= angleMax; angle +=angleStep) {
        servo1.write(angle);
        Serial.println(angle);
        delay(delayTime);
    }

    for(int angle = 180; angle >= angleMin; angle -=angleStep) {
        servo1.write(angle);
        Serial.println(angle);
        delay(delayTime);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.