Skip to content

Analog mic + pwm out behaves weird? #1482

Closed
@pschatzmann

Description

@pschatzmann

Discussed in #1480

Originally posted by thegoodhen April 4, 2024
Hello, I am putting this into Q and A, since I am not sure if it's a bug or if I am just doing something wrong.

Thank you so much for this awesome library.
I started playing with it and I'm trying to build a simple Dalek voice changer.
As a first step, I am simply trying to copy the microphone data to a speaker.

I tried reading the analog input and streaming it as PWM, but that doesn't work as expected.

I made sure I am using the correct pin for the analog input by running the base_adc_average_mono_serial example, which works.

I made sure I am using the correct pin for the analog output by running the streams_generator_pwm example, which works.

In order to have repeatable data, instead of microphone I am using a signal generator with a DC offset. I verified that the adc sees the data correctly with the base_adc_average_mono_serial.

There is low volume noise coming from the speaker. The oscilloscope shows a waveform with a roughly similar shape to the one I am putting in, extremely noisy, low amplitude, chopped up (discontinuities in the waveform, as if you cut pieces of it and stitched them back together) and at a much lower frequency, suggesting a sampling issue.

Am I just doing something really dumb? Or is this combination not supported? Or is it a bug?

I just need a combination of an analog input and "analog-compatible" (i.e. lm386 with an aliasing filter compatible) output.

As far as I remember, the ESP32 does not support ADC DMA with I2S PDM, since ADC DMA uses the I2S0 memory and furthermore PDM is only supported by I2S0 - that's why I tried my luck with the PWM instead.

I tried enabling "auto_center_read", didnt help.
I tried changing the sample frequency between 8kHz and 44100Hz, didn't help.
I tried changing the input resolution. 8bit (which you say is not supported by most the classes) makes the noise much louder.

Thank you very much!! If you need me to do some more tests, just let me know

HW+SW:

Arduino 1.8.19, ESP32 2.0.9, NodeMCU32S

Sketch:

/**
   @file streams-generator-pwm.ino
   @author Phil Schatzmann
   @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-generator-pwm/README.md
   @author Phil Schatzmann
   @copyright GPLv3
*/

#include "AudioTools.h"
#define SAMPLE_RATE 16000


PWMAudioOutput pwm;
AudioInfo info(SAMPLE_RATE, 1, 16);


// On board analog to digital converter
AnalogAudioStream analog_in;
StreamCopy copier(pwm, analog_in);    // copy in to out

void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);


  auto adcConfig = analog_in.defaultConfig(RX_MODE);
  adcConfig.copyFrom(info);


  // adcConfig.is_auto_center_read = true;


  analog_in.begin(adcConfig);


  // setup PWM output
  auto config = pwm.defaultConfig();
  config.copyFrom(info);
  config.resolution = 8;  // must be between 8 and 11 -> drives pwm frequency (8 is default)

  Pins pins = {23};
  config.setPins(pins);
  pwm.begin(config);
}

void loop() {
  copier.copy();
  //delay(10);
}
```</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions