Gas sensor module on A0, on an Arduino Uno
An MQ-2 gas sensor’s analog output on A0, printed twice a second: the more gas, the higher the reading. The heater takes a while to warm up first. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $29. On Mokxi they're free.
What you'll learn
- Reading an analog sensor with analogRead
- How a gas sensor's resistance changes with gas
- Printing readings to the Serial Monitor
- Powering a sensor module from 5V and GND
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 2
How it works
The MQ-2 module has a sensing element whose resistance falls when gas is near. That raises the voltage across the module's load resistor, and the module sends it out on AO to pin A0 on the Uno.
The sketch reads A0 with analogRead, a number from 0 to 1023, and prints it every half second. Drag the module's gas level up in the simulator and the reading climbs.
Parts list
2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Gas sensor, MQ-2 pin VCC | Arduino Uno R3 pin 5V |
| Gas sensor, MQ-2 pin GND | Arduino Uno R3 pin GND |
| Gas sensor, MQ-2 pin AO | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Gas sensor module on A0.
//
// module AO -> A0 VCC -> 5V GND -> GND
//
// The sensing element's resistance falls in gas, which raises the voltage
// across the module's load resistor and the reading on A0.
//
// Drag the module's gas level up.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: Gas sensor on A0");
}
void loop() {
int reading = analogRead(A0);
Serial.println(reading);
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. Serial.begin(9600) starts serial output at 9600 baud, then the sketch prints a title line to the Serial Monitor.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: Gas sensor on A0");
}loop sketch.ino
Reads A0 with analogRead, which maps 0 to 5 V onto 0 to 1023. Serial.println(reading) prints it, and delay(500) waits half a second, so you get two readings a second.
void loop() {
int reading = analogRead(A0);
Serial.println(reading);
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The reading never changes This project
Check that AO goes to A0, VCC to 5V and GND to GND. Then drag the gas level up. - The readings are high and unstable right after power on. Gas sensor
The heater needs time to warm up. Wait a few minutes, and for a fresh sensor preheat for 24 hours or more before trusting values. - The sensor reads wrong or the board drops out. Gas sensor
The heater draws about 150 mA at 5 V, so power it from 5 V with a good supply, and keep it away from breadboard rails that sag. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.