Fire Sensor Tutorial for Arduino, ESP8266 and ESP32

Fire Sensor Tutorial for Arduino, ESP8266 and ESP32

In this article you learn how to build your own fire alarm that detects a fire inside your home.

In the first part of the tutorial you learn the different methods to detect a fire.

We finish the tutorial with a project where we build a fire alarm based on the KY-026 board with the YG1006 flame sensor and an active buzzer.

Fire Sensor

Table of Contents

How to detect a fire?

A fire or flame sensor detects if there is a present fire or not. Generally the optical detection is the fastest way to detect a fire. Therefore we will concentrate on sensors with optical detection.

During a fire, photons with different wavelength are emitted. The following table shows the different wavebands, their wavelength and the corresponding flame detection methods.

WavebandsWavelengthFlame Detection Methods
Ultraviolet (UV)< 0.3μmUltraviolet Detector
Visible (Vis)0.7μm – 1.1μmNear IR Array Detector
Infrared (IR)> 1.1μmIR Detector

What flame detection method you use for a fire alarm depends on the environmental conditions, but the relative intensity of photons in the waveband is a good indication. The following picture shows the typical emission spectrum of a hydrocarbon fire along with the 3 wavebands.

Hydrocarbon fire emission spectrum

In the following three sub-chapters we discuss every waveband with the corresponding flame detection method and finally choose our flame sensor that we use for the fire alarm.

Ultraviolet Detector

The ultraviolet waveband has a wavelength shorter than 0.3μm and is not visible for the human eyes. To detect a fire, the ultraviolet detector recognizes the UV radiation emitted at the instant of a fire ignition. The sensor has a very fast reaction time with 3-4 milliseconds but due to the fast reaction time, you have to add a delay of a couple seconds to minimize false alarms. False alarms are caused by other UV sources like lighting and sunlight.

Near IR Array Detector

In the visible wavebands between 0.7μm – 1.1μm, it is possible to use flame recognition technology to confirm a fire. The recognition system analyzes near IR radiation with multiple channel or pixel arrays to create a picture or video of the flames. In combination with digital image processing and video analysis, the Near IR Array Detector is the most reliable technology to detect a fire.

IR Detector

The next higher wavelength with 1.1μm and greater are in the infrared waveband. There are specialized fire-fighting thermal imaging cameras (TIC) that detect a fire based on specific patterns giving off by hot gases. From the hydrocarbon fire emission spectrum, you see that the resonance frequency of CO2 is between 4.3μm – 4.4μm. This wavelength has a very high intensity due to the CO2 that is released during a fire, and is therefore very good to detect a fire.
But also the IR detector can have false alarms that are caused by hot surfaces, thermal radiation in the background, water on the detector lens or exposure to direct sunlight.

YG1006 Fire Sensor

In our project to build a fire alarm we use the KY-026 flame sensor that is based on the YG1006 sensor, a high sensitive NPN silicon phototransistor. Due to its black epoxy, the sensor is sensitive to infrared radiation between 0.76μm and 1.1μm. When fire burns small amounts of infrared light is emitted. This light is received by the photodiode (IR receiver) on the sensor module.

Fire Sensor

The YG1006 flame sensor is build on a sensor board together with the LM393 comparator that is adjustable due to a potentiometer and transforms the analog signal of the YG1006 to a digital signal by comparing the analog signal to the predefined threshold of the potentiometer. If the digital pin is logic HIGH it indicates the presence of flame or fire. Logic LOW on output indicates absence of flame or fire.

The sensor board has an operation voltage between 3.3V and 5V and is therefore suitable for Arduino, ESP8266 and ESP32 microcontroller. The detection angle of the phototransistor is between 0 and 60 degree. Therefore a fire is detected in a large angle around the sensor.

The following table gives you an overview of all components and parts that I used for this tutorial. I get commissions for purchases made through links in this table.

Arduino UnoAmazonBanggoodAliExpress
ORESP8266 NodeMCUAmazonBanggoodAliExpress
ORESP32 NodeMCUAmazonBanggoodAliExpress
ANDKY-026 Flame SensorAmazonBanggoodAliExpress
ANDActive BuzzerAmazonBanggoodAliExpress

Wiring between the Fire Sensor and Microcontroller

The following pictures show how to connect the flame sensor to different microcontroller boards. I also added an active buzzer to the project that makes a sound if there is an active fire. If you are interested in more details about active or passive buzzers, visit the sound tutorial.

