// ESP32 Servo motor control using Serial
/*
Servo Motor SG90
i. rotation angle of 180
ii PWM control signal with period of 20ms (50Hz) and pulse duration between 1ms (to left) to 2ms (to right)
iii Control using LEDC PWM Controller with 50Hz, 8-bit resolution, 50% duty cycle
iv. Duty cycle determines the position of the servo
ESP32 Pin connection
GPIO16 - PWM
*/
// minimum 7% maximum 31%
int servoPin = 16;
int PWMFreq = 50;
int PWMChannel = 0;
int PWMResolution = 8;
int dutyCycle;
void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
ledcSetup (PWMChannel, PWMFreq, PWMResolution);
ledcAttachPin (servoPin, PWMChannel);
ledcWrite (PWMChannel, dutyCycle);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available())
{
String user_string = Serial.readStringUntil('\n');
dutyCycle = user_string.toInt();
Serial.println (dutyCycle);
ledcWrite (PWMChannel, dutyCycle);
delay (10);
}
}