This commit is contained in:
2026-02-12 21:00:02 -08:00
parent cb1f2b0efd
commit 40714a3a68
1141 changed files with 1010880 additions and 2 deletions

View File

@@ -0,0 +1,25 @@
# Stream Analog input to Serial
We can read an analog signal from a microphone and and write it out to Serial so that we can check the result in the __Arduino Serial Plotter__. To test the functionality I am using a MCP6022 microphone module.
![MCP6022](https://pschatzmann.github.io/Resources/img/mcp6022.jpeg)
![MCP6022](https://pschatzmann.github.io/Resources/img/mcp6022-1.jpeg)
The MCP6022 is a anlog microphone which operates at 3.3 V
We sample the sound signal with the help of the ESP32 I2S ADC input functionality.
### Pins:
| MCP6022 | ESP32
|---------|---------------
| VCC | 3.3
| GND | GND
| OUT | GPIO34
## Serial Plotter
![Serial](https://pschatzmann.github.io/Resources/img/serial_plotter.png)

View File

@@ -0,0 +1,62 @@
/**
* @file adc-serial.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/adc-serial/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
* #TODO retest is outstanding
*/
#include "Arduino.h"
#include "AudioTools.h"
/**
* @brief We use a mcp6022 analog microphone on GPIO34 and write it to Serial
*/
AnalogAudioStream adc;
const int32_t max_buffer_len = 1024;
uint8_t buffer[max_buffer_len];
// The data has a center of around 26427, so we we need to shift it down to bring the center to 0
ConverterScaler<int16_t> scaler(1.0, -26427, 32700 );
// Arduino Setup
void setup(void) {
delay(3000); // wait for serial to become available
Serial.begin(115200);
// Include logging to serial
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info); //Warning, Info, Error, Debug
Serial.println("starting ADC...");
auto adcConfig = adc.defaultConfig(RX_MODE);
// For ESP32 by Espressif Systems version 3.0.0 and later:
// see examples/README_ESP32.md
// adcConfig.sample_rate = 44100;
// adcConfig.adc_bit_width = 12;
// adcConfig.adc_calibration_active = true;
// adcConfig.is_auto_center_read = false;
// adcConfig.adc_attenuation = ADC_ATTEN_DB_12;
// adcConfig.channels = 2;
// adcConfig.adc_channels[0] = ADC_CHANNEL_4;
// adcConfig.adc_channels[1] = ADC_CHANNEL_5;
adc.begin(adcConfig);
}
// Arduino loop - repeated processing
void loop() {
size_t len = adc.readBytes(buffer, max_buffer_len);
// move center to 0 and scale the values
scaler.convert(buffer, len);
int16_t *sample = (int16_t*) buffer;
int size = len / 4;
for (int j=0; j<size; j++){
Serial.print(*sample++);
Serial.print(", ");
Serial.println(*sample++);
}
}