# CircuitPython Code

This code's pretty simple, you can hook up the power and ground pins to 5V and GND, respectively

Then wire up the TX pin from the sensor to RX pin on your CircuitPython board. Note, RX does not connect to RX!

```import board
import busio
from digitalio import DigitalInOut, Direction

try:
import struct
except ImportError:
import ustruct as struct

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

# Connect the Sensor's TX pin to the board's RX pin
uart = busio.UART(board.TX, board.RX, baudrate=9600)

buffer = []

while True:
data = list(data)
# print("read: ", data)          # this is a bytearray type

buffer += data

while buffer and buffer[0] != 0x42:
buffer.pop(0)

if len(buffer) > 200:
buffer = []  # avoid an overrun if all bad data
if len(buffer) < 32:
continue

if buffer[1] != 0x4d:
buffer.pop(0)
continue

frame_len = struct.unpack(">H", bytes(buffer[2:4]))[0]
if frame_len != 28:
buffer = []
continue

frame = struct.unpack(">HHHHHHHHHHHHHH", bytes(buffer[4:]))

pm10_standard, pm25_standard, pm100_standard, pm10_env, \
pm25_env, pm100_env, particles_03um, particles_05um, particles_10um, \
particles_25um, particles_50um, particles_100um, skip, checksum = frame

check = sum(buffer[0:30])

if check != checksum:
buffer = []
continue

print("Concentration Units (standard)")
print("---------------------------------------")
print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" %
(pm10_standard, pm25_standard, pm100_standard))
print("Concentration Units (environmental)")
print("---------------------------------------")
print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" % (pm10_env, pm25_env, pm100_env))
print("---------------------------------------")
print("Particles > 0.3um / 0.1L air:", particles_03um)
print("Particles > 0.5um / 0.1L air:", particles_05um)
print("Particles > 1.0um / 0.1L air:", particles_10um)
print("Particles > 2.5um / 0.1L air:", particles_25um)
print("Particles > 5.0um / 0.1L air:", particles_50um)
print("Particles > 10 um / 0.1L air:", particles_100um)
print("---------------------------------------")

buffer = buffer[32:]
# print("Buffer ", buffer)
```

Then open up the REPL to see the data printed out nicely for you!

This guide was first published on Dec 27, 2017. It was last updated on Dec 27, 2017. This page (CircuitPython Code) was last updated on Oct 14, 2019.