NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Multiple A/D converters at the same time

November 20, 2009
by Mahcks
Mahcks's Avatar

Let's say that I was trying to read multiple temperatures at the same time. Is this possible with the ATmega168? If so, what would be the best method?

I've tried puting a for loop to cycle through the A/D pins (this is from memory):

while(1){

for(x=0;x<2;x++){

if x==0

admux=0;

if x==1

admux=1;

if admux==0

sample1=adc_read;

if admux==1

sample2=adc_read;

}

}

The problem I run into is that both variables read the same and one is always correct.

November 20, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Mahcks,

First of all, welcome to the NerdKits forums!

There are some errors in your C syntax -- you need to wrap the if-statement expressions in parenthesis, like (admux==0), and you need to put () on the adc_read() to make it actually call the function. But I'm guessing that this is just because you are writing the code here from memory.

You can definitely read from multiple analog inputs with the method you describe. There's an earlier forum thread about this too, which I recommend you look at.

However, if you're specifically using the adc_read() function as in the tempsensor project, you have to be aware that it's actually requesting a new conversion at the end of it's execution, which means it locks in the ADMUX value at that point. This would explain the issue if you noticed that sample1 and sample2 were switched from what you expected.

Can you tell us what you mean by one is always correct? What do you have connected to the inputs? How are you displaying the results (sample1, sample2), and what are they? If you could copy+paste in the actual code and results I think that might help too.

Welcome again, and we'll do our best to help.

Mike

November 20, 2009
by Mahcks
Mahcks's Avatar

That thread really helped. Thanks for the nudge. No need for further assistance!

November 23, 2009
by BobaMosfet
BobaMosfet's Avatar

Mahcks-

Get the ATMEGA168 .pdf/datasheet and read the section specifically about the ADMUX and the A/D Converter. Read slowly, and compare what you read against the tempsensor project. With a couple of very small modifications, you can easily read multiple A/D values (up to the available number of A/D pins).

You also need to make sure you understand exactly what the value is the A/D Converter needs to receive and what it displays, and how you can convert that to a useful number. Remember, it is taking an analog (eg, real-world value- voltage) and trying to stuff that into a finite range of digital values (a scale) without losing too much precision.

I have an old project that reads temperature, and ambient light level.

Post a Reply

Please log in to post a reply.

Did you know that a piezoelectric buzzer can be used to play music with your microcontroller? Learn more...