ESP-IDF driver for Sharp GP2Y Dust Sensor
Go to file
Morgan 'ARR\!' Allen 005264adc1 init 2022-07-19 21:23:40 -07:00
include init 2022-07-19 21:23:40 -07:00
main init 2022-07-19 21:23:40 -07:00
CMakeLists.txt init 2022-07-19 21:23:40 -07:00 init 2022-07-19 21:23:40 -07:00

ESP32 GP2Y Dust Sensor


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

git submodule add components/esp32-gp2y/
git submodule update --init


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.


#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;

    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);