#include #include "driver/i2s.h" // ---------- Pin definitions ---------- #define BUTTON_PIN 9 // Pushbutton #define LED_TEST 11 // Test LED (blinks on button press) #define LED_GREEN 10 // Green LED (blinks on voice) #define I2S_WS 5 // Word Select (LRCLK) #define I2S_SD 6 // Serial Data #define I2S_SCK 7 // Serial Clock (BCLK) // ---------- I2S Config ---------- #define SAMPLE_RATE 16000 #define SAMPLES_READ 256 // ---------- Functions ---------- void blinkLED(int pin, int times, int delayMs) { for (int i = 0; i < times; i++) { digitalWrite(pin, HIGH); delay(delayMs); digitalWrite(pin, LOW); delay(delayMs); } } // ---------- Setup ---------- void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(LED_TEST, OUTPUT); pinMode(LED_GREEN, OUTPUT); // --- I2S microphone setup --- i2s_config_t i2s_config = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = SAMPLE_RATE, .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // ICS-43434 uses left channel .communication_format = I2S_COMM_FORMAT_STAND_I2S, .intr_alloc_flags = 0, .dma_buf_count = 8, .dma_buf_len = SAMPLES_READ, .use_apll = false, .tx_desc_auto_clear = false, .fixed_mclk = 0 }; i2s_pin_config_t pin_config = { .bck_io_num = I2S_SCK, .ws_io_num = I2S_WS, .data_out_num = I2S_PIN_NO_CHANGE, .data_in_num = I2S_SD }; i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); i2s_set_pin(I2S_NUM_0, &pin_config); i2s_zero_dma_buffer(I2S_NUM_0); Serial.println("ICS-43434 Mic, LED, and Button Test Started"); } // ---------- Loop ---------- void loop() { // --- Button test --- if (digitalRead(BUTTON_PIN) == LOW) { Serial.println("Button pressed!"); blinkLED(LED_TEST, 3, 150); } // --- Microphone test --- int32_t samples[SAMPLES_READ]; size_t bytesRead; esp_err_t result = i2s_read(I2S_NUM_0, (void*)samples, sizeof(samples), &bytesRead, portMAX_DELAY); if (result == ESP_OK && bytesRead > 0) { int samplesRead = bytesRead / sizeof(int32_t); int64_t sum = 0; for (int i = 0; i < samplesRead; i++) { sum += abs(samples[i]); } int avgAmplitude = sum / samplesRead; if (avgAmplitude > 50000) { // Adjust sensitivity threshold Serial.printf("Voice detected! Amplitude: %d\n", avgAmplitude); digitalWrite(LED_GREEN, HIGH); delay(80); digitalWrite(LED_GREEN, LOW); } } delay(10); }