ESP8266 IR Flame Sensor Project
The ESP8266 IR Flame Sensor project demonstrates how to use an IR flame sensor with an ESP8266 microcontroller to detect the presence of a flame. The sensor detects infrared light emitted by flames and sends signals to the ESP8266, which interprets and displays the detection status on the Serial Monitor.
How It Works
A flame sensor module is a specialised infrared detector tuned to the 760–1100 nm band, where a hydrocarbon flame emits strongly. The sensing element is a photodiode in a dark epoxy package — usually angled at 60° — that responds to that near-infrared signature rather than to visible light.
Most carriers, such as the KY-026, provide two outputs. The analog pin gives a continuous voltage that falls as flame intensity rises, useful for judging how close or how large a fire is. The digital pin is the output of an LM393 comparator that trips when the analog level crosses a threshold set by the onboard potentiometer.
The sensor cannot distinguish a flame from any other strong near-IR source. Sunlight, incandescent bulbs and heat lamps all trigger it. That is why a practical fire detector cross-checks the flame sensor against a temperature sensor or a gas sensor rather than acting on it alone.
Components Needed
- ESP8266 (NodeMCU)
- IR Flame Sensor Module
- Jumper Wires
- Power Supply
Wiring to the ESP8266 (NodeMCU)
Connect AO to A0 and DO to D5, with VCC on the 3.3 V rail and GND to ground. Either output alone is enough — use the analog pin when you want proportional readings and the digital pin when you only need a trip signal.
With only one ADC channel on this board, consider using the digital output and leaving A0 free for another sensor.
Set the threshold with the onboard potentiometer: hold a lighter at the distance you want to trigger at, then turn the pot until the module's indicator LED just lights. Verify it stays off with the flame removed.
| Module pin | ESP8266 (NodeMCU) pin | Function |
|---|---|---|
| A0 / AO | A0 | Analog intensity — falls as flame grows |
| D0 / DO | D5 | Comparator output, LOW when threshold crossed |
| VCC | 3V3 | Supply |
| GND | GND | Common ground |
Build and Upload
Ensure the IR flame sensor is correctly positioned and powered.
Observe the Serial Monitor for real-time updates on flame detection ("Flame detected!" or "No flame detected.").
Example Code
Reading both outputs and requiring a sustained signal before alarming. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.
const int FLAME_ANALOG = A0;
const int FLAME_DIGITAL = 5;
const int ALARM_SAMPLES = 5; // consecutive hits before alarming
int hits = 0;
void setup() {
Serial.begin(115200);
pinMode(FLAME_DIGITAL, INPUT);
}
void loop() {
int level = analogRead(FLAME_ANALOG);
bool tripped = (digitalRead(FLAME_DIGITAL) == LOW);
// Lower analog value means more infrared, i.e. a stronger flame
Serial.print("level=");
Serial.print(level);
Serial.print(" digital=");
Serial.println(tripped ? "FLAME" : "clear");
hits = tripped ? hits + 1 : 0;
if (hits >= ALARM_SAMPLES) {
Serial.println("*** SUSTAINED FLAME DETECTED ***");
}
delay(200);
}
Applications
A ir flame sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Fire detection in workshops, server cupboards and battery-charging areas
- Flame-out supervision on gas burners and boilers
- Fire-fighting robots that steer towards a candle in a maze
- Safety interlocks that cut power or close a valve when flame is detected
- Combustion monitoring on small engines and furnaces
Working with the ESP8266 (NodeMCU)
The ESP8266 (NodeMCU) is built around the ESP8266 and runs on 3.3 V logic with roughly 80 KB usable of SRAM and 4 MB on most NodeMCU boards of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The ESP8266 is a 3.3 V part and is not 5 V tolerant — feeding a 5 V sensor output straight into a GPIO can damage the chip. Use a divider or level shifter.
There is only one ADC channel, so reading several analog sensors needs an external multiplexer such as a CD4051 or an ADS1115.
D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are strapping pins sampled at boot; holding them at the wrong level stops the board starting.
Built-in WiFi means the same sketch can publish readings to a dashboard without extra hardware.
| ESP8266 (NodeMCU) characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 3.3 V | Sensor outputs above this level need a divider or level shifter |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | a single analog channel, A0 | Determines how many analog sensors can share the board |
| PWM outputs | any GPIO via software PWM | Needed for brightness, speed and tone control |
| I²C pins | D2 (SDA, GPIO4) and D1 (SCL, GPIO5) by default | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | any GPIO except D0 (GPIO16) | Required for counting fast or asynchronous events |
| Serial | one hardware UART plus a transmit-only second port | Monitor runs at 115200 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- It triggers constantly outdoors or near a window — sunlight is the source. Shield the sensor or point it away from glazing.
- It never triggers — the potentiometer is set too far; back it off until the LED just extinguishes with no flame present.
- Range is only a few centimetres — these sensors are short-range by nature; 60–80 cm is realistic for a candle.
- The analog value rises instead of falling near a flame — some carriers invert the output; verify the direction before setting thresholds.
- The sketch compiles but the board resets or behaves erratically — a 5 V module output is being driven into a 3.3 V pin. Measure the signal before connecting it.
- Readings differ from an Arduino tutorial for the same part — the 10-bit ADC returns 0–1023, not 0–1023, so any constant copied from an Uno example needs rescaling.
Taking It Further on the ESP8266 (NodeMCU)
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the ESP8266 (NodeMCU) specifically:
The obvious extension on an ESP8266 is to stop printing to Serial and publish instead. A few lines using ESP8266WiFi and an HTTP client will push each reading to a dashboard such as ThingSpeak, or to an MQTT broker for home automation.
Running an onboard web server turns the board into its own display: serve a small HTML page that fetches the latest reading over AJAX, and any phone on the network becomes the instrument panel.
For battery operation, deep sleep is essential — the ESP8266 draws around 70 mA with WiFi active but under 20 µA asleep. Wire D0 (GPIO16) to RST so the board can wake itself, take a reading, publish and sleep again.
Notes and Practical Limits
Requiring several consecutive detections, as the sketch does, is the single most effective way to avoid false alarms. A camera flash or a passing headlight produces one sample; a real fire produces hundreds.
Never treat a hobby flame sensor as a substitute for a certified smoke or heat alarm. It is a useful trigger for automation and experiments, not a life-safety device.