Skip to content

Instantly share code, notes, and snippets.

@elktros
Created March 17, 2021 11:25
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/46582bb9db3acd24a5983bd8ffad1a53 to your computer and use it in GitHub Desktop.
Save elktros/46582bb9db3acd24a5983bd8ffad1a53 to your computer and use it in GitHub Desktop.
Sweep program for ESP32 Servo Control.
/* ESP32 Servo Sweep */
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()
{
for(dutyCycle = 5; dutyCycle <= 32; dutyCycle++)
{
ledcWrite(PWMChannel, dutyCycle);
delay(70);
}
for(dutyCycle = 32; dutyCycle >= 5; dutyCycle--)
{
ledcWrite(PWMChannel, dutyCycle);
delay(70);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment