Codementor Events

How to control several servos with ESP32?

Published Oct 01, 2019

I'm new with arduino/ESP32. I want to control several servo motors with the help of ESP32. I want to control all servos at different speeds and want to use sine wave logic to control the speed.

I have code that can control 2 servos seperataly. I have attached the code here,

#include <ESP32Servo.h>
#include "esp32-hal-ledc.h"
// create two servo objects 
Servo servo1SV;
Servo servo2SV;

// Published values of servos; adjust if needed
int minUs = 550;
int maxUs = 2250;

// These are all GPIO pins on the ESP32
// Recommended pins include 2,4,12-19,21-23,25-27,32-33 
int servo1SVPin = 15;   //outlet valve
int servo2SVPin = 16;   //inlet valve


int pos = 0;      // position in degrees
ESP32PWM pwm;
void setup() {
    Serial.begin(115200);
    servo1SV.setPeriodHertz(50);      // Standard 50hz servo
    servo2SV.setPeriodHertz(50);      // Standard 50hz servo
}

void loop() {
    servo1SV.attach(servo1SVPin, 500, maxUs);
    servo2SV.attach(servo2SVPin, minUs, maxUs);  

//// About outlet valve

    for (pos = 0; pos <= 85; pos += 1) { // sweep from 0 degrees to 85 degrees
        // in steps of 1 degree
        servo1SV.write(pos);
        delay(10);             // Opening of outlet valve
    }
 delay(5000);            // Outlet valve stay openned for 5 sec 

    for (pos = 85; pos >= 0; pos -= 1) { // sweep from 85 degrees to 0 degrees
        servo1SV.write(pos);
        delay(10);            // closing of outlet valve
    }

// About inlet valve

    for (pos = 0; pos <= 70; pos += 1) { // sweep from 0 degrees to 70 degrees
        // in steps of 1 degree
        servo2SV.write(pos);
        delay(10);             // Opening of inlet valve 
    }
  delay(5000);            // Inlet valve stay openned for 5 sec

    for (pos = 70; pos >= 0; pos -= 1) { // sweep from 70 degrees to 0 degrees
        servo2SV.write(pos);
        delay(10);            // Opening of inlet valve
    }

    servo1SV.detach();
    servo2SV.detach();
    delay(5000);
}

I want the same output that is given by this code but using sine wave. But I don't know how to implement sine wave logic in ESP32. How can I do that?

Discover and read more posts from Higa
get started
post commentsBe the first to share your opinion
Show more replies