Skip to content

Instantly share code, notes, and snippets.

@JanX2 JanX2/ forked from geoffwatts/
Created Feb 9, 2018

What would you like to do?
Read an SDS011 Laser PM2.5 Sensor (Nova PM Sensor) with Python
# -*- coding: UTF-8 -*-
# Requires pyserial. Install via:
# pip install pyserial
from __future__ import print_function
from serial import Serial, EIGHTBITS, STOPBITS_ONE, PARITY_NONE
import time, struct
port = "/dev/tty.wchusbserial1a150" # Set this to your serial port.
baudrate = 9600
# Prepare serial connection.
ser = Serial(port, baudrate=baudrate, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE)
byte, previousbyte = b"\x00", b"\x00"
while True:
previousbyte = byte
byte =
# We got a valid packet header.
if previousbyte == HEADER_BYTE and byte == COMMANDER_BYTE:
packet = # Read 8 more bytes
# Decode the packet - little endian, 2 shorts for pm2.5 and pm10, 2 ID bytes, checksum.
readings = struct.unpack('<HHcccc', packet)
# Measurements.
pm_25 = readings[0]/10.0
pm_10 = readings[1]/10.0
# ID
id = packet[4:6]
# Prepare checksums.
checksum = readings[4][0]
calculated_checksum = sum(packet[:6]) & 0xFF
checksum_verified = (calculated_checksum == checksum)
# Message tail.
tail = readings[5]
if tail == TAIL_BYTE and checksum_verified:
#print("PM 2.5:", pm_25, "μg/m^3 PM 10:", pm_10, "μg/m^3")
print("PM 2.5:", pm_25, "µg/m³ PM 10:", pm_10, "µg/m³ ID:", bytes(id).hex())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.