Raspberry Pi Pico - Flame Sensor

This tutorial instructs you how to use a Raspberry Pi Pico and a flame sensor to detect and measure flames and fire. We will cover these topics:

Raspberry Pi Pico flame sensor

Then, you can adjust the code to activate a warning horn (using a relay) when it detects fire.

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Flame Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Flame Sensor

The flame sensor can sense and measure infrared light from a flame, useful for fire detection. It is also called an infrared flame or fire sensor. This sensor provides two kinds of signals: a digital output (LOW or HIGH) and an analog output.

Infrared flame sensors are designed to detect certain infrared radiation wavelengths emitted by flames. They aim to minimize false alarms from other infrared sources such as human body heat or lights. Yet, these sensors can sometimes make errors, either by signaling an alarm for something that isn't there or failing to detect actual flames.

Pinout

The flame sensor has four metal parts called pins.

  • VCC pin: Connect this to VCC (3.3V to 5V).
  • GND pin: Connect this to GND (0V).
  • DO pin: This pin gives a digital output. It is HIGH when there is no flame and LOW when there is a flame. You can adjust the sensitivity to flames using a potentiometer on the board.
  • AO Garage Pin: This pin sends out an analog signal. The signal value decreases as the infrared level drops and increases as the infrared level rises.
Flame Sensor Pinout
image source: diyables.io

It also includes two LED lights.

  • One PWR-LED light indicates when the power is on.
  • One DO-LED light indicates when a flame is detected.

How It Works

Regarding the DO pin:

  • The module has a potentiometer to set the infrared threshold (sensitivity).
  • When the infrared level is above the threshold, it detects a flame, the sensor’s output pin becomes LOW, and the DO-LED turns on.
  • When the infrared level is below the threshold, no flame is detected, the sensor’s output pin is HIGH, and the DO-LED remains off.

For the AO pin:

  • If there is a lot of infrared light, the AO pin reading will be high.
  • If there is a little infrared light, the AO pin reading will be low.

The potentiometer does not alter the value at the AO pin.

Wiring Diagram

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

The wiring diagram between Raspberry Pi and Pico Flame Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Read value from DO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-flame-sensor """ from machine import Pin import utime # For timing functions DO_PIN = Pin(0, Pin.IN) # The Raspberry Pi Pico pin GPIO0 connected to the DO pin of the flame sensor module while True: flame_state = DO_PIN.value() # Read the digital value from the pin if flame_state == 1: print("The flame is NOT present => The fire is NOT detected") else: print("The flame is present => The fire is detected") utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the flame sensor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Aim the flame sensor at a flame.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The flame is present => The fire is detected The flame is present => The fire is detected The flame is NOT present => The fire is NOT detected The flame is NOT present => The fire is NOT detected The flame is NOT present => The fire is NOT detected The flame is present => The fire is detected The flame is present => The fire is detected The flame is present => The fire is detected
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If the LED light is always on or off, even when the sensor is facing a flame, you can turn the potentiometer to change the sensor's sensitivity.

Raspberry Pi Pico Code - Read value from AO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-flame-sensor """ from machine import ADC, Pin import utime # For timing functions # Define the Raspberry Pi Pico pin pin connected to the AO pin of the flame sensor module AO_PIN = ADC(Pin(28)) # GPIO28 (ADC2) as an analog input pin while True: flame_value = AO_PIN.read_u16() # Read the analog value (0-65535) print(flame_value) # Print the analog value utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your Raspberry Pi Pico.
  • Click the green Run button (or press F5) to execute the script.
  • Aim the flame sensor at a flame.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 145 206 226 513 697 959 965 1005 1006 1016 1019 661 545 341 172
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!