Today We will learn how to make raspberry pi Gas and Smoke Alarm using mq2 gas sensor raspberry pi. Here we will use a buzzer for the signal of alarm. Whenever there is a gas or smoke detector in the atmosphere, the buzzer will be a sound.
You have to need flowing component for making raspberry pi Gas and Smoke Alarm.
- Raspberry Pi
- MQ-2 gas and smoke sensor
- MCP 3008 chip
- pi zo buzzer
- 5 mm LED
- 330 Ω resistor
- Push Button
- Jumper wires
≡MQ-2 Introduction to gas and smoke sensors:
MQ-2 gas and smoke sensitive and this sensor Sensitive to the following combustible gases: propane, butane, methane, alcohol and hydrogen. Figure 1 shows the front and rear sides of the sensor.
Has two pins to output the gas label or value. The first is to read the gas concentration in the atmosphere and get the output from the analog output pin A0 as an analog signal, where the higher the gas level, the higher the output voltage.
The second is that when the digital pin gas reaches a certain level, the D0 pin is high and if the gas level is below a certain level, the D0 pin is low. A potentiometer is attached to the back of the sensor, which can be turned to control the gas level.
There is also a power LED on the back of the sensor that indicates if the sensor is on, and there is an LED to detect digital output when the gas level is above the specified edge.
We know that Raspberry Pi can only take digital signals as input we need to convert the analog signal and digital signal of the sensor when we provide the input of the sensor’s analog signal Raspberry Pi. In this case, we need to use MPC3008 ic to convert analog signal and digital signal shown in 3 in the previous project.
≡Introduction to Pizzo buzzer:
Any digital signal from Raspberry Pi is used to make a sound in Pizzo buzzer. You will use the buzzer shown in Figure 2.
a disk that vibrates at a certain frequency when voltage is sent. The piezo buzzer is very easy to combine with raspberry pi. Only one leg needs to be connected to the GND pin of the raspberry pi and the other to the GPIO pin.
≡ Making a Circuit Diagram
Smoke and gas detector alarm circuit, an LED and a pushbutton must be attached to the raspberry pi. The piezo buzzer and the MQ-2 sensor need to be connected to the Raspberry Pi via the MCP3008 chip. Follow these instructions using Figure 3 as a reference.
- Connect GND with blue breadboard rail and 3.3 V with red rail.
- Place the MCP3008 chip in the centre of the breadboard so that the legs are divided equally on either side of the two centre divisions, as shown in Figure 3, and provide connections according to the table below.
|1||MQ-2 AO pin|
3.MQ-2 Sensor Connect the raspberry pi according to the following table.
|MQ-2 SENSOR||CONNECT TO|
|AO||MCP3008 pin 1|
- Add an LED to the breadboard and register the 330 GPIO 17 with a positive pin and GND rail. Attach the negative pin.
- Place the pushbutton in the middle of the breadboard, as shown in the figure. Connect the bottom-right pin of the GND power rail and the left pin below the GPIO 2, ensuring that both connected pins are on the same side of the partition.
- Place the market on the breadboard and connect the black wire to the GND and the red wire to the GPIO 27.
≡ Script Writing mq2 gas sensor raspberry pi code:
Now take out the python IDE of Raspberry Pi and write the following mq2 gas sensor raspberry pi python code program there and when you finish writing it, it will be smoke_detector.py Name must be saved in the sensor folder.
#import necessary libraries ➊ from gpiozero import LED, Button, Buzzer, MCP3008 from time import sleep ➋ led = LED arm_gas_sensor(): global gas_sensor_status if gas_sensor_status == True: gas_sensor_status = False led.off () else: gas_sensor_status = True led.on () ➏ button.when_pressed = arm_gas_sensor ➐ while ➑ #print True:(gas_sensor.value) #check if the gas sensor is armed and #reached the threshold value if(gas_sensor_status == True and gas_sensor.value> threshold): buzzer.beep () else: buzzer.off () sleep (2)
≡Script Analysis mq2 gas sensor raspberry pi python code:
➊ from gpiozero import LED, Button, Buzzer, MCP3008 from time import sleep
gpiozero to control the required LED (LED) library, button library for button switch And MCP3008 libraries have been imported to convert digital signals to analog signals. And imported the sleep library for time control.
Led = LED (17) button = Button (2) buzzer = Buzzer (27) gas_sensor = MCP3008 (0)
Creating LED, Button, Buzzer, Gas_sensor objects to store LEDs, Buttons, Markets, and Gas Sensors Done.
➌ gas_sensor_status =False
A variable named Gas_sensor_status has been created that will determine the state of the sensor. If the sensor detects gas, the variable will be True, and if it does not detect gas, the variable will be false. You have to set the value of gas. If the sensor is better than that value, the market will beef.
➍ threshold = 0.1
Here a threshold value is specified.
➎ def arm_gas_sensor (): global gas_sensor_status if gas_sensor_status == True: gas_sensor_status = False led.off () else: gas_sensor_status = True led.on ()
Here a function called arm_gas_sensor () is created where the sensor Will is on, or here if-else condition is used if the condition is true, then the LED will be off, and if the condition is not true, then the LED will be on. Based on this condition you can change your LED according to your needs.
➏ button.when_pressed = arm_gas_sensor
The push-button is pressed when the function call arm_gas_sensor
➑ if (gas_sensor_status == True and gas_sensor.value
Of the sensor The value will be higher than the threshold value will give the market beef; otherwise, it will be closed.
run, first, you have to save or save the above program, then you have to run Run ▸ Run current script or press F5 from the keyboard then the program will run.
Press the pushbutton to arm the sensor. The red LED should light up. Then test it out by using a lighter to release some gas next to the sensor until the buzzer beeps. Press the push button. Now the red LED will light up. Then test it using a lighter to release some gas next to the sensor until the buzzer beeps.
1 total views, 1 views today