Introduction: Using Pm2.5 Sensor With Raspberry Pi

Picture of 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

Picture of 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

Picture of Running Screen Shot


SolitoneS (author)2017-06-30

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".

SolitoneS (author)2017-05-29

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.

SolitoneS (author)2017-05-29

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

foxfaisal (author)2017-02-02

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.

SolitoneS (author)foxfaisal2017-05-29

Yes, unit of measurement is μg/m3

RadekP11 (author)2017-02-22


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.



About This Instructable




Add instructable to: