A code library for HPMA115S0 particle sensor

Teodor Costachioiu
Mar 19, 2019 · 2 min read
Image for post
Image for post

In today’s blog post I will show you how to use my latest code library for the Honeywell HPMA115S0 particle sensor.

If you don’t need the explanations and you need only the library, the download link is https://github.com/Electronza/HPMA115S0.

The library takes a lot from my original post on HPMA115S0, and it should be regarded as an evolution of that blog post. What I did is to move most code inside the library; I also changed some functions names to better match what’s inside the HPMA115S0 datasheet. Furthermore, I wrote the code inside the library in a way that it works with both hardware and SoftwareSerial ports. Code examples for the two use cases are also provided.

The HPMA115S0 sensor requires 5V power, but it uses 3.3V logic levels on TX and RX lines. As such, a logic level converter like BOB-12009 is required when using the sensor with 5V boards like Arduino Uno and Mega, as I did in this blog post. On 3.3V boards like Arduino Due, the TX and RX lines can be directly connected to the board.

Regardless if you use hardware or SoftwareSerial, the serial port for the sensor must be configured first for 9600 baud rate. If you use SoftSerial, you will have to define the SoftwareSerial first.

#include <hpma115s0.h>
#include <SoftwareSerial.h>

bool my_status;
float p25;
float p10;

SoftwareSerial mySerial(10, 11); // RX, TX
HPMA115S0 my_hpm(mySerial);

//...

void setup() {
//...
mySerial.begin(9600);
//...
}

//...

Things are simpler when using hardware serial (Serial2 in the examples below):

#include <hpma115s0.h>

bool my_status;
float p25;
float p10;

HPMA115S0 my_hpm(Serial2);

//...

void setup() {
//...
Serial2.begin(9600);
//...
}

//...

The rest of the code works the same way, no matter if either hardware of serial port is used.

The current version of the library provides the following functions:

  • HPMA115S0.start_measurement() — this functions turns on the fan and enters measurement mode
  • HPMA115S0.stop_measurement() — this functions turns off the fan and enters low power mode
  • HPMA115S0.stop_autosend() — This function turns off autosend mode.
  • HPMA115S0.enable_autosend() — This function turns on autosend mode.
  • HPMA115S0.read(float *p25, float *p10) — performs a readout of particle data; it returns the results as a float.

All functions return “1” if successful, and “0” otherwise.

The following are not (yet) implemented

  • Reading and parsing of data in autosend mode
  • Read Customer Adjustment Coefficient
  • Read Customer Adjustment Coefficient

I have tested the library with Arduino Uno, using software serial, and with Arduino Due using hardware serial. I would be happy to hear how it works on other boards, and if you have any issues.


Originally published at https://electronza.com on March 19, 2019. Moved to Medium on April 26, 2020.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store