Go Down

Topic: Sampling Audio in Real Time  (Read 3268 times) previous topic - next topic

jenniferliu1272

Apr 07, 2018, 12:13 am Last Edit: Apr 07, 2018, 12:19 am by jenniferliu1272
I am attempting to sample audio signal using the audio jack show in the image.

I have 3 pins, a ground pin connected to ground, 1 connected to pin 7, and one connected to pin 8.

Then I tried sampling the signal by doing a analogRead(pin7) and analogRead(pin8) while playing some music on my laptop. I connected the audio jack to my the input on my computer.

Image of Setup:
https://drive.google.com/file/d/1CgLiUyHq4KNPDdqdLSA_us_lKHL04DDj/view

Code: [Select]
int LEDPin1 = 8;
int LEDPin2 = 7;
int audio1 = 0;
int audio2 = 0;

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:
  
  pinMode(LEDPin1, INPUT);
  pinMode(LEDPin2, INPUT);
}

void loop() {
  audio1 = analogRead(LEDPin1);
  audio2 = analogRead(LEDPin2);
  Serial.println(audio1);
  Serial.println(audio2);
  
}


This was not working, so I am not sure how I should actually approach the problem. Help would be greatly appreciated. Thank you!!

el_supremo

An analog input can't be a digital input. Remove both pinMode statements. It would be a good idea to give the pins better names than LEDpin1 and LEDpin2 since they have nothing to with LEDs.

Pete
Don't send me technical questions via Private Message.

Grumpy_Mike

As well as using an analogue input pin you need to bias it to 2.5V with a couple of 10K resistors, and then AC couple into that with your audio source.

But what are you trying to do, a Uno is barely fast enough for mono, let alone stero. The maximum sample rate you will get with that sort of setup is a 10K sample rate which is about telephone quality.

We you do get it working the numbers you get will look just like random numbers. The slow serial baud rate you use will also slow you down tremendously.

jenniferliu1272

I am trying to turn lights on base on frequency of my audio input. But I want my audio input to come from an audio jack and not some microphone.

I think 10K Sample rate should be enough for me. But if I want my sample rate to be higher, how would you suggest doing that.


You said "bias it to 2.5V with a couple of 10K resistors, and then AC couple into that with your audio source"

How exactly do I do that? Sorry I am sort of new to this. Do you have a sample circuit diagram or sample set up? Thank you

Grumpy_Mike

#4
Apr 07, 2018, 05:46 am Last Edit: Apr 07, 2018, 05:49 am by Grumpy_Mike
Quote
I am trying to turn lights on base on frequency of my audio input.
Are these real sounds like music?
If so it is very difficult to do this using software alone. Much easier is to use an MSGEQ7 chip, or two if you want stereo.

You can get just the chip or you can get it on a board. msgeq7-arduino-tutorial

Otherwise if you want to go the software route then look up the FFT function. There are a couple of libraries that do this, but using them is not a beginners project.

DVDdoug

Quote
You said "bias it to 2.5V with a couple of 10K resistors, and then AC couple into that with your audio source"

How exactly do I do that?
There is a schematic on this page.  The two 100K resistors form a 50/50 voltage divider that puts 2.5VDC on the input.  The 10uF capacitor isolates the DC bias from the audio signal (while allowing the audio signal through).    In most cases you can leave out the 47nF capacitor.

The bias allows the Arduino to read the negative-half of the AC audio waveform.   (You can subtract-out the DC bias in software, or it's easy to ignore if you're using FFT data.)

Go Up