Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 03: Flame Sensor Module
In this lesson, you will learn to use a flame sensor with Raspberry Pi for fire detection. We’ll show you how to connect the flame sensor to GPIO17 and write a Python script to read its output. You’ll learn to identify when the sensor detects a flame, indicated by a change in the sensor’s state. This practical project introduces you to the basics of sensor interfacing and Python coding on the Raspberry Pi, suitable for beginners interested in building safety-related projects.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi 5 |
|
Wiring
Code
from gpiozero import InputDevice
import time
# Connect the digital output of the flame sensor to GPIO17 on the Raspberry Pi
flame_sensor = InputDevice(17)
# Continuous loop to read from the sensor
while True:
# Check if the sensor is active (no flame detected)
if flame_sensor.is_active:
print("No flame detected.")
else:
# When the sensor is inactive (flame detected)
print("Flame detected!")
# Wait for 1 second before reading the sensor again
time.sleep(1)
Code Analysis
Importing Libraries
The script starts by importing the necessary classes from the gpiozero library and the time module from Python’s standard library.
from gpiozero import InputDevice import time
Initializing the Flame Sensor
An
InputDeviceobject namedflame_sensoris created, representing the flame sensor connected to GPIO pin 17 of the Raspberry Pi. This setup assumes that the digital output of the flame sensor is connected to GPIO17.flame_sensor = InputDevice(17)
Continuous Reading Loop
The script uses a
while True:loop to continuously read the sensor’s data. This loop will run indefinitely.Inside the loop, an
ifstatement checks the state of the flame sensor using theis_activeproperty.If
flame_sensor.is_activeisTrue, it indicates no flame is detected, and “No flame detected.” is printed.If
flame_sensor.is_activeisFalse, it indicates a flame is detected, and “Flame detected!” is printed.The
time.sleep(1)command pauses the loop for 1 second between each sensor reading, preventing the script from overloading the CPU.
while True: if flame_sensor.is_active: print("No flame detected.") else: print("Flame detected!") time.sleep(1)