Skip to content

Instantly share code, notes, and snippets.

@makersenses
Created April 26, 2021 15:32
Show Gist options
  • Save makersenses/51bdf8d03feab536c393e7f8dc2c2a62 to your computer and use it in GitHub Desktop.
Save makersenses/51bdf8d03feab536c393e7f8dc2c2a62 to your computer and use it in GitHub Desktop.
15.esp32_motor_pwm
const int motor_AL = 4;
const int motor_AR = 5;
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(motor_AL, ledChannel);
pinMode(motor_AL, OUTPUT);
pinMode(motor_AR, OUTPUT);
digitalWrite(motor_AR, LOW);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment