Crate sen0177[][src]

Expand description

sen0177 is a Rust library/crate that reads air quality data from the SEN0177 air quality sensor.


  • You’ve connected the sensor to a UART on your device, and your device has a crate implementing the embedded_hal::serial::Read trait.
  • You’ve configured the UART for 9600 baud, 8 data bits, no parity, 1 stop bit, and no flow control.


Include the following in your Cargo.toml file:

sen0177 = "0.2"

If you are in a no_std environment, you may depend on this crate like so:

sen0177 = { version = "0.2", default-features = false }


This example shows how to use the sensor when connected to a Linux- based serial device.

use linux_embedded_hal::Serial;
use sen0177::Reading;
use serial::{core::prelude::*, BaudRate, CharSize, FlowControl, Parity, StopBits};
use std::{io, path::Path, time::Duration};

const SERIAL_PORT: &str = "/dev/ttyS0";
const BAUD_RATE: BaudRate = BaudRate::Baud9600;
const CHAR_SIZE: CharSize = CharSize::Bits8;
const PARITY: Parity = Parity::ParityNone;
const STOP_BITS: StopBits = StopBits::Stop1;
const FLOW_CONTROL: FlowControl = FlowControl::FlowNone;

pub fn main() -> std::io::Result<()> {
    let mut serial = Serial::open(&Path::new(SERIAL_PORT))?;
    serial.0.reconfigure(&|settings| {

    let reading = sen0177::read(&mut serial).expect("Failed to read sensor data");
    println!("PM1: {}µg/m³, PM2.5: {}µg/m³, PM10: {}µg/m³",
             reading.pm1(), reading.pm2_5(), reading.pm10());

Note that the serial device occasionally returns bad data. If you receive Sen0177Error::InvalidData or Sen0177Error::ChecksumMismatch from the read call, a second try will usually succeed.


Raspberry Pi

If you’re using this with a Raspberry Pi, note that by default the primary GPIO pins are set up as a Linux serial console. You will need to disable that (by editing /boot/cmdline.txt) before this will work. Instead of using a specifiy TTY device node, you should use /dev/serial0, which is a symlink to the proper device.

Alternatively, you can use the second UART, but you’ll need to load an overlay to assign it to GPIO pins. See UART configuration and the UART-related overlays for more information.


A single air quality sensor reading


Describes errors returned by the SEN0177 sensor


Reads a single sensor measurement