Simulator

A gas sensor simulator: an MQ-2 leak alarm on an Arduino

Mokxi is a free online MQ-2 gas sensor simulator that runs in your browser, with nothing to install and no account needed. An MQ-2 gas sensor on A0 of an Arduino Uno, with a traffic light module, a buzzer and an LCD. Drag the gas level up and the reading climbs until the alarm sounds, after the 20 second warm-up every MQ sensor needs.

  • 505parts on the bench
  • 45boards running now
  • 1.00xreal time, on every board
Arduino Uno: an MQ-2 gas leak alarmlive0.000 s 0.00x
Click to open it in the editor
The sensor warms up for 20 seconds first. Then click the circuit to open it in the editor and drag the MQ-2’s slider up: past a reading of 100 the red lamp lights and the buzzer sounds.

What you can do here

In the circuit above, or in the editor it opens.

  • Set the gas concentration from clean air up to 10,000 ppm
  • Watch the warm-up: readings that start high and fall for 20 seconds
  • Read the analog level on the serial monitor and the LCD
  • Trip an alarm with a buzzer and a red lamp
  • See hysteresis stop the alarm chattering at the threshold
  • Change the threshold in the sketch and run it again

What the running circuit shows

A warm-up, a threshold and an alarm that does not chatter.

Press Run and the reading starts in the hundreds and falls while the sketch reports that the sensor is warming up. An MQ-2 has a heater inside, and until it is hot the output means nothing, so the sketch ignores the first 20 seconds. Skip that wait in your own sketch and the alarm goes off every time you power on.

In clean air the reading settles at 11 and the monitor prints “clean gas level 11”. Drag the slider to 500 ppm and it reads 90, still clean. At 800 ppm it reads 109 and the line becomes “GAS LEAK! gas level 109”, with the red lamp and the buzzer on. At 5000 ppm it reads 226.

The alarm starts at 100 and does not stop until the reading is back under 80. That gap is hysteresis. Without it, a reading hovering between 99 and 101 would switch the buzzer on and off over and over.

How the MQ-2 is modeled

The curve, the heater and the slow recovery.

The model follows the MQ-2’s sensitivity curve for the concentration you set, the heater’s warm-up, and the way the sensor responds: it takes about 2 seconds to react to gas arriving and about 8 seconds to recover when the gas clears. So the reading lags the slider, as it would lag a real leak.

What it leaves out: humidity, which shifts a real sensor’s reading, and the difference between gases. A real MQ-2 answers to LPG, smoke, alcohol and others and cannot tell them apart. The number is a level, not a calibrated ppm. Treat this circuit as a way to get the logic right, and never trust a hobby sensor as a safety device.

How it works

Three steps from a blank tab to a running circuit.

  1. Open the editor

    It runs in your browser tab. Nothing to install, and you need no account to start.

    Your first project
  2. Wire it and write the code

    Drag parts onto a breadboard, run each wire from pin to pin, and write the sketch beside it.

    Wiring
  3. Press Run

    Your sketch compiles in the tab and runs on a model of the chip, with the Serial Monitor beside it.

    Run and serial
Reviews

What makers say about Mokxi.

“The AI checker helps check and correct my circuits, and it has way more components than Tinkercad. As a student I really appreciate the tool.”

ChloeEngineering student

“I’d been looking for a real online electronic simulator that was intuitive and complete when I found Mokxi. I was able to quickly import a Fritzing file and my Arduino sketch and start testing. Mokxi has all of the makings for the best online Arduino simulator I have been able to find!”

JayModel builderFeatured in his DioramaOS post

“I’m using Mokxi to develop and simulate an STM32-based four-wheel-drive robot car before building the actual hardware. It allows me to experiment with DC motor control, PWM, RPM measurement using photointerrupters, and different acceleration, braking, and reversing strategies, helping me understand and refine the system before implementing it in real life.”

NusretRobot car builder, Hamburg

Share your story

FAQ

Questions, answered.

Is it free?

Yes. The circuit on this page, every part in it and the editor it opens in are on the free plan, with no card.

Do I need an account?

No. The circuit runs the moment the page does, and the editor opens without one. A free account is only for saving a project to the cloud and sharing it by link.

Why does my MQ-2 read high when I first power it?

Its heater is still cold. The reading starts high and falls as it warms, which takes about 20 seconds here. Wait before you trust it, as the sketch in this circuit does.

What does the MQ-2 analog value mean?

It is a voltage that rises with gas concentration, read as a count from 0 to 1023. In this circuit clean air reads 11, 800 ppm reads 109 and 5000 ppm reads 226. It is not ppm; turning it into ppm takes the datasheet curve and a calibration.

How do I stop a gas alarm switching on and off at the threshold?

Use two thresholds. This sketch starts the alarm at a reading of 100 and only clears it under 80, so a reading that wobbles around 100 cannot toggle the buzzer.

More simulators

Each one opens on a circuit that is already running.

Set off the alarm

Open the circuit, wait out the warm-up and drag the gas level up. Nothing to install, and no account needed.