Hi, I am using the ESP32, with WiFi, so only using ADC1. I'm using a few channels, but I'll keep this simple:
- One channel used with I2S driver to sample at higher frequencies.
- Another channel used simply with analogRead...
This was working perfectly - to be precise, a web request triggers a measurement and values are read and returned.
However, I wanted to take the channel used with analogRead, and take measurements regardless of web traffic every 100ms. I created a FreeRTOS task (code below). This works... but now the I2S readings are all zeroes. If I comment out the line containing analogRead in the task, it works again.
I would really appreciate any suggestions, happy to try anything... some thing I have already tried (with no success):
- Using adc1_get_raw instead of analogRead
- Placing a short delay after analogRead
- Increasing and decreasing the priority of the task and the intr_alloc_flags I2S option
- Surrounding the I2S code with vTaskSuspend/Resume
- Pinning the task to core 1 or core 0
```
xTaskCreatePinnedToCore(
[](void *arg) {
TickType_t lastWakeTime = xTaskGetTickCount();
for (;;) {
xTaskDelayUntil(&lastWakeTime, pdMS_TO_TICKS(100)); // maybe try split delay again
// int currentReading = analogRead(hallPin);
double filteredReading = emaAlpha * (double) 200 + (1 - emaAlpha) * previousReading;
previousReading = filteredReading;
double difference = filteredReading - midpoint;
if (abs(difference) < midpointAdjustmentThreshold) {
midpoint += midpointAdjustmentFactor * difference;
}
}
},
"MagSensTask",
4096,
this,
20,
nullptr,
1
);
```
I'm not sure if this is important, but including the I2S code also:
```
// init fn, called once
esp_err_t error;
i2s_config_t i2s_config = {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
.sample_rate = samplingFrequency,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
// If ALL LEFT is selected below, values are read twice so the sampling frequency is wrong
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL3,
.dma_buf_count = 2,
.dma_buf_len = 1024,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0,
};
error = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, nullptr);
if (error != ESP_OK) {
Serial.println(error);
}
error = i2s_set_adc_mode(ADC_UNIT_1, adcInput);
if (error != ESP_OK) {
Serial.println(error);
}
// measure fn
auto *data = (uint16_t *) calloc(samples, sizeof(uint16_t));
size_t bytes_read = 0;
esp_err_t error = i2s_adc_enable(I2S_NUM_0);
if (error != ESP_OK) {
ErrorHandler::submitError("[RF sensor] Failed to enable ADC.");
return doc;
}
error = i2s_read(I2S_NUM_0, data, sizeof(uint16_t) * samples, &bytes_read, portMAX_DELAY);
if (error != ESP_OK) {
ErrorHandler::submitError("[RF sensor] Failed to acquire samples for FFT.");
return doc;
}
error = i2s_adc_disable(I2S_NUM_0);
if (error != ESP_OK) ErrorHandler::submitError("[RF sensor] Failed to disable ADC.");
u_int samples_read = bytes_read / sizeof(uint16_t);
for (int i = 0; i < samples_read; i++) {
Serial.println(uint16_t(data)); // for debugging
}
free(data);
```
ADC Reading Issues With I2S
Jump to
- English Forum
- Explore
- News
- General Discussion
- FAQ
- Documentation
- Documentation
- Sample Code
- Discussion Forum
- Hardware
- ESP-IDF
- ESP-BOX
- ESP-ADF
- ESP-MDF
- ESP-WHO
- ESP-SkaiNet
- ESP32 Arduino
- IDEs for ESP-IDF
- ESP-AT
- ESP IoT Solution
- ESP RainMaker
- Rust
- ESP8266
- Report Bugs
- Showcase
- Chinese Forum 中文社区
- 活动区
- 乐鑫活动专区
- 讨论区
- 全国大学生物联网设计竞赛乐鑫答疑专区
- ESP-IDF 中文讨论版
- 《ESP32-C3 物联网工程开发实战》书籍讨论版
- 中文文档讨论版
- ESP-AT 中文讨论版
- ESP-BOX 中文讨论版
- ESP IoT Solution 中文讨论版
- ESP-ADF 中文讨论版
- ESP Mesh 中文讨论版
- ESP Cloud 中文讨论版
- ESP-WHO 中文讨论版
- ESP-SkaiNet 中文讨论版
- ESP 生产支持讨论版
- 硬件问题讨论
- 项目展示
Who is online
Users browsing this forum: No registered users and 79 guests
- All times are UTC
- Top
- Delete cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.