Skip to content

Instantly share code, notes, and snippets.

@elktros
Created March 17, 2021 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/5a17c649c5e8eb6270412638af21eed6 to your computer and use it in GitHub Desktop.
Save elktros/5a17c649c5e8eb6270412638af21eed6 to your computer and use it in GitHub Desktop.
Control Servo Motor using ESP32 and Serial Input.
const int servoPin = 16; /* GPIO16 */
int dutyCycle = 0;
/* Setting PWM properties */
const int PWMFreq = 50;
const int PWMChannel = 0;
const int PWMResolution = 8;
//const int MAX_DUTY_CYCLE = (int)(pow(2, PWMResolution) - 1);
void setup()
{
Serial.begin(115200);
ledcSetup(PWMChannel, PWMFreq, PWMResolution);
/* Attach the LED PWM Channel to the GPIO Pin */
ledcAttachPin(servoPin, PWMChannel);
ledcWrite(PWMChannel, dutyCycle);
}
void loop()
{
while(Serial.available())
{
String in_char = Serial.readStringUntil('\n');
dutyCycle = in_char.toInt();
Serial.println(dutyCycle);
ledcWrite(PWMChannel, dutyCycle);
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment