Hi Guys,
I need to read an analog input quickly.
analogRead()
was way too slow and was messing the rest of my code up.
adc1_get_raw(ADC1_CHANNEL_3)
was somewhat faster, but still not exactly what I was looking for.
I started looking at I2S, and although I don't understand it fully yet, I managed to whittle away at some code I found online and came up with this:
#include <driver/i2s.h>
// I2S
#define I2S_SAMPLE_RATE (3000)
#define ADC_INPUT (ADC1_CHANNEL_4) //pin 32
#define I2S_DMA_BUF_LEN (8)
// The 4 high bits are the channel, and the data is inverted
size_t bytes_read;
uint16_t buffer[I2S_DMA_BUF_LEN] = {0};
unsigned long lastTimePrinted;
unsigned long loopTime = 0;
void i2sInit() {
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
.sample_rate = I2S_SAMPLE_RATE, // The format of the signal using ADC_BUILT_IN
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = I2S_DMA_BUF_LEN,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT);
i2s_adc_enable(I2S_NUM_0);
adc1_config_channel_atten(ADC_INPUT, ADC_ATTEN_DB_11);
}
void setup() {
Serial.begin(115200);
i2sInit();
}
void loop() {
unsigned long startMicros = ESP.getCycleCount();
i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, 0);
unsigned long stopMicros = ESP.getCycleCount();
loopTime = stopMicros - startMicros;
if (millis() - lastTimePrinted >= 100) {
Serial.println("------------------");
Serial.println(buffer[0] & 0x0FFF);
Serial.println(loopTime);
lastTimePrinted = millis();
}
}
This works really well, and I'm happy. Need to study I2S some more to get a better grasp of it.
However, I'd like to read another analog input but I'm not sure how it can be done (if at all) using I2S.
I tried fiddling a bit but not really understanding it 100%. With any changes it either doesn't compile, or has no output.
Any ideas? Any snippets of code doing something similar?
Cheers,
Matt