Skip to content

Search the documentation

Kit
Firmware

Gas alarm — Smart Home

The board sniffs the air with the MQ135 gas sensor every two seconds. While the air is clean it just reports the number. When the number climbs past 1500 it shouts: the RGB LED lights up and the buzzer plays the alarm tune.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Gas alarm armed"
  • repeat forever
    • wait 2 seconds
    • Print to serial (create text with "Gas: " Read Gas Sensor (MQ135))
    • Set RGB LED R: 0 G: 0 B: 0 .
    • if (Read Gas Sensor (MQ135) > 1500)
      • Print to serial "BAD AIR!"
      • Set RGB LED R: 255 G: 0 B: 0 .
      • Play sequence : Alarm
  • A Smart Home board with the MQ135 gas sensor, the RGB LED and the buzzer.
  • A USB cable and the serial monitor.
  • Something smelly and safe to test with: a felt-tip marker, a bottle of vinegar, or an adult’s lighter held unlit near the sensor for a second. Never light anything near the board.
  1. Open 03-gas-alarm.mobot in MoBot.
  2. Plug in, pick the port, press Send to Device.
  3. Open the serial monitor and watch the numbers for half a minute first — that is your “clean air” reading.
  • [MOBOT] Gas alarm armed once.
  • Then [MOBOT] Gas: 400 (or whatever your room is) every two seconds. The number is a raw sensor reading between 0 and 4095 — it is not a smell in any unit, just “bigger means dirtier”.
  • Hold the open marker under the sensor. The number climbs, and when it goes over 1500:
    • [MOBOT] BAD AIR! appears,
    • the RGB LED lights up,
    • the buzzer plays a wailing two-tone alarm.
  • Take the marker away, wait a few seconds, and the LED goes dark again and the alarm stops.

If the alarm never fires, your room’s clean-air number is probably already near 1500, or the marker is too weak. Look at the printed numbers, pick a value a few hundred above the resting one, and type it into the 1500 block. That is the whole point of printing the number.

A brand-new MQ135 reads high for its first few minutes and settles as it warms up. Give it five minutes before deciding it is broken.

The blocks say R:255 G:0 B:0 (each colour goes from 0 to 255), which is how a child spells red, and the Python MoBot writes (see The Python code below) passes red first too:

home.rgb_led.set_color(255, 0, 0)

(On firmware 2.x the colours were in percent, so the same block wrote mobotIOT.rgb_led.write(round(255 * 100 / 255), round(0 * 100 / 255), round(0 * 100 / 255)).)

The LED must glow red. Every Smart Home board has its red and green LED pins the other way round from the silkscreen; firmware 2.0 and later correct that in software, and the Set RGB LED block passes red, green, blue in that order.

If your alarm LED comes on green, the board is running firmware older than 2.0. Flash the current firmware; do not “fix” the example.

Why the wait is at the top, and why there is no “otherwise”

Section titled “Why the wait is at the top, and why there is no “otherwise””

The Play sequence block starts its tune and goes on at once, without waiting for it to finish. So:

  • the wait 2 sits at the top of the loop: each round pauses first, and the two seconds give the alarm tune time to finish before the next reading, and
  • the LED is switched off before the if, so every round starts dark and the if turns it red only when the air is bad. There is no “otherwise” branch to build.
block what it does
iot_gaz Reads the MQ135. A number from 0 to 4095.
logic_compare + controls_if “If the gas is bigger than 1500, do this.”
iot_rgb_color Sets the colour of the RGB LED, 0–255 per colour.
iot_playsequence Plays one of 23 ready-made tunes. Try siren or warning.
iot_print, text_join The running commentary in the serial monitor.
wait Two seconds — long enough for the whole alarm tune to finish.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

03-gas-alarm.firmware-3.main.py
# Gas alarm — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 03-gas-alarm.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 03-gas-alarm.main.py.
from machine import Pin
import time
from sounds import SEQUENCES
print("[MOBOT]", 'Gas alarm armed')
while True:
time.sleep(2)
print("[MOBOT]", ('Gas: ' + str(home.gas_sensor.level())))
home.rgb_led.set_color(0, 0, 0)
if home.gas_sensor.level() > 1500:
print("[MOBOT]", 'BAD AIR!')
home.rgb_led.set_color(255, 0, 0)
home.buzzer.play_notes(SEQUENCES.get('alarm'))
  • Swap alarm for siren, warning or doorbell in the Play sequence block.
  • Make the LED blue instead (R:0 G:0 B:255) — blue is the one colour the swap above cannot spoil.
  • Lower 1500 until the alarm fires on ordinary room air, then raise it again.