Smoke Detection System with Arduino Uno R4 WiFi, Servo Motor, and Voice Alert
Check the video below for demonstration:
In this project, I will show you how to gradually open and close a door and play an MP3 file when smoke is detected. We will use the Arduino Uno R4 WiFi board, an MQ-2 smoke sensor, the DFPlayer Mini module, and an SG92R servo motor.

When smoke is detected, the DFPlayer Mini module will play an MP3 file that says “Gas has been detected”, and the door will gradually close to prevent oxygen from entering the house.
Parts Required
Below is the list of all components used in this project:

- Arduino Uno R4 WiFi
- MQ-2 Smoke Sensor
- SG92R Servo Motor
- 20 Ω, 1 W Speaker
- DFPlayer Mini Module
- External Power Supply
- SD Card
DFPlayer Mini
The DFPlayer Mini is a compact audio player module that supports audio formats such as MP3, WAV, and WMA. It features a simplified output, allowing direct connection to a speaker. The module includes an SD card slot and supports cards up to 32 GB formatted in FAT16 or FAT32. It can be powered with a 3.2 V to 5 V DC supply and communicates with the microcontroller using the UART protocol.

Preliminary Steps
Before getting started with the DFPlayer Mini module, there are a few important preparations to consider.
SD Card & MP3 File
You will need an SD card (maximum 32 GB) and an MP3 file that will play when smoke is detected.

How to Copy Audio Files to the SD Card
Format the SD Card: Make sure your micro SD card is formatted using FAT16 or FAT32.
Organize Folders: Rename standard folders using numbers like 01, 02, 03 … up to 99.

Rename Audio Files: Audio files inside these folders should be renamed as 001.mp3, 002.mp3, 003.mp3 … up to 255.mp3. You can also use WAV format (001.wav, 002.wav, etc.).

Optional File Naming: You may keep the original file name while adding a number prefix. For example, if the original file is “Yesterday Once More.mp3”, rename it to 001Yesterday Once More.mp3.
For this project, I created my audio files using TTSMaker. Below, I have provided the audio file used in this project.

After creating the audio files using TTSMaker and copying them to the SD card, don’t forget to insert the SD card into the DFPlayer Mini.

Door
The door used in this project is made out of cardboard, as I don’t have a 3D printer. To create it, I drew the door shape on the cardboard using a pen and ruler. Then, I carefully made the necessary cuts with a sharp knife to produce a door that can open and close using the SG92R servo motor and the control module.

If you want to see a detailed guide on how I built the door, I recommend checking out this blog post:
Arduino Rain Detection System with Servo, LED, and LED Matrix Display
Circuit Diagram

Connections between Arduino Uno R4 WiFi, DFPlayer Mini, and Speaker:
| Arduino Uno R4 WiFi | DFPlayer Mini | Speaker |
| Pin 2 | Tx | |
| Pin 3 | Rx | |
| 3.3V | Vcc | |
| GND | GND | |
| SPK1 | + | |
| SPK2 | – |
Install the DFRobotDFPlayerMini Library
Before using the DFPlayer Mini module, you need to install its library in the Arduino IDE:
- Open the Arduino IDE and go to Library Manager.
- In the search bar, type “DFRobotDFPlayerMini”.
- Locate the library named “DFRobotDFPlayerMini by DFRobot” and install it.
This library allows your Arduino to communicate with the DFPlayer Mini and control audio playback easily.

Install the Servo Library
Before controlling the SG92R servo motor, you need to install the Servo library in the Arduino IDE:
- Open the Arduino IDE and go to Library Manager.
- In the search bar, type “Servo”.
- Locate the library named “Servo by Michael Margolis, Arduino” and install it.
This library allows your Arduino to control the servo motor’s position smoothly and accurately.

Arduino code
#include <Servo.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
Servo myservo;
#define whiteLED 6 //I didnt use but you can use it if you want. Its will act like a lightbulb above the door outside
#define SERVO_PIN 5
#define MQ135Sensor A0
SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
bool SMOKEDetected = false;
// Timing variables
unsigned long doorOpenStart = 0;
const unsigned long doorOpenDuration = 2000;
int targetPos = 80;
int currentPos = 80;
unsigned long lastServoMove = 0;
const unsigned long servoStepInterval = 50;
void setup() {
myservo.attach(SERVO_PIN);
Serial.begin(115200);
pinMode(whiteLED, OUTPUT);
pinMode(MQ135Sensor, INPUT);
mySoftwareSerial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println("DFPlayer not initialized!");
} else {
myDFPlayer.volume(25);
Serial.println("DFPlayer OK!");
}
myservo.write(currentPos);
}
void loop() {
int analogValue = analogRead(MQ135Sensor);
Serial.print("MQ135 Value: ");
Serial.println(analogValue);
unsigned long currentMillis = millis();
// -------------------------------------
// SMOKE DETECTED (CLOSE THE DOOR)
// -------------------------------------
if (analogValue > 70) {
if (!SMOKEDetected) {
SMOKEDetected = true;
Serial.println("SMOKE DETECTED! CLOSING door...");
digitalWrite(whiteLED, HIGH);
myDFPlayer.loop(5);
targetPos = 27; // CLOSE the door smoothly
doorOpenStart = currentMillis;
} else {
doorOpenStart = currentMillis;
}
}
// -------------------------------------
// NO SMOKE (OPEN THE DOOR AFTER DELAY)
// -------------------------------------
if (SMOKEDetected && (currentMillis - doorOpenStart >= doorOpenDuration)) {
SMOKEDetected = false;
Serial.println("SMOKE cleared. OPENING door...");
digitalWrite(whiteLED, LOW);
myDFPlayer.stop();
targetPos = 80; // OPEN the door smoothly
doorOpenStart = currentMillis;
}
// -------------------------------------
// SERVO SMOOTH MOTION
// -------------------------------------
if (currentMillis - lastServoMove >= servoStepInterval) {
lastServoMove = currentMillis;
if (currentPos < targetPos) currentPos++;
else if (currentPos > targetPos) currentPos--;
myservo.write(currentPos);
}
// Clear DFPlayer buffer
if (myDFPlayer.available()) {
myDFPlayer.readType();
}
}
How the projects works
When the project starts, the door is fully open at 80°, the DFPlayer Mini is initialized, and the white LED is off.

As soon as smoke is detected by the MQ135 sensor (value above 70), the system sets SMOKEDetected to true, turns on the LED, and starts playing the MP3 file saying “Gas has been detected.” The servo then begins to move the door gradually toward 27°, starting to close it.

When the servo reaches 27°, the door is fully closed, and the audio keeps playing in a loop.

Once the smoke clears and the sensor value drops below 70, after a 2-second delay, the system stops the MP3, turns off the LED, and sets the servo target back to 80°. The door begins to open gradually.

Finally, when the servo reaches 80°, the door is fully open again, the system is ready, and the DFPlayer Mini and LED are back to their initial states.

