// Motor Control using PWM on ESP32-C3
const int motorPin = 3; // Use GPIO3 (check available PWM pins on ESP32-C3)
const int pwmChannel = 0;
const int freq = 5000;
const int resolution = 8; // 8-bit: 0–255
void setup() {
Serial.begin(115200);
ledcSetup(pwmChannel, freq, resolution);
ledcAttachPin(motorPin, pwmChannel);
Serial.println("Motor PWM control started...");
}
void loop() {
// Increase speed gradually
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle += 5) {
ledcWrite(pwmChannel, dutyCycle);
Serial.print("Increasing speed: ");
Serial.println(dutyCycle);
delay(50);
}
delay(500);
// Decrease speed gradually
for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle -= 5) {
ledcWrite(pwmChannel, dutyCycle);
Serial.print("Decreasing speed: ");
Serial.println(dutyCycle);
delay(50);
}
delay(500);
}