Arduino mkr gsm 1400 and HPMA115C0-003 compact

Hello, as the title says I'm using mkr gsm 1400 and hpm particle sensor (datasheet). I tested the sensor with the following library PMSensor-HPMA115 and an arduino UNO since this library uses software serial, and it works. However when I try to do the same thing using SERCOM on mkr gsm the communication between the sensor and the MCU doesn't work.
Here is the code I used, basically it's an example sketch and the only thing I changed was

Uart hpmSerial(&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); 

void SERCOM3_Handler(){
  hpmSerial.IrqHandler();
}

  hpmSerial.begin(9600, SERIAL_8E1);
  pinPeripheral(1, PIO_SERCOM); // RX
  pinPeripheral(0, PIO_SERCOM); // TX

to create SERCOM uart. I connected the HPM RX to digital pin 0 (should be TX) and HPM TX to digital pin 1 (should be RX).

/*
 * Prints readings like these to the Serial console:
 *
 * AQI 56  PM 1.0 = 16, PM 2.5 = 18, PM 4.0 = 21, PM 10.0 = 21
 * AQI 55  PM 1.0 = 15, PM 2.5 = 17, PM 4.0 = 20, PM 10.0 = 20
 * AQI 54  PM 1.0 = 14, PM 2.5 = 16, PM 4.0 = 18, PM 10.0 = 18
 * etc.
 *
 * This example uses the SoftwareSerial interface because the Arduino Uno
 * has only one hardware UART. If you have a board with a second hardware
 * serial, consider using it instead for the hpmSerial UART, because it
 * will be more efficient.
 */
#include <Arduino.h>
#include <HPMA115_Compact.h> // https://github.com/jedp/PMSensor-HPMA115
#include "Uart.h"
#include "wiring_private.h"

// A channel for interacting with the sensor.
// For boards that have multiple hardware serial UARTs, consider
// making this a hardware serial.
HPMA115_Compact hpm = HPMA115_Compact();
Uart hpmSerial(&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); 

void SERCOM3_Handler(){
  hpmSerial.IrqHandler();
}

// Helper function to print current measurement results.
void printResults();

// Timestamp for last time we checked the sensor reading.
// We will use this to avoid requesting data faster than the sensor can
// provide it, which is one reading per second.
unsigned long lastCheck = 0;

void setup() {
  Serial.begin(HPMA115_BAUD);

  hpmSerial.begin(9600, SERIAL_8E1);
  pinPeripheral(1, PIO_SERCOM); // RX
  pinPeripheral(0, PIO_SERCOM); // TX
  while(!hpmSerial){
    Serial.println("DEBUG: error opening Serial_plc");
  }

  // Configure the HPM device to use our data stream.
  // (Note carefully the '&' in the next line.)
  hpm.begin(&hpmSerial);

  delay(1000);

  // When the device powers up, it goes directly into auto-send mode.
  while (!hpm.isNewDataAvailable()) {
    // Still waking up?
    delay(1000);
  }

  // Disabling auto-send. We will check results manually.
  hpm.stopAutoSend();
}

void loop() {

  // Only ask for a new reading if a second has elapsed.
  if (millis() - lastCheck > 1000) {
    lastCheck = millis();

    if (hpm.readParticleMeasurementResults()) {
      printResults();
    }
  }

  // The sensor only sends readings at intervals of one second.
  // There is no point in hammering it with data requests.
  delay(1000);
}

void printResults() {
  Serial.print("AQI ");
  Serial.print(hpm.getAQI());
  Serial.print("  PM 1.0 = ");
  Serial.print(hpm.getPM1());
  Serial.print(", PM 2.5 = ");
  Serial.print(hpm.getPM25());
  Serial.print(", PM 4.0 = ");
  Serial.print(hpm.getPM4());
  Serial.print(", PM 10.0 = ");
  Serial.println(hpm.getPM10());
}

Any ideas on what could be causing problems?

Hi @arduinomegalul

Your code for the hpmSerial looks good. I also checked that SERCOM3 isn't being used by any other port, be it SPI, I2C, Serial or the GSM module, which it isn't.

If you've got a 3.3V USB-to-Serial USB or FTDI board, (I just use a 5V USB-to-Serial with an I2C voltage level converter for the 3.3V), it's possible to test the hmp serial port by connecting it to your host computer's USB and use Arduino IDE's console send, as well as receive the echoed back characters:

void loop() 
{
  if (hpmSerial.available())            // Check if incoming data is available on hpmSerial
  {
    byte byteRead = hpmSerial.read();   // Read the most recent byte
    hpmSerial.write(byteRead);          // Echo the byte back out
  }
}

Thanks for the quick response the code was indeed fine (at least when it come to setting up UART). The problem was that i used SERIAL_8E1 instead of SERIAL_8N1 (HPM uses UART with no parity).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.