esp32-gp2y/README.md

1.4 KiB

ESP32 GP2Y Dust Sensor

Install

Designed to be used as an esp-idf component. Suggested usage is with git submodule

git submodule add https://git.oit.cloud/morgan/esp32-gp2y.git components/esp32-gp2y/
git submodule update --init

API

typedef gp2y_cfg_t

float K Sensitivity. Default: 0.5 float offset_voltage Voltage level at 0ug/m3 dust. Defaults to 0.6v, dynamically lowers. int8_t pin_led Pin to pulse LED on. adc1_channel_t adc_channel ADC channel to read on. See IDF Guide to get pin to channel reference. adc_unit_t adc_unit ADC unit to read on.

int32_t gp2y_read(gp2y_cfg_t *cfg, uint8_t samples)

Passing a pointer to gp2y_cfg_t and number of samples to take. 100 is a good starting point.

Example

#include <stdio.h>
#include <unistd.h>
#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp32-gp2y.h"

#define PIN_LED         5

void app_main() {
    gp2y_cfg_t gp2y_cfg = GP2Y_DEFAULT_CFG;
    gp2y_cfg.adc_channel = channel;
    gp2y_cfg.pin_led = PIN_LED;

    gpio_config_t io_conf;
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = (1ULL<<gp2y_cfg.pin_led);
    io_conf.pull_down_en = 1;
    io_conf.pull_up_en = 0;
    gpio_config(&io_conf);

    adc1_config_channel_atten(gp2y_cfg.adc_channel, ADC_ATTEN_DB_11);

    while (1) {
      printf("aqi: %dug/m3\n", gp2y_read(&gp2y_cfg, 100));
      vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}