Up to date guide for MQ-2 gas sensor usage with Raspberry Pi 4 model B.

MQ-2 module.
Sensor’s diagram.
Breadboard with MQ-2 sensor, a Logic Level Converter and an ADC.
Voltage divider formula.
Circuit for voltage division.
MQ sensor with voltage division at the analog output.
MCP3008 adc
Names of the MCP3008 legs
  • MCP3008 VDD to Raspberry Pi 3.3V
  • MCP3008 VREF to Raspberry Pi 3.3V
  • MCP3008 AGND to Raspberry Pi GND
  • MCP3008 DGND to Raspberry Pi GND
  • MCP3008 CLK to Raspberry Pi pin 18
  • MCP3008 DOUT to Raspberry Pi pin 23
  • MCP3008 DIN to Raspberry Pi pin 24
  • MCP3008 CS/SHDN to Raspberry Pi pin 25
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D5)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create an analog input channel on pin 0
chan = AnalogIn(mcp, MCP.P0)

print('Raw ADC Value: ', chan.value)
print('ADC Voltage: ' + str(chan.voltage) + 'V')

--

--

--

I am a pharmacist turned android developer and machine learning engineer. Right now I’m a senior android developer at Invisalign and a ML GDE.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Africa COVID-19 Response Toolkit:Data collection

How to Analyse Whatsapp Messege using Python.

GPT-3 Explains Things To Me: AppleScript vs. JXA

GPT-3, your bot ready to teach ya

Julia Examples for beginners

Learning a New Programming Language: Take the Plunge!

Build/Test/Deploy your Go App using IBM Cloud Toolchain

#1 Reason Why You Don’t Improve As a Software Developer

Why is coding creative ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
George Soloupis

George Soloupis

I am a pharmacist turned android developer and machine learning engineer. Right now I’m a senior android developer at Invisalign and a ML GDE.

More from Medium

Remote access PostgreSQL in Raspberry Pi

Building a Raspberry PI home server

MQ-2, MQ-3, MQ-4, MQ-135, MQ-6, MQ-7, MQ-8, MQ-9 air sensors combined together in a Raspberry…

How to securely connect to your servers?