const int gasSensorPin = A0;
const int motorPin = 9; // Must be a PWM pin (~9)
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600); // For monitoring values in Wokwi console
}
void loop() {
int gasValue = analogRead(gasSensorPin); // Range 0 - 1023
// Map the sensor value to motor speed
// Assuming 100 is clean air and 700 is heavy smoke
int fanSpeed = map(gasValue, 100, 700, 0, 255);
// Constrain the value so it stays between 0 and 255
fanSpeed = constrain(fanSpeed, 0, 255);
analogWrite(motorPin, fanSpeed); // Set motor speed
// Debugging info for Wokwi Serial Monitor
Serial.print("Gas Level: ");
Serial.print(gasValue);
Serial.print(" | Fan Speed: ");
Serial.println(fanSpeed);
delay(200); // Short delay for stability
}