// Smoke-Controlled Ventilation Fan using Stepper Motor (A4988)
// Wokwi Compatible – DC fan replaced by Stepper motor
// -------- Pin Definitions --------
int gasPin = A0; // Gas sensor analog output
int stepPin = 3; // A4988 STEP pin
int dirPin = 4; // A4988 DIR pin
int enablePin = 5; // A4988 ENABLE pin (optional)
// -------- Gas Thresholds --------
int minGas = 200; // Clean air
int maxGas = 900; // Heavy smoke
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(dirPin, HIGH); // Fixed rotation direction
digitalWrite(enablePin, LOW); // Enable driver (LOW = enabled)
Serial.begin(9600);
}
void loop() {
int gasValue = analogRead(gasPin);
// Map gas value to step delay (inverse: more gas = faster motor)
int stepDelay = map(gasValue, minGas, maxGas, 2000, 200);
stepDelay = constrain(stepDelay, 200, 2000);
// Debug
Serial.print("Gas: ");
Serial.print(gasValue);
Serial.print(" | Step Delay (us): ");
Serial.println(stepDelay);
// Run stepper motor
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}