This example show how to program ESP32 (ESP32-DevKitC-V4) to drive Servo Motors (one SG90/two DS3120) using ESP32Servo lib, using ESP32Servo library.
Drive one SG90 Servo Motor:
Connection:
Install ESP32Servo in Arduino IDE's Library manager.
Open example of ESP32Server > Sweep.
As shown in the video, change min/max to 500/2500.
Sweep_esp32_sg90.ino/* Sweep
Original from ESP32Servo examples Sweep
https://github.com/madhephaestus/ESP32Servo
*/
#include <ESP32Servo.h>
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
int servoPin = 18;
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2500); // attaches the servo on pin 18 to the servo object
// 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() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Drive two DS3120 Servo Motor:
Connection:
modify the code to add one more Servo:
Sweep_esp32_two_ds3120.ino
/* Sweep
Original from ESP32Servo examples Sweep
https://github.com/madhephaestus/ESP32Servo
*/
#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2;
// 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
int servoPin = 18;
int servoPin2 = 19;
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2500); // attaches the servo on pin 18 to the servo object
myservo2.setPeriodHertz(50); // standard 50 hz servo
myservo2.attach(servoPin2, 500, 2500);
// 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() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos);
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos);
delay(15); // waits 15ms for the servo to reach the position
}
}
Next:
Can you please indicate what power source you are using there? including the readout device with the voltages?
ReplyDeleteThank you!