3
\$\begingroup\$

I have an electret microphone pre amplifier and peak detector circuit that's working great. The microphone picks up sounds and I get a steady DC voltage that, depending on the volume of the sound the microphone is listening to, is somewhere between 0 and ~3.5v

I need to feed this into an ADC pin on a AVR microcontroller though so that I can read in the relative volume that the microphone is getting.

The only problem is that when I connect the circuit to the ADC pin on the microcontroller, my peak voltage drops from 3.5v to about 100mV.

I'm connecting the pre amp and peak detector circuit to an STK board that the chip is running off. When the board is powered off the peak voltage I'm reading with an oscilliscope (and with a multimeter) is correct but it massively drops down when I power it on. I still does exhibit the same sort of behaviour as it should though (the peak rises when there's noise) but I need the peak to be around 3.5v (not 100mV) as I need to a great resolution in the ADC value I read in in my software.

I have connected the grounds of both circuits to the STK board's ground so I don't think that that is the issue.

Can anyone give me any sort of idea about what might be going wrong? Is it something to do with the internal resistance of the microcontroller? Am I perhaps putting too much current into the ADC pin? I'd really appreciate any help.

edit: I'm using pin A7 on an atmega16.

edit 2:

This is the pre amp circuit I'm using: http://www.zen22142.zen.co.uk/Circuits/Audio/lf071_mic.htm

and this is the peak detector/rectifier: peak detector

and the code to get the ADC value:

unsigned int ADC_read(unsigned char ch)
{
    ADMUX = 0b11000111;

    ADCSRA|=(1<<ADSC); // start conversion

    while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
    {
        ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
    }

    return (ADC);
}

I can't see how the code would have any effect though.

\$\endgroup\$
  • \$\begingroup\$ Post your schematic, and software. \$\endgroup\$ – Leon Heller May 16 '11 at 11:23
  • \$\begingroup\$ @Leon done, please let me know if you have any ideas. :) \$\endgroup\$ – Sam May 16 '11 at 11:49
  • \$\begingroup\$ I asked for the code in case you have the ADC input configured as an output that is driven low. Post the initialisation code. Have you checked the ADC with input from a pot? \$\endgroup\$ – Leon Heller May 16 '11 at 11:59
  • \$\begingroup\$ The code is a bit long to post in full.. what should I be looking for though? Writes to DDRA? I am setting all the data direction bits of A to 1, could that be it? \$\endgroup\$ – Sam May 16 '11 at 12:04
  • \$\begingroup\$ And I am unfamiliar with the term pot.. any links would be great. :) \$\endgroup\$ – Sam May 16 '11 at 12:04
4
\$\begingroup\$

Are you absolutely sure the pin/port is configured as an analog input, and not a digital output that's been set low?

An accidental digital low on the port could be what's loading the peak detector output.

I'm not an expert on your particular architecture, but in general, micro I/O pins are multiplexed - you need to explicitly set registers to configure them as inputs, outputs or ADC channels. Setting up an internal ADC still requires you to manually configure its input pin appropriately.

\$\endgroup\$
  • \$\begingroup\$ Actually, I just checked my code and I do have DDRA = 0xFF in there but I don't ever set PORTA to anything. Could that be the problem? The weird thing is that the ADC does still work with the pins set to be an output.. I get a value from 0 to 100 out depending on how much voltage the mic is putting out. Is that expected behaviour? I can't test anything again till tomorrow. \$\endgroup\$ – Sam May 16 '11 at 12:13
  • \$\begingroup\$ Additionally, do I have to set DDRA to anything to use one of its pins for ADC? Or will the ADC code I posted do everything I need? \$\endgroup\$ – Sam May 16 '11 at 12:14
  • \$\begingroup\$ @Mad - Your answer is overly brief, and could probably be a comment. Could you explain how the pin might be configured as an output and set low, and why this would cause the OP to observe the problematic behavior? \$\endgroup\$ – Kevin Vermeer May 16 '11 at 13:25
  • \$\begingroup\$ agreed. and fixed up. \$\endgroup\$ – Adam Lawrence May 16 '11 at 16:33
4
\$\begingroup\$
  1. If DDRA = 0xFF it is all outputs.
  2. If you haven't written to PORTA it will default to 0x00 so all pins will be low.
  3. The ADC just works (TM) so it will happily read the result of the low output fighting with the preamp/rectifier just as happily as it would read any other voltage on the pin.
  4. You don't have to do anything to make the ADC work, except make the pins inputs...
\$\endgroup\$
1
\$\begingroup\$

Check so there is not a internal pull-down inside the mcu that is enabled for that pin?

\$\endgroup\$
  • \$\begingroup\$ Thanks Johan! How can I check this though? \$\endgroup\$ – Sam May 16 '11 at 10:55
  • \$\begingroup\$ I have not worked with the AVR:s for a while, but first check that datasheet if there is any pull up/down:s to begin with. Then I think you play with the fuses to enable/disable them. (but I'm not sure) \$\endgroup\$ – Johan May 16 '11 at 11:01
  • \$\begingroup\$ This answer is a little brief. Can you expand a little? Readers might be unfamiliar with the states in which you'd have a pull-down resistor enabled. Also, it would be good to explain why this would cause the behavior that the OP is observing. \$\endgroup\$ – Kevin Vermeer May 16 '11 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.