If your favorite board is missing, use the comment section below and I will add your board to this article. Also you can click on each picture to enlarge it to see the connection in detail.

Fire Sensor Arduino Nano

Fire Sensor Arduino Nano

Fire Sensor Arduino Pro Mini

Fire Sensor Arduino Pro Mini

Fire Sensor Arduino Uno

Fire Sensor Arduino Uno

Fire Sensor Arduino Mega

Fire Sensor Arduino Mega

Fire Sensor ESP32 NodeMCU

Fire Sensor ESP32 NodeMCU

Fire Sensor ESP8266 NodeMCU

Fire Sensor ESP8266 NodeMCU

Fire Sensor ESP8266 WeMosD1 Mini

Fire Sensor ESP8266 WeMosD1 Mini

From the wiring between the fire sensor and the different Arduino, ESP8266 and ESP32 microcontroller, you see that I use different pins. Therefore we have to take care that the right pins are used in the program code, that you see in the following section.

Arduino Program Code for the Fire Sensor

int Buzzer = 6;        // used for Arduino
int Fire_analog = A0;    // used for Arduino
int Fire_digital = 7;   // used for Arduino

//int Buzzer = D2;        // used for ESP8266
//int Fire_analog = A0;    // used for ESP8266
//int Fire_digital = D1;   // used for ESP8266

//int Buzzer = 32;        // used for ESP32
//int Fire_analog = 4;    // used for ESP32
//int Fire_digital = 2;   // used for ESP32

void setup() {
  pinMode(Buzzer, OUTPUT);      
  pinMode(Fire_digital, INPUT);

void loop() {
  int firesensorAnalog = analogRead(Fire_analog);
  int firesensorDigital = digitalRead(Fire_digital);

  Serial.print("Fire Sensor: ");
  Serial.print("Fire Class: ");
  if (firesensorAnalog < 1000) {
    digitalWrite (Buzzer, HIGH) ; //send tone
    digitalWrite (Buzzer, LOW) ;  //no tone
  else {
    Serial.println("No Fire");

In the first part of the program code, we have to define the connected pins. If you copy the script, it is commented in the way that it runs with Arduino boards. If you want to use ESP8266 or ESP32 boards, you have to comment the three Arduino lines and uncomment the lines for your microcontroller that you want to use.

The three pins that we define are the following

Digital pin to active buzzer6D232
Analog pin of fire sensorA0A04
Digital pin of fire sensor7D10

Because the ESP32 has build-in analog to digital converter, you can use multiple pins as analog input. The pins that you can use as analog pins are described in the ESP32 pinout tutorial.

In the setup function, we set the baud rate to 115200, that have to match to the baud rate at the serial monitor in the Arduino IDE. Also we define the digital pin that connects the buzzer as output because we want to activate and deactivate the buzzer in case of a fire. Also we set the digital connection of the fire sensors output as input of the Arduino, ESP8266 or ESP32 microcontroller, because we want to read the digital value.

In the loop function, we first read the analog and digital value of the fire sensor and print both values to the serial interface. To separate each value in the serial monitor, we use the tab function \t.

Now we have to define if there is a fire or not. This decision can be made based on two values:

  • The analog value of the fire sensor
  • The digital value of the fire sensor

My suggestion is always to choose the analog value because in most cases it is easier to change the threshold in software, compared to the adjustable LM393 comparator that outputs the digital value.

For this definition, I set the threshold to 1000 and lower for the analog value of the fire sensor. For the initial build you can also use the threshold of 1000 but maybe you have to play around with the potentiometer and different thresholds to get a stable operation point for your fire sensor.

For the fire sensor, lower values mean a higher infrared light emitted from a fire. If the analog value of the fire sensor is lower than 1000, we print in the serial monitor that there is a fire and set the buzzer high. When the active buzzer is activated we wait for 1 second and turn the buzzer off. If the analog value is higher or equal 1000, we print that there is no fire.

At the end of the program code we wait for 100 milliseconds and start the loop function all over.

The following video shows the fire sensor in action. The fire is detected and you hear the buzzer.

Fire Alarm for Arduino, ESP8266 and ESP32

I hope you enjoyed reading my tutorial about the YG1006 fire sensor. You can also add a MQ-2 gas sensor to the fire alarm to detect smoke in case of a fire. In the MQ-2 gas sensor tutorial you learn how to add the gas sensor to your project.

If you have any questions regarding this article, use the following comment section to ask your questions. I will answer them as soon as possible.

Leave A Comment