Robotics: servo motors

Controlling servo with ESP8266 / ESP32 board

Here is the code to controm servo with ESP8266 board. Here, the ESP32Servo is used, other than that, the code is pretty much the same.

                    
#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo

// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27, 32
int servoPin = 13;

void setup() {
myservo.setPeriodHertz(50); 
myservo.attach(servoPin);
}

void loop() {
myservo.write(0); 
delay(2000);
myservo.write(180);
delay(2000);
}
                    
                

Same can be achieved with standard Servo library:

                    
#include <Servo.h>

Servo servo;
    
void setup() 
{
    servo.attach(2); //D4
    servo.write(0);
    delay(2000);
}
    
void loop() 
{
    servo.write(90);
    delay(1000);

    servo.write(0);
    delay(1000);
}                        
                    
                

(C) snowcron.com, all rights reserved

Please read the disclaimer