Simple Audio input - not working as expected

Hi all,

I am trying to get audio input from an external source.

It's a simple setup, Guitar mono input jack + goes to A0 to read the input and the - to Ground.

I have had this working before but It's been a few iterations back and forth. The end goal is to control a servo based on the audio data.

The serial monitor now only displays values around 200 - 400 and the data is the same when the input is unplugged. IE it's not reading. Any help is appreciated

 int sensorPin = A0; // Audio input



void setup() {

// put your setup code here, to run once:



pinMode(sensorPin, INPUT);

Serial.begin(9600);



}



void loop() {

// put your main code here, to run repeatedly:



int noiseLevel = analogRead(sensorPin);



Serial.print("Volume is :");

Serial.println(noiseLevel);

delay(200);



}

In case image isn't showing of my wiring
Imgur

Serial monitor

Thank you all :slight_smile:

You didn't say what the audio input source is.
But if you trying to sense audio directly from the guitar pickup those signals are low. Also they will swing negative to zero which the MCU will not respond to.

apologies, sound is coming from PC speaker 3.5mm jack output to the guitar lead input. IE no amp required.

I think the arduino reads from 0 to 5 volts. If the output gives millivolts, then you need to amplify that signal.
and if you give more than 5 volts it can be a problem...

If you change the single quotes around the 'Volume is :' to double quotes like this
"Volume is :" i think you will be back in business.

Ah, forgot about that. Copy and pasted from another time. Changed to “” instead of ‘’ and same issue. I updated that in the original post due to my error of pasting wrong code.
I also updated with image of wiring and the Serial monitor results

sound is coming from PC speaker 3.5mm jack output to the guitar lead input

I found it a bit difficult to understand this statement. Are you saying A0 is connected directly the loudspeaker? This will destroy the MCU - analogue inputs have clamping diodes the negative input swings will conduct down to zero volts.
At least use a series limiting resistor, 10K or so.

What is the peak signal level going into A0?
It sounds like you are wanting a peek level detector? On the input you just need a series diode and capacitor + resistor to 0V.

Cheers,

Yes the audio output from PC soundcard, then the other end goes into the Guitar input jack, + to A0 - to Ground.

I'm pretty new still so I was following this tutorial:

https://create.arduino.cc/projecthub/ben-eagan/big-mouth-billy-bass-alexa-820481?fbclid=IwAR2TiHJ72AogmiI3lUDUnl_vJWvRtuGPFDbog9bsCiZ_aZcPtMX5b9sDyO0

Not sure about frequency etc, sorry...

Just need to move a puppet mouth (servo) to the input provided..ie Recorded voice mp3 (no music)
This will later get repaced with live input to the PC via microphone (again output via the soundcard into the arduino)

Am I taking the wrong approach all together?

Ensure that the computer isn't muted via hardware. Press any external mute buttons, confirm that the speakers are turned on, and turn the volume all the way up. Test by playing a song or using the Sound control panel (click the Sounds tab, select Asterisk, and click Test). If that doesn't work, check Windows Upsers

thanks, gave that a shot to confirm, even when the output is removed from the pc (confirmed it's making noise) I'm getting the same readings. I would expect them to be 0 when disconnected.

A floating analogue input because of it's high impedance will pickup noise especially mains hum, so you won't see zeros. Disconnect jack plug and connect A0 input to ground, you should see zero readings then.
As another test you could connect a battery, less than 5V, to the input. The readings will be the battery voltage/5 * 1023. So for 1.5V battery readings will be approx 1.5/5 * 1023 = 307.
If that looks OK then the issue is with the input signal. Make sure the + battery terminal connects to the A0, otherwise the MCU will break. As mentioned it's best to connect a series resistor when playing with analogue inputs.

The approach will work, it may need a signal amplifier and a few components on the input. Report back I can send you a simple diagram if you want.
Can't silence the puppets!

Thank you, threw in a 10k resistor between A0 and the input

Testing results

A0 to ground - Volume 0 : Great

Testing with 2.5v on PSU via resistor to A0

Got 327 / 328 seems pretty close with what you said.

So in theory the input / cable is the issue. Problem is I have used multiple cables.
The output jack from the speakers on my pc works fine for headphones, could it seriously be that causing the issues?

Thanks again, greatly appreciate your input.

If you want to be certain the lead is OK, you could just unplug it from the PC end and repeat the previous two tests by zeroing and connecting the battery (via the 10K) at the 3.5mm jack. See you get the same results.

If that checks out what happens to the readings when you turn the PC volume down - do the readings also go down?

I'm also wondering if there is enough signal coming from the sound card, typical line output levels could be anywhere from 0.5V peak to max of 1.5V or so peak.

Start by checking the lead then turn the PC volume down to zero see if the readings reduce to near zero, if they don't there may be a ground loop between your arduino power supply and the PC. If so try putting a capacitor in series with the input to A0 this will block any DC voltage between your arduino and the PC but pass the audio signal through.

Thanks again, valuable information.

Testing as follows

Volume control does not change the readings.

Tested on a laptop headphone out plug, same thing random numbers, same removing the plug.
Good to replicate results on another audio source, bad because it means I still have to troubleshoot.

Tested by putting 1.5v + and - on one end of the 3.5mm jack, 10k resister to A0 and - to earth.

Started giving me the expected 327 / 328 again. So I guess I can rule the lead out.

As for the capacitor, do you know what size etc, im pretty new to the smaller things?

I do have a 2 Channel 3W PAM8403 Audio Super Mini Digital Amplifier I tried earlier to see if the signal was too low and I got the same kind of results.

Again, thank you I appreciate your efforts.

When discussions get to this point, it's futile to continue without a clear wiring diagram, and/or clear photographs of the wiring.

There is a circuit on this old thread you may want to consider using, looks like some good information.

Capacitor value 0.22uF to 1uF none polarised.
You can see input circuitry in the useful link given by Bue Eyes.

I would use an amplifer module like ths one it can be powered from arduino 5V. The DC blocking capacitor will be required.

I wonder if progressive learning can be futile. I agree a clear wiring diagram including input source would make for more relvant answers.

oneis:
I wonder if progressive learning can be futile.

When you're working alone, no. When other people are involved, yes.

Thanks again all

Here is the wiring diagram and 2 of my real life shots.

Nano is powered from USB port on PC.

For sanity sake I also tried

A new Nano, a new shield and testing without the shield.

Same issue.

Will give the diagram in thread linked and give that a shot, if it fails I'll add in an amp.

Thanks all :slight_smile:

Ok, so I went back to the drawing board and as suggested had a look @ How to read data from audio jack? - Audio - Arduino Forum

I have now created the circuit from the thread and using my same original code (below to save the scroll) I am getting 0 with audio input attached or not.

I'm assuming it will be my wiring or the use of incorrect components.

The setup is attempting to replicate the below:

int sensorPin = A0; // Audio input

void setup() {

Serial.begin(9600);
}

void loop() {

int noiseLevel = analogRead(sensorPin);

Serial.print("Volume is :");

Serial.println(noiseLevel);

delay(100);

}

I'm not really across the component side of things so took the diagram and comments in the thread as a shopping list to my local Jaycar.

Here are the componets in case they are incorrect (sorry for the head turning, apparrently Imgur rotations don't stick when linking)



Additionally here are the photos of my wiring



Hopefully that should cover everything required to help me :slight_smile: my sanity is going...going....

Thanks again all.