Flame sensor on pin 2 lights the LED on 13, on an Arduino Uno
A flame sensor module on pin 2: a flame in range pulls its digital output LOW, and the LED on pin 13 lights. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $31. On Mokxi they're free.
What you'll learn
- Reading a digital sensor with digitalRead
- Active-low signals: LOW means detected
- Reacting only when a state changes
- Using the built-in LED on pin 13
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The flame sensor has an infrared photodiode tuned to the light a flame gives off. It feeds a comparator, and when a flame is close enough, the digital output DO goes LOW. DO is wired to pin 2.
The sketch reads pin 2 every 10 ms. A LOW reading means a flame, so it turns on the Uno's LED on pin 13. It prints "flame!" or "no flame" only when the state changes.
Parts list
3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Flame sensor
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Flame sensor pin GND | Arduino Uno R3 pin GND |
| Flame sensor pin DO | Arduino Uno R3 pin 2 |
| Flame sensor pin VCC | Arduino Uno R3 pin 5V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Flame sensor on pin 2 lights the LED on 13.
//
// sensor DO -> pin 2 VCC -> 5V GND -> GND
//
// An infrared photodiode tuned to a flame's light feeds a comparator; close enough, DO goes LOW.
//
// Drag the flame closer.
const int SENSOR = 2;
const int LED_PIN = 13;
int last = -1;
void setup() {
pinMode(SENSOR, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: flame sensor on pin 2, the LED on pin 13");
}
void loop() {
int now = digitalRead(SENSOR) == LOW ? 1 : 0;
digitalWrite(LED_PIN, now ? HIGH : LOW);
if (now != last) {
last = now;
Serial.println(now ? "flame!" : "no flame");
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets the sensor pin as an input and the LED pin as an output. It opens the serial port at 9600 and prints a short hello.
void setup() {
pinMode(SENSOR, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: flame sensor on pin 2, the LED on pin 13");
}loop sketch.ino
Reads the sensor and turns the signal around: LOW becomes 1, meaning flame. It writes that to the LED, then compares it with last. Only when it differs does it print a message, so the serial monitor is not flooded. A 10 ms delay ends each pass.
void loop() {
int now = digitalRead(SENSOR) == LOW ? 1 : 0;
digitalWrite(LED_PIN, now ? HIGH : LOW);
if (now != last) {
last = now;
Serial.println(now ? "flame!" : "no flame");
}
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is on when there is no flame This project
The sensor is active low. Check that DO is on pin 2 and that the sketch treats LOW as a flame. - Nothing ever triggers This project
Check the sensor's 5 V and ground, and bring the flame closer. - The sensor triggers all the time or never, even with a flame. Flame sensor
Turn the potentiometer to set the comparator threshold. DO goes low when a flame is detected, so test for LOW. Sunlight and incandescent bulbs also give infrared and cause false triggers. - Digital output is correct but the analog values look backwards. Flame sensor
AO usually falls as the flame gets closer or stronger. Read it with a pull and check VCC and GND pins. Power with 3.3 V to 5 V and match your board's logic level. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.