element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
RoadTests & Reviews
  • Products
  • More
RoadTests & Reviews
Blog Using I2C communication with the Panasonic SN-GCJA5 Laser Type Particulate Matter (PM) Sensor
  • Blog
  • RoadTest Forum
  • Documents
  • Events
  • RoadTests
  • Reviews
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • SN-GCJA5
  • raspberry pi pico
  • panasonic
  • PM-Sensor
  • arduino
  • Share
  • Subscribe by email
  • More
  • Cancel
Related
Recommended

Using I2C communication with the Panasonic SN-GCJA5 Laser Type Particulate Matter (PM) Sensor

BigG
BigG
30 Dec 2021

Introduction

As mentioned in my first blog, I’m in the process of road-testing the Panasonic SN-GCJA5 Laser Type Particulate Matter (PM) Sensor and, having successfully parsed the UART data stream into something meaningful, I decided to see how easy or difficult it would be to receive data using I2C. As before, I’m using a Raspberry Pi Pico board as my microcontroller.

I also decided to stick with the Arduino IDE for this endeavour as Arduino provides a standard I2C library called Wire.h.

However to keep things really simple, I decided to use a “helper” library (or library “wrapper”) called the Adafruit Bus IO Library instead of creating my own library for this sensor. This library wrapper provides some generic read and write functions that seemed suitable for my needs.

So, this is what I’ve done to get me started. Please note that I have not carried out any tests to validate the data received from this firmware. This blog merely demonstrates that I2C communication between the sensor (I2C slave) and the Raspberry Pi Pico (I2C master) works with firmware written using the Arduino IDE and associated off-the-shelf libraries.

Firmware development

The Adafruit Bus IO Library comes with three I2C examples. I started with the “i2c_address_detect.ino” example to see if I could detect my sensor using the slave address provided in the sensor documentation.

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

Now that I knew my PM sensor could be found using the correct I2C slave address, I wanted to see if I could read data from the sensor. For this exercise I used the “i2c_readwrite.ino” example.

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

Hopefully that video demonstrated how painless that was to read data from the sensor. As shown, I was able to use this library’s “write_then_read” function to obtain the data I required when setting the stop bit boolean parameter to false to ensure a repeat start condition.

One observation I made was that it appears from this test that the 500us delay (as per documentation) between the write and read request was not required – this of course may need further testing to validate.

To include the 500us delay, separate write and read functions are required. I used these separate functions with my final test code (as provided below) where I also parsed out the data according to the data format specification.

/*
  Panasonic Laser Type PM Sensor I2C sketch

  This sketch allows you to read data from a Panasonic SN-GCJA5 PM sensor.
  It is based on the Adafruit Bus IO library (https://github.com/adafruit/Adafruit_BusIO)

  created 30 December 2021
  by C Gerrish (BigG)

  Permission to use, copy, modify, and/or distribute this software for any 
  purpose with or without fee is hereby granted.
*/

#include <Adafruit_I2CDevice.h>

#define I2C_ADDRESS (0x33u)

#define SENSOR_STATUS_REG   (0x26u)
#define SENSORDAT_PM10_REG  (0x00u)

const byte STABLE = 20;

/**! Structure holding Panasonic's PM Sensor payload data **/
typedef struct SNGCJA5_datastruct {
  uint32_t 
      pm10_mdv,             ///< Standard PM1.0
      pm25_mdv,             ///< Standard PM2.5
      pm100_mdv;            ///< Standard PM10.0
  uint16_t 
      reg1_pc,              ///< 0.3um Particle Count
      reg2_pc,              ///< 0.5um Particle Count
      reg3_pc,              ///< 1.0um Particle Count
      reg4_pc,              ///< 2.5um Particle Count
      reg5_pc,              ///< 5.0um Particle Count
      reg6_pc;              ///< 7.5um Particle Count
} PM_MDVPC_Data;

uint8_t statBuffer[1] = {0};
byte StableCntr = 0;
bool StableFlag = false;

Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS);


