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.
- Publishes
std_msgs/Float32onmq2/lpg_ppm(gas concentration) - Publishes
std_msgs/Float32onmq2/voltage(raw sensor voltage) fake_modefor 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_ratechange viaros2 param set
- ROS 2 Jazzy
- Python 3
smbus2(pip install smbus2) — only needed for real hardware- ADS1115 ADC connected to MQ-2 analog output
cd ~/ros2_ws
colcon build --packages-select mq2_gas --symlink-install
source install/setup.bashros2 launch mq2_gas mq2_launch.pyros2 run mq2_gas mq2_node.py --ros-args -p fake_mode:=trueros2 launch mq2_gas mq2_launch.py params_file:=my_params.yamlmq2_gas_node:
ros__parameters:
fake_mode: false
adc_channel: 0
vcc: 5.0
rl: 10.0
r0: 9.8 # Calibrated R0 valueros2 topic echo /mq2/lpg_ppm
ros2 topic echo /mq2/voltage| 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) |
| 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 |
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
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 |
MIT