Template: MQ-2 Gas Sensor Pico Guide

From Waveshare Wiki
Revision as of 07:41, 9 October 2021 by Waveshare-eng11 (talk | contribs) (Created page with "==Configure Pico== MicroPython and C examples are provided for this sensor, to use it with Pico, you need to first flash firmware to the Pico according to the example.<br /> P...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Configure Pico

MicroPython and C examples are provided for this sensor, to use it with Pico, you need to first flash firmware to the Pico according to the example.
Please refer to the guides of Raspberry Pi about how to flash the firmware. We recommend you use the firmware from the Demo codes archive.

Hardware connection

Hardware Connection-Pico
MQ-2 Pico Description
VCC 3.3V Power input
GND GND Ground
AOUT GP26 Analog data output
DOUT GP22 Digital data output

Examples

Download the example

Open the terminal of Raspberry Pi and run the following command to download the example:

sudo apt-get install p7zip-full
cd ~
sudo wget https://www.waveshare.com/w/upload/f/f2/MQ-2-Gas-Sensor-code.7z
7z x MQ-2-Gas-Sensor-code.7z -o./MQ-2-Gas-Sensor-code
cd ~/MQ-2-Gas-Sensor-code
cd Pico/c/build/

C

Here we use the Raspberry Pi board to flashing the Pico.

  • Compile the c examples
    • Go into the directory of C examples
cd ~/MQ-2-Gas-Sensor-code/Pico/c/
  • Go into the build folder and add the sdk; ../../pico-sdk is the path of the SDK, if may be different if you have saved the SDK in other path.
cd build
export PICO_SDK_PATH=../../pico-sdk
  • Generate Makefile by cmake command
cmake ..
  • Compile the codes by command make
make -j9
Note: If you use Pi zero, please run make only.
  • After compiling, a uf2 file is generated.
    • Hold the button of Pico board, connect the Pico board to Raspberry Pi by USB cable
    • After connecting, release the button and a portable disk (RPI_RP2) is recognized.
    • Copy the main.uf2 file generated which locates in build folder to the portable disk (RPI-RP2)
cp main.uf2 /media/pi/RPI-RP2/

Python examples

In windows PC
  • Hold the BOOTSET button of the PICO board, connect the Pico board to Raspberry Pi by USB cable
    • After connecting, release the button and a portable disk (RPI-RP2) is recognized.
  • Copy the rp2-pico-20210418-v1.15.uf2 file to the portable disk (RPI-RP2).
  • Open the Thonny IDE (Please install the newest version which supports Pico board or update).
  • Choose Tools -> Options ->Interpreter, choose the Pico and the port
Pico-lcd-0.96-img-config.png
  • Download the demo codes, unzip and find the MicroPython example
  • Choose File -> Open -> MQ2 Gas Sensor.py and run it.
Pico-lcd-0.96-img-run.png
In Raspberry Pi
  • Flash the uf2 file to the Pico board just like in the Windows PC
  • Open the Thonny IDE of Pi, make sure that it is the newest version, or update it.
  • Choose Tools -> Options... -> Interpreter
    • Choose Pico and the Port
Pico-lcd-0.96-img-config2.png
  • If your Thonny IDE cannot support the Pico board, you can update it and try again.
sudo apt upgrade thonny
  • Choose File -> Open... -> python/MQ2 Gas Sensor.py and run it.

Expected result

  • The sensor needs to preheat for about a minute to heat the internal sensitive materials of the sensor.
  • Put the sensor inside the device containing sensitive gas, and the indicator on the module lights up. The indicator goes off when the sensor is removed from the device.