void setup() {
  while (!Serial) { delay(10); }
  Serial.begin(115200);
  Serial.println("Panasonic SN-GCJA5 PM Sensor I2C communication test");

  if (!i2c_dev.begin()) {
    Serial.print("Did not find device at 0x");
    Serial.println(i2c_dev.address(), HEX);
    while (1);
  }
  Serial.print("Panasonic SN-GCJA5 PM Sensor has been found on I2C address 0x");
  Serial.println(i2c_dev.address(), HEX);

  if (i2c_dev.setSpeed(400000)) Serial.println("I2C Speed set to 400k");

  
}

void loop() {
  // Check sensor status
  statBuffer[0] = SENSOR_STATUS_REG;
  
  if (i2c_dev.write(statBuffer, 1, false)) {
    delayMicroseconds(500);
    statBuffer[0] = 255;        // Setting to 255 so that we know if returns 0 then status ok
    if (i2c_dev.read(statBuffer, 1)) {
      if (statBuffer[0] == 0) {
        Serial.println("Sensor Status: OK");
        
        // Now grab the sensor data
        statBuffer[0] = SENSORDAT_PM10_REG;
        if (i2c_dev.write(statBuffer, 1, false)) {
          delayMicroseconds(500);
          uint8_t PMbuffer[26] = {0};
          if (i2c_dev.read(PMbuffer, 26)) {
            PM_MDVPC_Data data;
            data.pm10_mdv = (PMbuffer[0] | PMbuffer[1]<< 8 | PMbuffer[2]<< 16 | PMbuffer[3]<< 24);
            data.pm25_mdv = (PMbuffer[4] | PMbuffer[5]<< 8 | PMbuffer[6]<< 16 | PMbuffer[7]<< 24);
            data.pm100_mdv = (PMbuffer[8] | PMbuffer[9]<< 8 | PMbuffer[10]<< 16 | PMbuffer[11]<< 24);
            data.reg1_pc = (PMbuffer[12] | PMbuffer[13]<< 8);
            data.reg2_pc = (PMbuffer[14] | PMbuffer[15]<< 8);
            data.reg3_pc = (PMbuffer[16] | PMbuffer[17]<< 8);
            //gap
            data.reg4_pc = (PMbuffer[20] | PMbuffer[21]<< 8);
            data.reg5_pc = (PMbuffer[22] | PMbuffer[23]<< 8);
            data.reg6_pc = (PMbuffer[24] | PMbuffer[25]<< 8);
            
            if (!StableFlag) Serial.println("*** UNSTABLE READINGS (within approx 28 secs of power on) ***");
            Serial.println("Particle Mass Density (μg/m3):");
            Serial.print("PM1.0: "); Serial.print(data.pm10_mdv/1000.0, 3);
            Serial.print(" | PM2.5: "); Serial.print(data.pm25_mdv/1000.0, 3);
            Serial.print(" | PM10: "); Serial.println(data.pm100_mdv/1000.0, 3);
  
            Serial.println("Particle Count:");
            Serial.print("0.3-0.5μm: "); Serial.print(data.reg1_pc, DEC);
            Serial.print(" | 0.5-1.0μm: "); Serial.print(data.reg2_pc, DEC);
            Serial.print(" | 1.0-2.5μm: "); Serial.print(data.reg3_pc, DEC);
            Serial.print(" | 2.5-5.0μm: "); Serial.print(data.reg4_pc, DEC);
            Serial.print(" | 5.0-7.5μm: "); Serial.print(data.reg5_pc, DEC);
            Serial.print(" | 7.5-10.0μm: "); Serial.println(data.reg6_pc, DEC);
            Serial.println();
          }
          else Serial.println("Failed to read sensor data");
        }
        else Serial.println("Failed to write to PM10_REG");
        
      }
      else Serial.println("Sensor Status: ERROR " + String(statBuffer[0], DEC));
    }
    else Serial.println("Failed to read");
  }
  else Serial.println("Failed to write STATUS_REG");  
  
  delay(1000);
  if (StableCntr < STABLE) StableCntr++;
  else StableFlag = true;
  
}

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

Anonymous
  • DAB
    DAB 4 months ago

    Nice start, I look forward to seeing this device in action.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • BigG
    BigG 4 months ago in reply to genebren

    Thanks. Probably a case of beginners luck when deciding to go with the Bus IO library (had never used it before).

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • genebren
    genebren 4 months ago

    Well done! Looks like you were able to get your I2C running just fine.  I might have to circle back, skipping over the existing library and write a new one to allow the 'repeated start'.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube