How to use a flame sensor with an Arduino
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
Wire VCC to 5 V, GND to GND and DO to any digital pin, then read it with digitalRead(). DO goes LOW when the module sees a flame and stays HIGH the rest of the time. That one backwards fact is most of what there is to know, and it is the reason many first flame alarms go off all day.
The circuit above is a flame sensor module on pin 2 of an Uno, and a red LED on pin 13. Press Run and drag the slider on the sensor to bring a lighter’s flame closer. With the trimmer in the middle, as it comes out of the bag, the LED stays dark at 52 cm and lights at 50 cm. Pull the flame back out and the LED goes out again.
The sketch running there is the plain button example, not a flame sketch at all. To pin 2, a flame sensor’s DO pulling down to ground looks exactly like a pressed button, so the same code works. Below are the parts, the wiring, a real flame alarm with a buzzer, and what the analog pin is good for.
What you need
- An Arduino Uno
- A flame sensor module (the four-pin KY-026 style, or the three-pin board with DO only)
- An active buzzer, or an LED and a 220 ohm resistor
- A breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| Flame sensor VCC | 5 V | Works from 3.3 V to 5 V |
| Flame sensor GND | GND | |
| Flame sensor DO | Pin 2 | LOW when a flame is close enough |
| Flame sensor AO | A0 | Optional: lower means more infrared |
| LED, through 220 ohm | Pin 13 | Or an active buzzer on pin 8 |
What the flame sensor really sees
The black part sticking out of the board is not a fire detector. It is an infrared receiver behind a dark lens that blocks most visible light, and it looks for light between about 760 and 1100 nm. A flame gives off plenty of that, which is why it works, but so does the sun, a hot stove or an old light bulb.
The receiver sits under a 10k pull-up, so more infrared pulls AO lower. A comparator chip on the board, an LM393, compares AO with the little blue trimmer and pulls DO to ground when AO drops below it. The second lamp on the board lights at the same moment, which makes it easy to check your trimmer setting without any code.
A flame alarm with a buzzer
This sketch reads DO on pin 2 and sounds an active buzzer on pin 8 while there is a flame. It only prints when the answer changes, so the serial monitor is not flooded, and each line carries the AO reading at that moment. We wired it to a flame sensor and a buzzer on a simulated Uno and moved the flame from out of sight to 40 cm and back. It printed "flame alarm ready", then "FLAME! 205", then "no flame 1023", and the buzzer sounded only in between.
Use pinMode(FLAME_DO, INPUT), not INPUT_PULLUP. The module already has its own pull-up on DO. INPUT_PULLUP does no harm, which is why the button sketch above still works, but it is not needed.
const int FLAME_DO = 2;
const int FLAME_AO = A0;
const int BUZZER = 8;
bool burning = false;
void setup() {
pinMode(FLAME_DO, INPUT);
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
Serial.println("flame alarm ready");
}
void loop() {
// DO goes LOW when the module sees a flame.
bool now = digitalRead(FLAME_DO) == LOW;
if (now != burning) {
burning = now;
Serial.print(burning ? "FLAME! " : "no flame ");
Serial.println(analogRead(FLAME_AO));
}
digitalWrite(BUZZER, burning ? HIGH : LOW);
delay(50);
}Range, and the trimmer
The trimmer sets how much infrared counts as a flame, so it sets the range. In the simulator it is the threshold property of the part, 0.5 by default, which is the middle of its turn. At 0.5 a lighter trips DO at 50 cm. Turn it up to 0.8 and it reaches out to 75 cm, but not to 80. Turn it down to 0.2 and the flame has to come to 35 cm.
More range is not always better. A sensor turned all the way up on a real bench sees a sunny window or a warm radiator as a flame. Set it on the spot where it will live: turn the trimmer until the DO lamp just goes out with no flame, then back it off a little.
What AO is good for
AO is the receiver’s raw voltage, and the light it gets falls with the square of the distance. We read it with the sketch below at each distance in turn. With no flame it read 1023. A flame at 120 cm read 932, at 100 cm 892, at 80 cm 818, at 60 cm 659, at 50 cm 499 and at 40 cm 205. At 30 cm and closer it read 20 every time, because the receiver is saturated.
So AO can tell "far" from "near", and that is about all. It cannot give you a distance, it is pulled around by any other infrared in the room, and closer than about 30 cm every flame looks the same. That is why almost every flame sketch, including the alarm above, makes its decision from DO.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(500);
}What the simulation leaves out
Here the only infrared in the world is the flame on the slider. A real module also sees sunlight, incandescent bulbs, a TV remote and a warm heater up close, and it looks out in a cone of about 60 degrees rather than straight ahead. The flame here is steady, where a real one flickers. Test a real sensor in the room where it will be used, with the lights and the sun as they normally are.
Common mistakes
Reading DO backwards. LOW means flame. If your alarm sounds all the time and stops when you light a match, flip the test.
Trusting it as a fire alarm. This is a toy sensor for robots and science projects. A real smoke alarm is tested and certified for the job, and a hobby module is not.
Pointing it at a window. The sun is full of infrared, and a real module trips on it.
Expecting AO to give a distance. It saturates at about 30 cm and drifts with any other infrared in the room.
Leaving the trimmer at full. Turn it until the DO lamp just goes out with nothing there, then a little more.
Questions
How do I connect a flame sensor to an Arduino?
VCC to 5 V, GND to GND and DO to a digital pin such as pin 2. Read it with digitalRead(): LOW means a flame is in range. AO can go to an analog pin such as A0 if you want the raw reading as well.
Why does my flame sensor read LOW when there is a flame?
The comparator on the board pulls DO down to ground when it sees enough infrared, and a resistor holds it high the rest of the time. So LOW means flame and HIGH means no flame.
How far away can a flame sensor detect fire?
In the simulator, with the trimmer in the middle, a lighter trips it at 50 cm, and with the trimmer at 0.8 at 75 cm. A bigger flame reaches further. On a real bench the room’s other infrared moves those numbers.
Can I use the flame sensor without the digital pin?
Yes. Read AO with analogRead() and pick your own threshold. Lower means more infrared: 1023 with no flame and 499 at 50 cm in the simulator. Most sketches still use DO, because the trimmer does the same job without any code.
Can I test a flame sensor without a real flame?
Yes, in the circuit at the top of this page: the slider on the sensor moves a lighter’s flame closer or further away. With the trimmer in the middle, the LED stayed dark at 52 cm and lit at 50 cm.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.