Smoke sensor with micro:bit
This is the MQ-2 smoke sensor module. It is a device that is sensitive to flammable gases such as LPG, propane, and methane.
In this guide, we will learn to connect it to the micro:bit and program it to sound an alarm, if it goes above the threshold level. When it is below the threshold level, a heart icon will be displayed instead.
Complete this guide to understand the basics on how to use a smoke sensor.
|Parts Used in This Guide|
Step 1 The Module
Let's take a look at the smoke sensor module!
It has four pins:
- Analog Output (AO)
- Digital Output (DO)
- Ground (GND)
- Voltage Common Collector (VCC)
Step 2 Connect module to breadboard
Step 3 Connect + to VCC
Step 4 Connect GND to GND
Step 5 Connect P5 to DO
Step 6 Connect P1 to AO
Step 7 Connect + to VCC (Buzzer module)
Step 8 Connect P0 to I/O
Step 9 Connect GND to GND
Step 10 Connect + to 3.3V
Step 11 The MakeCode!
- We will get started with programming it using the MakeCode editor.
- We want to set it up so that:
- A heart icon is displayed on the micro:bit's 5X5 LED matrix when all is well.
- A sad smiley face icon is displayed as well as an alarm indicates that harmful gases are detected.
- First, create a variable and call it 'sensorVal'.
- As the analog pin of the smoke sensor is connected to Pin 1 of the micro:bit, the 'analog read pin' block's value is set to P1.
- Create another variable, and call it 'threshold', set its value to 800.
- Get an 'if... then ... else ... ' block, and place this after 'set threshold to 800'
- Create a condition. If 'sensorVal' is above threshold:
- Play a High C tone for 1 beat and show a sad face icon.
- Else, show a heart icon.
- Note: Although a threshold level has been set, this does not mean the device has been calibrated! Calibration is a safety procedure that ensures the detectors are measuring the correct level of flammable gas. But playing around with flammable gases is not something you should try at home, so we will not cover calibration today.
Step 12 Upload the code
- Connect the micro:bit to the computer using a microUSB cable
- Download the code by clicking on the 'Download' button in MakeCode editor
- Find the downloaded hex file in your Downloads folder
- Drag and drop it into the MICRO:BIT drive
- The micro:bit will flash as the code is being uploaded