gfvalvo:
What, exactly, does that mean? You instead use the Espressif ESP-IDF? Seems to me that in doing that you would lose access to the thousands of libraries that are targeted to the Arduino Ecosystem.
What it means:
This was written in the Arduino IDE bypassing the ESP32 core:
#include <driver/adc.h>
#include "sdkconfig.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
////
#define evtDoParticleRead ( 1 << 0 ) // declare an event
EventGroupHandle_t eg; // variable for the event group handle
////
esp_timer_handle_t oneshot_timer; //veriable to store the hardware timer handle
//
void IRAM_ATTR oneshot_timer_callback( void* arg )
{
BaseType_t xHigherPriorityTaskWoken;
xEventGroupSetBitsFromISR( eg, evtDoParticleRead, &xHigherPriorityTaskWoken ); //freeRTOS event trigger made for ISR's
}
////
void setup()
{
eg = xEventGroupCreate(); // get an event group handle
//
gpio_config_t io_cfg = {}; // initialize the gpio configuration structure
io_cfg.mode = GPIO_MODE_OUTPUT; // set gpio mode
//bit mask of the pins to set
io_cfg.pin_bit_mask = ( (1ULL << GPIO_NUM_4) ); // assign gpio number to be configured
//configure GPIO with the given settings
gpio_config(&io_cfg); // configure the gpio based upon the parameters as set in the configuration structure
gpio_set_level( GPIO_NUM_4, LOW); // set air particle sensor trigger pin to LOW
// set up A:D channels
// https://dl.espressif.com/doc/esp-idf/latest/api-reference/peripherals/adc.html
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_11);// using GPIO 32
//
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/esp_timer.html?highlight=hardware%20timer High Resoultion Timer API
esp_timer_create_args_t oneshot_timer_args = {}; // initialize High Resoulition Timer (HRT) configuration structure
oneshot_timer_args.callback = &oneshot_timer_callback; // configure for callback, name of callback function
esp_timer_create( &oneshot_timer_args, &oneshot_timer ); // assign configuration to the HRT, receive timer handle
//
xTaskCreatePinnedToCore( fDoParticleDetector, "fDoParticleDetector", 5000, NULL, 3, NULL, 1 ); // assign all to core 1
} // end void setup()
////
void fDoParticleDetector( void * parameter )
{
float ADbits = 4095.0f;
float uPvolts = 3.3f;
float adcvalue = 0.0f;
for (;;)
{
//enable sensor led
gpio_set_level( GPIO_NUM_4, HIGH ); // set gpio 4 to high
esp_timer_start_once( oneshot_timer, 280 ); // trigger one shot timer for a 280uS timeout
//esp_timer_start_once( oneshot_timer, 380 ); //
xEventGroupWaitBits (eg, evtDoParticleRead, pdTRUE, pdTRUE, portMAX_DELAY ); // event will be triggered by the timer expiring, wait here for the 280uS
//take a raw ADC reading from the dust sensor
adcvalue = float( adc1_get_raw(ADC1_CHANNEL_4) );
//Shut off the sensor LED
gpio_set_level( GPIO_NUM_4, LOW );
//calculate voltage
adcvalue = ( uPvolts * adcvalue ) / ADbits; // calculate sensor voltage
log_i( "%f", adcvalue ); // print the ADC value
vTaskDelay( 125 );
}
vTaskDelete( NULL );
}// end fDoParticleDetector()
////
void loop() {}
The code runs faster, it does not use the overhead of the Arduino ES32 IDE core.
The code for the A:D converter gives a more accurate result then if I used the Arduino ESP32 IDE core.
The code is, when added to the larger project, not blocking, even though there is a 280uS delay
There are some ESP32 features that are not in the Arduino core.
IDF was not used to write that code. All done in the Arduino IDE.
Retains my access to
the thousands of libraries that are targeted to the Arduino Ecosystem.