Skip to content

Instantly share code, notes, and snippets.

@FLamparski
Created June 5, 2019 10:48
Show Gist options
  • Save FLamparski/25a39a8f5a1c4b3535fe67d7ca39b0ea to your computer and use it in GitHub Desktop.
Save FLamparski/25a39a8f5a1c4b3535fe67d7ca39b0ea to your computer and use it in GitHub Desktop.
Simple ESP32/Arduino program for reading the SDS-011 output in continuous mode
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, -1, -1, false);
}
void loop() {
if (Serial2.available()) {
byte pm_buf[10];
Serial2.readBytes(pm_buf, 10);
unsigned int pm_25_count = 0;
pm_25_count = pm_buf[3] << 8;
pm_25_count |= pm_buf[2];
float pm_25 = (float) pm_25_count / 10.0f;
unsigned int pm_10_count = 0;
pm_10_count = pm_buf[5] << 8;
pm_10_count |= pm_buf[4];
float pm_10 = (float) pm_10_count / 10.f;
Serial.printf("PM2.5 = %.4f; PM10 = %.4f\n", pm_25, pm_10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment