Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "driver/i2s.h"
- #define I2S_PORT I2S_NUM_0
- #define ADC_CHANNEL ADC1_CHANNEL_6 // GPIO34
- void setup() {
- Serial.begin(115200);
- Serial.println("Initializing I2S ADC...");
- // Configure I2S for ADC mode
- i2s_config_t i2s_config = {
- .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_ADC_BUILT_IN),
- .sample_rate = 44100,
- .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
- .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
- .communication_format = I2S_COMM_FORMAT_I2S_MSB,
- .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
- .dma_buf_count = 4,
- .dma_buf_len = 1024
- };
- esp_err_t err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
- if (err != ESP_OK) {
- Serial.printf("I2S driver install failed: %d\n", err);
- return;
- }
- Serial.println("I2S driver installed successfully.");
- // Set ADC mode for I2S
- i2s_set_adc_mode(ADC_UNIT_1, ADC_CHANNEL);
- Serial.println("ADC mode set for I2S.");
- // Enable I2S ADC mode
- err = i2s_adc_enable(I2S_PORT);
- if (err != ESP_OK) {
- Serial.printf("I2S ADC enable failed: %d\n", err);
- return;
- }
- Serial.println("I2S ADC enabled successfully.");
- }
- void loop() {
- uint16_t adcBuffer[1];
- size_t bytesRead;
- esp_err_t err = i2s_read(I2S_PORT, adcBuffer, sizeof(adcBuffer), &bytesRead, portMAX_DELAY);
- if (err != ESP_OK) {
- Serial.printf("I2S read failed: %d\n", err);
- delay(1000);
- return;
- }
- uint16_t adcValue = adcBuffer[0] & 0xFFF;
- Serial.printf("ADC Value: %d\n", adcValue);
- delay(100);
- }
- I get this:
- Initializing I2S ADC...
- I2S driver installed successfully.
- ADC mode set for I2S.
- E (16) i2s(legacy): i2s_adc_enable(899): i2s built-in adc not enabled
- I2S ADC enable failed: 259
- E (28) i2s(legacy): i2s_read(1739): RX mode is not enabled
- I2S read failed: 258
Advertisement
Add Comment
Please, Sign In to add comment