I got issues with analogRead

Apologies for my poor english

please consider those source codes with the corresponding output:

CASE 1

const int pinMic=0;   //sound pressure level in the room
const int pinLedB=9;  //yellow led
void setup() {
  Serial.begin(19200);
}
void loop() {
  int r=analogRead(pinMic); 
  Serial.println(r); 
}

On serial monitor I got something like this:
58
58
61
61
61
60
60
60
60
59

CASE 2

const int pinMic=0;   //sound pressure level in the room
const int pinLedB=9;  //yellow led
void setup() {
  Serial.begin(19200);
}
void loop() {
  int r=analogRead(pinMic); 
  Serial.println(r); 
  delay(200);
}

Serial monitor:
5
3
3
2
9
7
5
4
5
5

CASE 3

const int pinMic=0;   //sound pressure level in the room
const int pinLedY=9;  //yellow led
void setup() {
  Serial.begin(19200);
  analogWrite(pinLedY, 100);  
}
void loop() {
  int r=analogRead(pinMic); 
  Serial.println(r); 
}

Serial monitor:
149
149
149
149
150
150
150
150
150
149

and so on…

Or course all the reading was made in the same silent room.

I am very confused, because I did not expect to see analog read be dependent by the source code.
How can i develop a project if analog read is affected by things like a delay or a pwm out?
Could it be an issue with my board or am i missing some important concept about arduino programming?

Thank you in advance

The big question here, how did you connect everything?

Because analogRead() isn’t dependent on the rest of the code. Only maybe if you left the input floating. So not a code problem, but a connection problem :wink:

Everything is connected in the same way, you can see the board in the attached picture.
The difference in the result depends on the code not the wiring because the wiring remain the same.
If I add a delay i got certain readings, if i remove the delay i got the previous reading
If i ad a analogWrite I got certain readings , if I remove it I got the previous reading
The result are CONSISTENS with the code changes

noise0:
Everything is connected in the same way, you can see the board in the attached picture.

That picture is useless. All we see is a bunch of wires. Make a proper schematic (no fritzing breadboard view).

And did you test that proto board? What’s on it?

And if you want use to triple check the wiring make a photo we can actually see all the wires.

noise0:
The difference in the result depends on the code not the wiring because the wiring remain the same.
If I add a delay i got certain readings, if i remove the delay i got the previous reading
If i ad a analogWrite I got certain readings , if I remove it I got the previous reading
The result are CONSISTENS with the code changes

Then probably that analog input is just floating. Then reading it can influence it and because of capacitive coupling a analogWrite() (or any signal) can influence it as well.

I'm very confused by your photo - for example, it looks to me like you've got the analog input and the Arduino's GND connection both connected to the ground rail of the breadboard. (the blue wires)

I don't think the microphone board is connected to the analogue input.

well, the output of the spl board is connected to arduino analog in (pin 0).
Pin 0 is not floating.
And it works perfectly, I mean that it detect variations in sound pressure,
the only issue is about the base level (no sound), it apparently depend by how is written the code.

I attach now the picture of the electric diagram of my project.
I hope anyone can explain me this strange phenomenon.

thx

I attach now the picture of the electric diagram of my project.

In that diagram, the microphone circuit ground is not connected to the Arduino ground.

sorry if i did not put the connection in the diagram, but of course bot the boards are connected to the same vcc and gnd

Then I still suspect a wiring error. Because without an analogRead does not depend on what the Arduino does or controls. But unable to tell with that photo...

Consider that the circuit is very simple, my sound board have 3 connections:
-VCC
-GND
-OUTPUT

VCC is connected to VCC arduino pin
GND is connected to GND arduino pin
OUTPUT is connected to arduino analog in pin 0

I have attached a new photo of the board… but it is a so simple configuration that i can not figure how it could be wrong.

thx

Apart from some crappy cables it looks okay. Assuming the board and pinout is correct.

But there must be a software error somewhere because the read isn't influenced by other setting and the code is simple. Give it a try with A0 connected to GND or a random voltage divider.

With a voltage divider it works fine.. i mean that readings do not depends on the code, so there must be something wrong with my audio circuit.

But I tested the audio circuit with the oscilloscope and it seems ok, there is no noise on the output, it's perfectly smooth and clear. The output signal is proportional to the sound pressure in the room.

Then i connected the audio circuit to arduino and the oscilloscope still show me that the signal is ok

But arduino read values depending by how the code is written...
This issue is driving me crazy :frowning:

But how fast does that signal change?

And is the power supply of that opamp connected? (Beside no connection between Arduino and the board you also didn't draw the supply of the opamp)

Yes, TLC272 is connected to vcc and gnd.
I attach 2 new images
The 1st one is the electric schema of my audio circuit with all connection in place. I hope now it is correct.
The 2nd one is the oscilloscope connected to out pin

OUT PIN → Arduino analog in 0
VCC → Arduino 5V pin
GND → Arduino GND pin

Arduino take power supply from USB

noise0:
I hope now it is correct.

Nope, still missing :wink: Hint, pins 4 and 8 of the IC :slight_smile:

noise0:
The 2nd one is the oscilloscope connected to out pin

When connector or disconnected from the Arduino? Also, a scale would have been useful.

pin4 -> gnd
pin8 -> vcc

output range from 0 v to approximately 4 v UNCONNECTED
wait let me chk again if connected is the same

I have made some interesting discoveries

My previous test with arduino connected was made with external vcc from a power supply unit because the oscilloscope is far from my pc

now I have made the correct test with arduino connected to USB and i have found that electric signal is coherent with serial readings,

please check the attached image!!

USB power supply make the signal noisy, and when I remove the delay there is and offset on the electric signal on the OUT pin of the audio circuit regardless if it connected or not to arduino.

If i remove all the code from the main loop like this:
void loop() {
}

there is still a bit of noise but it is lower.

So for me now it is clear that my audio circuit suffer of some interference on the USB power rail.
And this interference is way bigger if arduino compute something.

do it have sense?

I HAVE PLACED A BIG CAP (2200uF) on USB power rails and now it works fine!!!! :smiling_imp:

I did not imagine that usb power supply could be the problem, To be more precise the 5v output of arduino when connected to usb. It seems that it suffer of a lot of noise especially when arduino is working on some code.