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.

Sensors, Actuators, and Robotics » Using two different sensors on single chip

April 06, 2012
by rmore
rmore's Avatar

Hello All!

I am not sure if this sort of question has been asked before. If so, kindly direct me to the appropriate link.

My question is related to the temp_sensor project in Nerdkits. So we have a temperature sensor connected to the ADC0 pin. Also, from the data-sheet and the code, I gather that while the ADC conversion is happening Bit 6 (ADSC) is '1'. This bit turns to '0' once the conversion is complete.

Now if I had another sensor (perhaps a distance sensor) attached to - say pin ADC1, and I do a similar ADC conversion for this pin.

Then how will things occur in this case? The ADSC bit value will depend on which sensor now?

How does one generally use multiple sensors on a single chip?

Thank you for your replies in advance.

Regards

rmore

April 06, 2012
by pcbolt
pcbolt's Avatar

@ rmore -

That is where the multiplexer register (ADMUX) comes into play. You can set the lowest 3 bits to indicate what channel (PIN #) you want to sample. There are some rules to follow so check the datasheet for when it is "safe" to change channels. Generally, you can sample channel 1, get a result, change to channel 2, start a conversion, get result etc.

April 07, 2012
by esoderberg
esoderberg's Avatar

Rmore,

Here are two functions you can call to read multiple ADC inputs serially. The first function just inits the ADC, you only need to call it once. Call the second function anytime you want to read a particular ADC pin (just pass in the ADC channel you want to read).

void adc_init() {
//set analog to digital converter 
// analog input
ADMUX = 0;
ADMUX |= (1<<REFS0);  //sets reference voltage via internal connection to AVcc
//ADEN TURNS ON THE ADC
//ADPS2,1,0 sets conversion timing, pg 256 datasheet
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
//ADSC starts a conversion from A to D
ADCSRA|= (1<<ADSC);

}

// reading input to ADC

uint16_t adc_read(char mux) {
ADMUX = (1<<REFS0) | mux;

//while(ADCSRA & (1<<ADSC));

// set ADSC bit to get the *next* conversion started
ADCSRA |= (1<<ADSC);

while(ADCSRA & (1<<ADSC)) {
//Do nothing while ADCSRA ADSC bit is 1.  ie wait until last ADC complete to start next. 
}
//ADCL and ADCH is where the ADC writes output.  2 var used because output greater than 8  bit.
//ADCL is low bits ADCH is high bits
//return ((ADCL) +(ADCH<<8)); or ADCW does the same as this function
return ADCW;
}

//call function with adc_read(x) with x being the ADC channel you want converted
April 07, 2012
by Ralphxyz
Ralphxyz's Avatar

Search the forum for "multi sensors" to see some good discussions.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that our customers love us? Learn more...