// PWM Fan Control with ESP32 in Wokwi
const int fanPin = 5; // PWM output to fan (can be simulated with LED)
const int pwmFreq = 25000; // 25kHz PWM frequency
const int pwmChannel = 0; // PWM channel
const int pwmResolution = 8;// 8-bit resolution (0-255)
int fanSpeed = 128; // Mid speed (50%)
void setup() {
Serial.begin(115200);
ledcSetup(pwmChannel, pwmFreq, pwmResolution); // Configure PWM
ledcAttachPin(fanPin, pwmChannel); // Attach pin to channel
ledcWrite(pwmChannel, fanSpeed); // Set initial speed
Serial.println("PWM Fan Control Initialized");
}
void loop() {
// Simulate dynamic control (e.g., from temperature sensor)
for (int speed = 0; speed <= 255; speed += 5) {
ledcWrite(pwmChannel, speed);
Serial.print("Fan Speed PWM: ");
Serial.println(speed);
delay(100);
}
for (int speed = 255; speed >= 0; speed -= 5) {
ledcWrite(pwmChannel, speed);
Serial.print("Fan Speed PWM: ");
Serial.println(speed);
delay(100);
}
}