ESP32 - Flame Sensor

The flame sensor can detect and measure the infrared light coming from a flame. It's useful for spotting fires and is also known as an infrared flame sensor or fire sensor. This sensor provides two types of information: one is like a simple switch (either on or off), and the other is analog signal showing the strength of the flame.

In this tutorial, we'll learn how to use an ESP32 with a flame sensor to detect flames. Specifically, we'll cover these steps:

esp32 flame sensor

Afterward, you can modify the code to activate a warning horn when the fire is detected.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Flame Sensor
1×5-in-1 5-way Flame Sensor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to Flame Sensor

infrared flame fire-sensor module

The infrared flame sensor can detect a flame or check how much infrared light the flame gives off. So, it helps us spot fires. This sensor offers two choices using a digital output pin and an analog output pin.

These sensors are designed to catch a certain types of infrared light emitted by flames while ignoring other types, like the heat from people or indoor lights. But like any sensor, they have their limits, and sometimes they might make mistakes, either saying there's a fire when there isn't (false positive) or missing a fire when it's there (false negative).

Pinout

There are two types of flame sensor modules available:

A single flame sensor includes four pins:

  • VCC pin: It needs to be connected to VCC (3.3V to 5V).
  • GND pin: It needs to be connected to GND (0V).
  • DO pin: It is a digital output pin. It is HIGH if the flame is not detected and LOW if detected. The threshold value for flame detection can be adjusted using a built-in potentiometer.
  • AO pin: It is an analog output pin. The output value decreases as the infraed level is decreased, and it increases as infraed level is increased.
Flame Sensor Pinout
image source: diyables.io

Furthermore, it has two LED indicators:

  • One PWR-LED indicator for power.
  • One DO-LED indicator for the flame state on the DO pin: it is on when flame is present.

The 5-in-1 flame sensor module combines five individual flame sensors on a single PCB. The sensors share a common potentiometer, VCC, and GND connections, while each sensor has its own independent DO (Digital Output) and AI (Analog Input) pins.

How It Works

For the DO pin:

  • The module has a built-in potentiometer for setting the infrared threshold (sensitivity).
  • When the infrared intensity is above the threshold value, the flame is detected, the output pin of the sensor is LOW, and the DO-LED is on.
  • When the infrared intensity is below the threshold value, the flame is NOT detected, the output pin of the sensor is HIGH, and the DO-LED is off.

For the AO pin:

  • The higher the infrared intensity in the surrounding environment, the higher the value read from the AO pin.
  • The lower the infrared intensity in the surrounding environment, the lower the value read from the AO pin.

Note that the potentiometer does not affect the value on the AO pin.

Wiring Diagram

Since the flame sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • How to connect ESP32 and flame sensor using breadboard

The flame sensor module provides two output options. You can use one or both based on what you need.

ESP32 Flame Sensor wiring diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and flame sensor

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Read value from DO pin

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-flame-sensor */ #define DO_PIN 13 // ESP32's pin GPIO13 connected to DO pin of the flame sensor void setup() { // initialize serial communication Serial.begin(9600); // initialize the ESP32's pin as an input pinMode(DO_PIN, INPUT); } void loop() { int flame_state = digitalRead(DO_PIN); if (flame_state == HIGH) Serial.println("No flame dected => The fire is NOT detected"); else Serial.println("Flame dected => The fire is detected"); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Direct the flame sensor to a flame.
  • See the result on Serial Monitor.
COM6
Send
No flame dected => The fire is NOT detected No flame dected => The fire is NOT detected Flame dected => The fire is detected Flame dected => The fire is detected Flame dected => The fire is detected No flame dected => The fire is NOT detected No flame dected => The fire is NOT detected No flame dected => The fire is NOT detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Please keep in mind that if you notice the LED status remaining on constantly or off even when the sensor faces to a flame, you can adjust the potentiometer to fine-tune the sensitivity of the sensor.

ESP32 Code - Read value from AO pin

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-flame-sensor */ #define AO_PIN 36 // ESP32's pin GPIO36 connected to AO pin of the flame sensor void setup() { // initialize serial communication Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); } void loop() { int infrared_value = analogRead(AO_PIN); Serial.print("The AO value: "); Serial.println(infrared_value); }

Quick Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Direct the flame sensor to a flame.
  • See the result on Serial Monitor.
COM6
Send
245 246 246 573 677 949 955 1004 1007 1013 1018 641 543 340 179
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a flame sensor. The ESP32 ADC is good for projects that do NOT need high accuracy. However, for projects that need precise measurements, please note:

  • The ESP32 ADC is not perfectly accurate and might need calibration for correct results. Each ESP32 board can be a bit different, so you need to calibrate the ADC for each individual board.
  • Calibration can be difficult, especially for beginners, and might not always give the exact results you want.

For projects that need high precision, consider using an external ADC (e.g ADS1115) with the ESP32 or using an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to ESP32 ADC Calibration Driver

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Learn More

※ OUR MESSAGES