Skip to content

Instantly share code, notes, and snippets.

Last active August 10, 2022 17:32
  • Star 11 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Nova SDS011 sensor. Code is free to use in own projects, but I don't provide any support nor don't make me liable if it is not working :)
# -*- coding: UTF-8 -*-
import serial, time, struct, array
from datetime import datetime
ser = serial.Serial()
ser.port = "/dev/ttyUSB0" # Set this to your serial port
ser.baudrate = 9600
byte, lastbyte = "\x00", "\x00"
cnt = 0
while True:
lastbyte = byte
byte =
# print("Got byte %x" %ord(byte))
# We got a valid packet header
if lastbyte == "\xAA" and byte == "\xC0":
sentence = # Read 8 more bytes
# print "Sentence size {}".format(len(sentence))
readings = struct.unpack('<hhxxcc',sentence) # Decode the packet - big endian, 2 shorts for pm2.5 and pm10, 2 reserved bytes, checksum, message tail
# print array.array('B',sentence)
pm_25 = readings[0]/10.0
pm_10 = readings[1]/10.0
# ignoring the checksum and message tail
if (cnt == 0 ):
line = "PM 2.5: {} μg/m^3 PM 10: {} μg/m^3".format(pm_25, pm_10)
print("%d %b %Y %H:%M:%S.%f: ")+line)
cnt += 1
if (cnt == 5):
cnt = 0
Copy link

Very useful!
Thank you!

Copy link

Hi @netmaniac, based on your Linux script for SDS 011 I've developed a Windows version of script for SDL 607.
It's included in my API application in Node.js, used to monitor indoor air quality and display it on the network.
I hope you don't mind that! Please let me know and feel free to contribute with your version too in a pull request:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment