Dust Sensor in Python

import grovepi
import math
import time
import datetime
import atexit


######################################

# Temperature and Humidity Sensor Pro


def getTemp():

    DHT_Sensor_Port = 4  # Sensor connected to digital port D4
    # Temperature and Humidity Sensor Type
    # Grove Base Kit comes with the blue sensor while we use a white pro sensor.
    blue = 0    # The Blue colored sensor. DHT11
    white = 1   # The White colored Pro sensor. DHT22 (High-precision)

    [temp,humidity] = grovepi.dht(DHT_Sensor_Port,white)  
        
            

    return temp, humidity


######################################

# Water Sensor

def getWater():

    Water_Sensor_Port = 3  # Sensor connected to digital port D3
    grovepi.pinMode(Water_Sensor_Port,"INPUT")
    waterReading = grovepi.digitalRead(Water_Sensor_Port)

    return waterReading

######################################

# Air Quality Sensor v1.3

def getAirQuality():

    Air_Sensor_Port = 0  # Sensor connected to analog port A0
    grovepi.pinMode(Air_Sensor_Port,"INPUT")
    airReading = grovepi.analogRead(Air_Sensor_Port)
    
    return airReading

######################################

# Gas Sensor MQ2 - Combustible Gas, Smoke

def getMQ2():

    MQ2_Sensor_Port = 1  # Sensor connected to analog port A1
    grovepi.pinMode(MQ2_Sensor_Port,"INPUT")
    gasMQ2Reading = grovepi.analogRead(MQ2_Sensor_Port)

    # Calculate gas density - large value means more dense gas
    densityMQ2 = (float)(gasMQ2Reading / 1024)

    return gasMQ2Reading, densityMQ2

######################################

# Gas Sensor MQ9 - Carbon Monoxide, Coal Gas, Liquefied Gas

def getMQ9():

    MQ9_Sensor_Port = 2  # Sensor connected to analog port A2
    grovepi.pinMode(MQ9_Sensor_Port,"INPUT")
    gasMQ9Reading = grovepi.analogRead(MQ9_Sensor_Port)

    # Calculate gas density - large value means more dense gas
    densityMQ9 = (float)(gasMQ9Reading / 1024)

    return gasMQ9Reading, densityMQ9

######################################

# Dust Sensor

def getDust():

    #Dust sensor connected to digital port D2 - the only port it will work with
    atexit.register(grovepi.dust_sensor_dis)
    grovepi.dust_sensor_en()
    # The dust sensor takes 30 seconds to update the new values
    # the fist byte is 1 for a new value and 0 for old values
    # second byte is the concentration in pcs/0.01cf

    [new_val,lowpulseoccupancy] = grovepi.dustSensorRead()

    return new_val, lowpulseoccupancy

######################################


#Read above sensors in loop
while True:

    print ("----------------------")
    
    dateString = '%d/%m/%Y %H:%M:%S'
    print datetime.datetime.now().strftime(dateString)

    #Temperature & Humidity
    [temp,humidity] = getTemp();
    if math.isnan(temp) == False and math.isnan(humidity) == False:
        print("Temperature = %.02fc Humidity = %.02f%%"%(temp, humidity))


    #Water
    water = getWater();
    #print "Water Detected? = ", water

    
    if water == 0:
       print ("Water Sensor Dry")
    elif water == 1:
       print ("Water Detected")
    else:
       print ("Check Water Sensor")


    #Air Quality
    air = getAirQuality();

    if air > 700:
       print ("High pollution")
    elif air > 300:
       print ("Low pollution")
    else:
       print ("Air is fresh")

    print "Air Quality = ", air


    #MQ2 - Combustible Gas, Smoke

    [gasMQ2Reading,densityMQ2] = getMQ2();
    print("MQ2 Reading = %.02f MQ2 Density = %.02f%%"%(gasMQ2Reading, densityMQ2))


    #MQ9 - Carbon Monoxide, Coal Gas, Liquefied Gas

    [gasMQ9Reading,densityMQ9] = getMQ9();
    print("MQ9 Reading = %.02f MQ9 Density = %.02f%%"%(gasMQ9Reading, densityMQ9))


    #Dust

    [new_val,lowpulseoccupancy] = getDust();
    if new_val:
        print(lowpulseoccupancy)

    #Time between sensor readings (seconds)
    time.sleep(10)




We have a PR waiting on this issue.
It may fix what you’re encountering.

I’ll be back with an answer as soon as I get on it.

Thank you!

Hi everyone,

It seems that this issue still isn’t resolved with firmware version 1.2.7. Can anyone give an update on this?

Hi @d_cyphert,

Just saw the PR is still open. We’ll try the following days to approve it and hence bump up the version number.

Though, I have to mention, I never personally had an issue with this sensor - so I never encountered the issue described above.

Thank you!