Twitter icon
Facebook icon
LinkedIn icon
Google icon
Reddit icon
StumbleUpon icon icon

Pollution Detecting System

Added to IoTplaybook or last updated on: 02/24/2020
Pollution Detecting System



The most prevalent problem in India and worldwide is the ever-increasing risk in air-pollution. One of the major contributions to air pollution is done by harmful emissions from automobile vehicles. Here I am trying to present a product idea that will monitor the emission of the vehicles and send alerts to the owner whenever the pollution level rises above the threshold values for the required service. Once the given time period is over, the vehicle’s ignition system is cut-off and RTO will be informed. It could also detect whether last filled fuel was adulterated or not by the wrong fuel or wrong octane rating.

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
× 1

Bolt IoT

USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
× 1


Grove - Gas Sensor(MQ2)
Seeed Grove - Gas Sensor(MQ2)
× 1


5 mm LED: Yellow
5 mm LED: Yellow
× 1


5 mm LED: Green
5 mm LED: Green
× 1


Solderless Breadboard Half Size
Solderless Breadboard Half Size
× 1



Male/Female Jumper Wires
Male/Female Jumper Wires
× 1



Male/Male Jumper Wires
× 1



Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
SMS Messaging API
Twilio SMS Messaging API
Snappy Ubuntu Core
Snappy Ubuntu Core



Pollution Detecting System

Pollution Detecting System Demo Video


Steps for connecting the sensor to the bolt IoTWi-Fi module: -

STEP 1: Connect the Vcc pin of the MQ2 sensor to the 5V pin of the Bolt IoT Wi-Fi module.

STEP 2: Connect the Gnd pin of the MQ2 sensor to the GND rail of breadboard, also connect the ground rail to the GND pin of the Bolt IoT Wi-Fi module.

STEP 3: Connect the Analog output of theMQ2 sensor to the A0 pin of the Bolt Wi-Fi module.

Steps for connecting LEDs to Bolt Wi-Fi module: -

STEP 1: Connect the cathodes of the LEDs to the ground rail of the breadboard.

STEP 2: Connect the anode of the yellow led to the D0 pin.

STEP 3: Connect the anode of the green led to the D1 pin.

Steps for writing the PYTHON code: -

STEP 1: In the code, first import the conf file which has all the credentials. Bolt python library which will fetch the data stored in Bolt cloud. The Sms library is also imported to send SMS alerts and Bolt is used for accessing data from bolt devices. The python JSON and time libraries are also imported.

STEP 2: Now initialize a variable that will store the maximum threshold value. This would send an alert if carbon content/pollution emission goes above the maximum limit.

STEP 3: To fetch data from Bolt cloud, create an object called ‘mybolt’ which can access the data from Bolt. For the Bolt Cloud to identify the bolt device, provide the API key and the Device ID when creating mybolt object. To send an SMS, create an object of the same.

STEP 4: To continuously monitor the pollution/carbon reading, enclose the logic to fetch, compare and send the SMS inside an infinite loop using the ‘while True:’ statement. To exit the loop, press CTRL+C.

STEP 5: The green led/ ignition system is turned on by the ‘digitalWrite’ function, connected to the D1 pin of Bolt.

STEP 6: The code fetches sensor value using ‘analogRead’ function which is connected to the A0 pin of Bolt.

STEP 7: The response from the Bolt cloud using the analogRead() function is in JSON format, so load the JSON data sent by cloud using Python’s JSON library.

STEP 8: The sensor value is inside a field labeled as “value” in the response. Access the JSON values using the statement ‘sensor_value = int(data[‘value’])’. This line also converts sensor reading to integer datatype.

STEP 9: This is enclosed inside a try-except block to handle any error that may occur in the code.

STEP 10: The next line of code checks if the sensor reading is above the maximum limit or not. If it exceeds, then SMS will be sent.

STEP 11:If the above statement is true then digitalWrite() is executed on the D0 pin of the Bolt, turning ON the yellow led.

STEP 12:The SMS to be sent containing the text “The Current pollution level is” followed by sensor reading and “Your vehicle needs servicing in concern with pollution emission.

