Skip to content

ROS2 Jazzy driver for MQ-2 gas/smoke sensor via ADS1115 ADC

License

Notifications You must be signed in to change notification settings

mingyo186/mq2_gas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mq2_gas

ROS2 Jazzy driver for MQ-2 gas/smoke sensor via ADS1115 ADC (Analog).

The MQ-2 detects LPG, propane, methane, alcohol, hydrogen, and smoke. It outputs an analog voltage proportional to gas concentration, which is read via an external ADS1115 ADC. The driver converts the raw voltage to Rs/R0 ratio and approximate PPM using datasheet curve approximations.

Features

  • Publishes std_msgs/Float32 on mq2/lpg_ppm (gas concentration)
  • Publishes std_msgs/Float32 on mq2/voltage (raw sensor voltage)
  • fake_mode for testing without hardware
  • Rs/R0 ratio calculation with configurable RL, R0, VCC
  • Datasheet log-log curve approximation for LPG and smoke
  • Clean air calibration via service call
  • Runtime publish_rate change via ros2 param set

Prerequisites

  • ROS 2 Jazzy
  • Python 3
  • smbus2 (pip install smbus2) — only needed for real hardware
  • ADS1115 ADC connected to MQ-2 analog output

Installation

cd ~/ros2_ws
colcon build --packages-select mq2_gas --symlink-install
source install/setup.bash

Usage

Launch (default: fake_mode)

ros2 launch mq2_gas mq2_launch.py

Direct execution

ros2 run mq2_gas mq2_node.py --ros-args -p fake_mode:=true

Real hardware (Raspberry Pi + ADS1115)

ros2 launch mq2_gas mq2_launch.py params_file:=my_params.yaml

YAML override

mq2_gas_node:
  ros__parameters:
    fake_mode: false
    adc_channel: 0
    vcc: 5.0
    rl: 10.0
    r0: 9.8    # Calibrated R0 value

Topic echo

ros2 topic echo /mq2/lpg_ppm
ros2 topic echo /mq2/voltage

Parameters

Parameter Type Default Description
fake_mode bool true Use fake driver (no hardware)
i2c_bus int 1 I2C bus number
adc_address int 0x48 ADS1115 I2C address
adc_channel int 0 ADS1115 channel (0–3)
pga_gain int 1 ADS1115 PGA (1=±4.096V)
publish_rate double 1.0 Publishing rate (Hz)
vcc double 5.0 Supply voltage (V)
rl double 10.0 Load resistance (kOhm)
r0 double 10.0 Sensor resistance in clean air (kOhm)

Services

Service Type Description
mq2/calibrate std_srvs/srv/Trigger Collect samples in clean air, set PPM baseline
mq2/reset std_srvs/srv/Trigger Clear bias, reinitialise sensor

Package Structure

mq2_gas/
├── CMakeLists.txt
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── .gitignore
├── package.xml
├── config/
│   └── mq2_params.yaml
├── launch/
│   └── mq2_launch.py
├── nodes/
│   └── mq2_node.py
├── mq2_gas/
│   ├── __init__.py
│   └── mq2_driver.py
└── test/
    └── test_mq2_node.py

Test Results

Tested on Ubuntu 24.04 (WSL2) with fake_mode: true.

Test Category Test Result
Topics mq2/lpg_ppm publishes std_msgs/Float32 PASS
Topics mq2/voltage publishes std_msgs/Float32 PASS
Services mq2/calibrate returns success=True PASS
Services mq2/reset returns success=True PASS
Parameters publish_rate runtime change PASS
Shutdown Clean exit PASS
Linting pep257, flake8, copyright, xmllint PASS

License

MIT

About

ROS2 Jazzy driver for MQ-2 gas/smoke sensor via ADS1115 ADC

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published