// 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);
}
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
servo1:GND
servo1:V+
servo1:PWM