STEP 13:The response from Twilio will be stored inside the ‘response’ variable.

STEP 14: The status of the message is printed on the console.

STEP 15: The statement ‘time.sleep(60)’ puts the program execution on hold for 60 seconds. This means here seconds are for the pollution service time period of the vehicle.

STEP 16: The next line code shows the turning off the ignition system/ green led using digitalWrite() function.

STEP 17: The message is sent to the RTO/owner which contains the text “The engine has been cut off since pollution service period had been expired.”

STEP 18: The statement ‘time.sleep(45)’ puts the program execution on hold for 45 seconds.


1. When there is no pollution i.e. the sensor value is below the threshold, then the green LED/ ignition system is turned ON.

Status of the system when pollution is not detected
Status of the system when pollution is not detected

2. When there is pollution i.e. the sensor value is above the threshold value, then the yellow LED is turned ON indicating the alert. Simultaneously, an alert message is sent to the owner of the vehicle to get the vehicle serviced.

Status of the sytem when pollution is detected
Status of the system when pollution is detected


Alert when pollution is detected
Alert when pollution is detected

3. Once the servicing time period in concern with pollution emission has been expired, the ignition system/engine/green Led turns OFF and the message is sent for the same to the owner and can be sent to RTO.

Status of the system when pollution service delay gets expired
Status of the system when pollution service delay gets expired




Circuit diagram of pollution detecting system

It shows all the connections of all the modules, sensor and devices.


Schematic Diagram of MQ-2 gas sensor

It shows the inner circuitry of the MQ-2 sensor.


Schematic Diagram of Bolt IoT Wi-Fi module

It shows the inner circuitry of the Bolt Wi-Fi module.



Code for whole execution of the project - Python

It is code for running the whole product system.


Import conf #fetches all the credentials
from boltiot import Sms, Bolt #imports sms & Bolt from boltiot library
import json, time #imports json & time from python library

maximum_limit = 400 #threshold of the sensor

mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID) #mybolt object fetches api key & device ID
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER) #sms object fetches SID, Auth token, To number and from number

while True: 
    print ("Reading sensor value")
    mybolt.digitalWrite('1', 'HIGH') #turns ON the green LED /ignition system
    response = mybolt.analogRead('A0') #read the output of MQ2 gas sensor
    data = json.loads(response) #convert sensor output into data
    print("Sensor value is: " + str(data['value'])) #prnt sensor value to console
    try: #used to avoid traceback errors
        sensor_value = int(data['value']) 
        if sensor_value > maximum_limit: #verifies the outout to be in limits
          response = mybolt.digitalWrite('0', 'HIGH') #turn ON yellow LED depicting requirement of service
          print(response) #print the status of output
          print("Making request to Twilio to send a SMS")
          response = sms.send_sms("The Current pollution level is " +str(sensor_value) +  "Your vehicle needs servicing in concern with pollution emission") #message sent to owner
          print("Responses received from Twilio is: " + str(response))
          print("Status of SMS at Twilio is :" + str(response.status))
          time.sleep(45) #delay for the owner to get vehicle serviced
          mybolt.digitalWrite('1', 'LOW') #turns OFF ignition system/green LED
          response = sms.send_sms("The engine has been cut off since pollution service period had been expired.") #message sent to owner/RTO on expiry of pollution service period
    except Exception as e: 
        print ("Error occurred: Below are the details")
        print (e)

Code for controlling LEDs for system

<!DOCTYPE html>
        <title>Bolt IoT Platform</title>
        <script type="text/javascript" src=""></script>
        <button style="background-color:green; height:150px; width:250px;" onclick="digitalWrite(0, 'HIGH'), digitalWrite(1, 'HIGH');"> 
<font size="7"><B>ON</B></font></button>
        <button style="background-color:red; height:150px; width:250px;" onclick="digitalWrite(0, 'LOW'), digitalWrite(1, 'LOW');">
 <font size="7"><B>OFF</B></font></button>





This content is provided by our content partner, an Avnet developer community for learning, programming, and building hardware. Visit them online for more great content like this.

This article was originally published at It was added to IoTplaybook or last modified on 02/24/2020.