ESP-IDF driver for Sharp GP2Y Dust Sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Morgan 'ARR\!' Allen 005264adc1 init 11 months ago
include init 11 months ago
main init 11 months ago
CMakeLists.txt init 11 months ago init 11 months ago

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