Air Pollution Monitoring System with Micro:bit

In this project, learners explore how technology can monitor air quality and protect health using a Micro:bit and air quality sensor. They measure pollution levels and program alerts for unsafe air, connecting robotics, coding, science, and environmental awareness to real-life situations at home, school, and outdoors. We have included extra challenges that encourage learners to experiment with different scenarios, making learning fun and interactive. Through this hands-on experience, learners build creativity, problem-solving skills, and confidence. The project also introduces AI ideas, showing how smart systems can predict pollution trends and help communities stay safe and healthy.

ItemPurpose
BBC Micro:bitProgrammable microcontroller for running the project
MQ-2 Gas SensorDetects gas levels like smoke, LPG, and CO
Green LEDIndicates good air quality
220Ω ResistorProtects the LED from too much current
Buzzer (active)Sounds an alert when air quality is poor
Breadboard (optional)Makes connecting components easier without soldering
Jumper WiresConnect components (male-to-female and male-to-male)
USB CableConnects the Micro:bit to your computer

Let’s bring your air quality system to life! You will connect the MQ-2 gas sensor, a green LED, and a buzzer to your BBC Micro:bit.

The Micro:bit will:

  • Read air quality data from the MQ-2 sensor

  • Light up the green LED when air is clean

  • Sound the buzzer when air quality becomes poor

Let’s learn how to connect each component.

The Green LED:

  • Long leg (+) → connects to Micro:bit Pin 1 through a 220Ω resistor

  • Short leg (–) → connects to GND on Micro:bit

The Buzzer:

  • Positive (+) → connects to Pin 0 on Micro:bit

  • Negative (–) → connects to GND on Micro:bit

MQ2 Gas Sensor:

  • VCC → connects to 3V on Micro:bit

  • Ground (GND) → connects to GND on Micro:bit

  • Analog Out → connects to Pin 2 on Micro:bit

Follow these steps carefully to open and explore the project in Microsoft MakeCode for Micro:bit:

Plug in your Micro:bit to your computer using a USB cable, then follow the steps in the video to upload your code and bring your project to life!

Code BlockWhat It Does
set air_quality to 0Creates a variable called air_quality and sets its starting value to 0. This variable stores the air quality sensor reading.
foreverRepeats the blocks inside continuously so air quality is monitored all the time.
set air_quality to analog read pin P2Reads the air quality sensor value from pin P2 and saves it in the air_quality variable.
if air_quality > 400 thenChecks if the air quality value is above 400, meaning the air is polluted or unsafe.
digital write pin P0 to 1Turns ON the warning LED or alarm connected to pin P0.
digital write pin P1 to 0Turns OFF the safe/green LED connected to pin P1.
elseRuns when the air quality value is 400 or below (air is clean).
digital write pin P0 to 0Turns OFF the warning LED or alarm.
digital write pin P1 to 1Turns ON the safe/clean air LED.
pause 1000 msWaits 1 second before taking the next air quality reading.

  • Buzzer not beeping? Check the polarity (+ and -).

  • LED not lighting? Try lowering the resistor or checking orientation.

  • No response from MQ-2? Wait 20 seconds for it to warm up.

  • How to read analog sensor data with the Micro:bit.
  • How to use LEDs and buzzers as indicators.
  • How to create an environmental monitoring system.

  • Add a red LED that turns ON when the air is polluted.

  • Display the actual air quality value on the Micro:bit LED screen.

  • Log readings over time using the serial plotter in MakeCode or Mu Editor.

Use either Micro:bit's CreateAI or Edge Impulse to build, train, and deploy machine learning (ML) models to make the air quality monitor smart and interactive. The AI can learn from sensor data and predict what might happen next.

For example:

  • Pollution Predictor: The Micro:bit observes past air quality readings and predicts when the air might become unhealthy, triggering the LEDs or buzzer earlier.

  • Fun Alert Game: Students can test different rooms or outside areas, and the AI tries to guess which areas will be safe or unsafe next.

  • Smart Display: AI can adjust the LED color or blink speed based on predicted pollution levels, teaching kids about forecasting and environmental awareness.