<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[M5StickC running i2s microphone with i2c ToF Hat with Arduino code]]></title><description><![CDATA[<p dir="auto">Hi M5StickC community,</p>
<p dir="auto">I love my new M5StickC with my ToF Hat. I would to use the built-in i2S microphone and the i2c ToF Hat simultaneously. I tried combining the 2 examples provided in the Arduino library. The ToF distance shows up on the display (but very slow response time), but the audio doesn't show up.</p>
<p dir="auto">May I have some help getting these two working together?</p>
<p dir="auto">Thanks</p>
<p dir="auto">Here's the code below:</p>
<p dir="auto">#include &lt;M5StickC.h&gt;<br />
#include &lt;driver/i2s.h&gt;<br />
#include &lt;Wire.h&gt;<br />
#include &lt;VL53L0X.h&gt;</p>
<p dir="auto">#define PIN_CLK  0<br />
#define PIN_DATA 34<br />
#define READ_LEN (2 * 256)<br />
#define GAIN_FACTOR 3<br />
uint8_t BUFFER[READ_LEN] = {0};</p>
<p dir="auto">uint16_t oldy[160];<br />
int16_t *adcBuffer = NULL;<br />
VL53L0X sensor;<br />
TFT_eSprite img = TFT_eSprite(&amp;M5.Lcd);<br />
uint16_t sensorDistance;</p>
<p dir="auto">void i2sInit()<br />
{<br />
i2s_config_t i2s_config = {<br />
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),<br />
.sample_rate =  44100,<br />
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB<br />
.channel_format = I2S_CHANNEL_FMT_ALL_RIGHT,<br />
.communication_format = I2S_COMM_FORMAT_I2S,<br />
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,<br />
.dma_buf_count = 2,<br />
.dma_buf_len = 128,<br />
};</p>
<p dir="auto">i2s_pin_config_t pin_config;<br />
pin_config.bck_io_num   = I2S_PIN_NO_CHANGE;<br />
pin_config.ws_io_num    = PIN_CLK;<br />
pin_config.data_out_num = I2S_PIN_NO_CHANGE;<br />
pin_config.data_in_num  = PIN_DATA;</p>
<p dir="auto">i2s_driver_install(I2S_NUM_0, &amp;i2s_config, 0, NULL);<br />
i2s_set_pin(I2S_NUM_0, &amp;pin_config);<br />
i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);<br />
}</p>
<p dir="auto">void mic_record_task (void* arg)<br />
{<br />
size_t bytesread;<br />
while(1){<br />
i2s_read(I2S_NUM_0,(char*) BUFFER, READ_LEN, &amp;bytesread, (100 / portTICK_RATE_MS));<br />
adcBuffer = (int16_t *)BUFFER;<br />
//sensorDistance = sensor.readRangeContinuousMillimeters();<br />
<a href="//Serial.print" target="_blank" rel="noopener noreferrer nofollow ugc">//Serial.print</a>(sensorDistance);<br />
//if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }<br />
<a href="//Serial.println" target="_blank" rel="noopener noreferrer nofollow ugc">//Serial.println</a>();<br />
showSignal();<br />
vTaskDelay(100 / portTICK_RATE_MS);<br />
}<br />
}</p>
<p dir="auto">void setup() {<br />
Serial.begin(115200);<br />
Wire.begin(0, 26, 100000);<br />
M5.begin();<br />
M5.Lcd.setRotation(3);<br />
M5.Lcd.fillScreen(WHITE);<br />
M5.Lcd.setTextColor(BLACK, WHITE);<br />
M5.Lcd.println("mic test");</p>
<p dir="auto">img.createSprite(70, 35);<br />
img.fillSprite(BLACK);<br />
img.setTextColor(WHITE);<br />
img.setTextSize(2);</p>
<p dir="auto">sensor.setTimeout(500);<br />
if (!sensor.init()) {<br />
img.setCursor(10, 10);<br />
img.print("Failed");<br />
img.pushSprite(0, 0);<br />
Serial.println("Failed to detect and initialize sensor!");<br />
while (1) {}<br />
}<br />
// Start continuous back-to-back mode (take readings as<br />
// fast as possible).  To use continuous timed mode<br />
// instead, provide a desired inter-measurement period in<br />
// ms (e.g. sensor.startContinuous(100)).<br />
sensor.startContinuous();</p>
<p dir="auto">i2sInit();<br />
xTaskCreate(mic_record_task, "mic_record_task", 2048, NULL, 1, NULL);<br />
}</p>
<p dir="auto">void showSignal(){<br />
img.fillSprite(BLACK);<br />
img.setCursor(10, 10);<br />
img.print(sensorDistance);<br />
img.pushSprite(0, 0);<br />
int y;<br />
for (int n = 0; n &lt; 160; n++){<br />
y = adcBuffer[n] * GAIN_FACTOR;<br />
y = map(y, INT16_MIN, INT16_MAX, 10, 70);<br />
M5.Lcd.drawPixel(n, oldy[n],WHITE);<br />
M5.Lcd.drawPixel(n,y,BLACK);<br />
oldy[n] = y;<br />
}<br />
}</p>
<p dir="auto">void loop() {<br />
printf("loop cycling\n");<br />
vTaskDelay(1000 / portTICK_RATE_MS); // otherwise the main task wastes half of the cpu cycles<br />
}</p>
]]></description><link>https://community.m5stack.com/topic/3632/m5stickc-running-i2s-microphone-with-i2c-tof-hat-with-arduino-code</link><generator>RSS for Node</generator><lastBuildDate>Fri, 08 May 2026 06:46:25 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/3632.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Oct 2021 00:03:36 GMT</pubDate><ttl>60</ttl></channel></rss>