analogRead() in loop() stops Serial.print() from working in setup()

void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.print(analogRead(5)); // reads from microphone
}

I receive the message "test" only if I comment out the line in loop(). Putting a delay in loop() doesn't help. I'm not sure what's wrong here, does anyone want to explain?

I tried this on my Arduino Uno with 0023 and the serial monitor displays the reading from the analog input. Strange that it doesn't work for you.

Ok. I did your code. The analog did read something, and it was going "nuts"...
So I change a bit, put a delay in loop() function, and I have a reading and the test did show. I simply place a piece of wire at analog pin 5. The reading was a floating value ( any values from 0 to 1024 ) which I was expecting.

I don't know what you did ( the hardware section ), but if you need to read a mic, you need some amplification and make sure the signal goes from 0 V to 5 V. Not in the minus range... it will damage your Atmega chip.

Here the test code.

void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.println(analogRead(5));
  delay(1000);
}

I have a Duemilanove, and I need to read the microphone faster than once per second (the faster the better). It just seems like the code never runs properly when I try this. I am sure the microphone is giving values from 0V to 5V, already tested.

Microphone? Can you show your hardware setup?

It's this; SparkFun Electret Microphone Breakout - BOB-12758 - SparkFun Electronics

And I'm sure the circuitry with the microphone works just fine. The code is the problem.

That sounds pretty strange.

I wonder whether the problem is caused by Serial.print() or analogRead(5).

Suggest you try printing an arbitrary int, and calling analogRead() (but not printing the result) and see whether either of them on their own is causing the problem.

Which version of the IDE? Try this instead:

void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.println(analogRead(5), DEC); // reads from microphone
}

Note the println rather than print, and making sure we see decimal.

@xvedejas

That should work fine, as long you connect properly, like +5 to +5 of the Ardiuino, GND with the Ardiuno, and Out at analog pin 5. You should expect a reading value around half of 1024 , if the level of sound in your room is quiet. I use my code modification ( with the delay(1000); or Nick Gammon code, but add a delay(1000); because the serial display will go fast, ( fast scrolling ) Heh you can still use Nick' code and disconnect the Arduino so you see the result of the serial output.

I have a Duemilanove, and I need to read the microphone faster than once per second (the faster the better). It just seems like the code never runs properly when I try this. I am sure the microphone is giving values from 0V to 5V, already tested.

I am sure the code is fine. The way the code is, that the fastest you can get. It only got one line in loop()... it Serial.println(analogRead(5)); That it ! And you should have a reading about 300 to 700 <-- +/- 2.5 V, but on average close to 500.

Now, that make sense.

The reading was a floating value ( any values from 0 to 1024 )

I'd be worried if analogRead returned a value of 1024. :wink:

xvedejas:

void setup()

{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.print(analogRead(5)); // reads from microphone
}




I receive the message "test" only if I comment out the line in loop(). Putting a delay in loop() doesn't help. I'm not sure what's wrong here, does anyone want to explain?

...

xvedejas:
It's this; SparkFun Electret Microphone Breakout - BOB-12758 - SparkFun Electronics

And I'm sure the circuitry with the microphone works just fine. The code is the problem.

Excepting a hardware issue, your code will print "test".

Step 1. Disconnect all external devices (eg. microphones) and cables, except the cable connecting the Arduino to your PC, and run the test again. You should see random values shoot by as the analogRead picks up noise.

OK, AWOL, I mean 1023, happy XD