pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

StickC MIC

Example Program

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include <M5StickC.h> #include <driver/i2s.h> #define PIN_CLK 0 #define PIN_DATA 34 #define READ_LEN (2 * 256) #define GAIN_FACTOR 3 uint8_t BUFFER[READ_LEN] = {0}; uint16_t oldy[160]; int16_t *adcBuffer = NULL; void i2sInit() // Init I2S { i2s_config_t i2s_config = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM), // Set the I2S operating mode .sample_rate = 44100, // Set the I2S sampling rate .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // Fixed 12-bit stereo MSB .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, // Set the channel format #if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 1, 0) .communication_format = I2S_COMM_FORMAT_STAND_I2S, // Set the communication format #else .communication_format = I2S_COMM_FORMAT_I2S, #endif .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Set the interrupt flag .dma_buf_count = 2, // DMA buffer count .dma_buf_len = 128, // DMA buffer length }; i2s_pin_config_t pin_config; #if (ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 3, 0)) pin_config.mck_io_num = I2S_PIN_NO_CHANGE; #endif pin_config.bck_io_num = I2S_PIN_NO_CHANGE; pin_config.ws_io_num = PIN_CLK; pin_config.data_out_num = I2S_PIN_NO_CHANGE; pin_config.data_in_num = PIN_DATA; i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); i2s_set_pin(I2S_NUM_0, &pin_config); i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); } void showSignal() { int y; for (int n = 0; n < 160; n++) { y = adcBuffer[n] * GAIN_FACTOR; y = map(y, INT16_MIN, INT16_MAX, 10, 70); M5.Lcd.drawPixel(n, oldy[n], WHITE); M5.Lcd.drawPixel(n, y, BLACK); oldy[n] = y; } } void mic_record_task(void *arg) { size_t bytesread; while (1) { i2s_read(I2S_NUM_0, (char *)BUFFER, READ_LEN, &bytesread, (100 / portTICK_RATE_MS)); adcBuffer = (int16_t *)BUFFER; showSignal(); vTaskDelay(100 / portTICK_RATE_MS); } } void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.fillScreen(WHITE); M5.Lcd.setTextColor(BLACK, WHITE); M5.Lcd.println("mic test"); i2sInit(); xTaskCreate(mic_record_task, "mic_record_task", 2048, NULL, 1, NULL); } void loop() { printf("loop cycling\n"); vTaskDelay(1000 / portTICK_RATE_MS); }
On This Page