Using Pm2.5 Sensor With Raspberry Pi


Introduction: Using Pm2.5 Sensor With Raspberry Pi

This tutorial shows how to connect a pm2.5 sensor with raspberry pi.

SDS021 is used as the pm2.5 sensor, SDS011 sensor should be the same way.

Step 1: Edit the System File for Using the Serial Port

  • Set the debug serial port to normal serial port in raspberry pi
  1. Edit file /boot.cmdline.txt, delete the red characters

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

After deleted, the result should be something like :

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Save and exit this edit

  1. Edit file /etc/inittab

Open the file and coment the last line of the file:

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

  1. Power off raspberry pi

Step 2: Connect the Sensor

Connect the sensor as the image shows

Step 3: Coding

Python code

# -*- coding: utf-8 -*-
import serial import time t = serial.Serial('/dev/ttyAMA0',9600) t.setTimeout(2) while True: time.sleep(0.5) retstr = if len(retstr)==10: if(retstr[0]==b"\xaa" and retstr[1]==b'\xc0'):#check header char checksum=0 for i in range(6): checksum=checksum+ord(retstr[2+i])#check checksum if checksum%256 == ord(retstr[8]): pm25=ord(retstr[2])+ord(retstr[3])*255 pm10=ord(retstr[4])+ord(retstr[5])*255 print "pm2.5:%d pm10 %d"%(pm25,pm10)

Step 4: Running Screen Shot



    • Woodworking Contest

      Woodworking Contest
    • Make it Move Contest

      Make it Move Contest
    • Planter Challenge

      Planter Challenge

    We have a be nice policy.
    Please be positive and constructive.



    Can you please give me the website on which we can buy this Pm2.5 sensor.?


    Hi Thanks for such nice post.

    One question i have data is printing in python script that is good but i can send that data to my remote server?


    Is this script able to put SDS021 into sleep mode?

    Do you know how to put SDS021 into sleep mode and awake it for measurement?

    Do you know what's the meaning of the ID transmitted in bytes #6 and 7? In my case those two bytes are always "af 4a".

    Users of current Raspbian

    should replace "t = serial.Serial('/dev/ttyAMA0',9600)" with "t = serial.Serial('/dev/serial0',9600)", after enabling serial port hardware with raspi-config: run sudo raspi-config, select Interfacing Options / Serial then specify if you want a Serial console (probably no) then select that you want the Serial Port hardware enabled.

    The plug connecting to Raspberry pins should be connected the other way round.

    Hello. I would like to ask something related to the screenshot in Step 4. May I know what is the unit for the PM2.5 in your screenshot? The data sheet for SDS021 says that the range of measurement that can be recorded using the sensor is within 0.0-999.9 μg /m3. Is your reading within that range? Thank you.

    1 reply

    Yes, unit of measurement is μg/m3


    Very nice setup! One question, have you tried linking it with Domoticz for Raspberry Pi? I'd like to purchase this sensor, but I would be good to know if Domoticz supports it as it's a nice tool for managing data of your own weather station.