Author Options:

How do you use an analog to digital converter? Answered

I am fairly new to electronics and want to know how to use an analog to digital converter with an avr microcontroller preferably in c. I do not mean the built in one.


My advice would be to go straight to the datashet of the ADC you want to use.

Let's take the example of this: ADC081S021CIMF.  It's a surface mount chip made by National Semiconductor.  Specs are 8-bit, 200k samples per second.  The vast majority of external ADCs will output serial data, that is all the output will be on one pin in a string of ones and zeros.

Looking at the datasheet, we see that there are 5 pins, VA, SCLK, SDATA, CS, Vin and GND.  VA is the sensing voltage, GND is obviously ground and Vin is the power supply.

The serial output works as follows.  Each time the SCLK pin goes to logical high and then logical low, the SDATA pin outputs the next bit value.  In your C code this should be a case of toggling the pin connected to SCLK.  The SDATA pin changes on the falling edge of the clock.

The output format is three leading zeros, a byte and 4 trailing zeroes.

You start a conversion by taking the chip select pin to logic low.

In pseudo-code, it could work something like this (imagine this is a function you'd call to check the adc, returning a byte of data:

set CSPIN low

//get the leading zeroes
for(i=0; i<3; i++)
toggle SCLK
//get the actual data
for(i=0; i<8; i++)
toggle SCLK;
adcvalue[i] = read pin SDATA;
//get the trailing zeroes
for(i=0; i<4; i++)
toggle SCLK;

return adcvalue;

Now, you could alternatively just take all the data at once and extract the bits you need afterwards, but something like the above is how i'd do it.  This is a bit-banging approach, if your ports are in short supply you could hook it up to your serial ports or use I2C.

As jeff-o says, there are thousands of circuits available so you'll need to read up on the specific datasheets, but I imagine they're all fairly similar.

Its a shame you weren't using one of the Microchip PIC devices - there is plenty of example code out there (supplied by Microchip) and Microchip also do their own ADC's.
Go on, ditch the Atmel parts and come over to the dark side!
Mind you, the Atmel parts are significantly cheaper than the Microchip equivalents!

PCBPolice Electronics Forum - we need some users....please!

I would recommend using another AVR as an additional ADC and network them together. The easiest way is to use the UART in RS232 mode. A better way is to use the SPI interface, which makes it easier to network more than two devices if required. By using a second AVR, you can perform any signal processing required before sending to the master AVR. You should be able to set your "ADC" AVR to use the internal 2.56V internal reference which is ideal for reading this sensor. For the ADC AVR, I recommend a loop where you read each ADC channel a number of times and average the results. After each channel is read and averaged, change to the next ADC channel and dump the result out of the serial port. By the time the serial data has been sent, the next ADC channel will have settled. Download "ATMEL AVR Studio" and "WinAVR" to get a free development environment and compiler. Hope this helps

What are you trying to measure with your external ADC?

Is the sensor an analog output distance sensor like the GP2D12 by any chance? A part number would help.

Yes it is exactly like the GP2D12. That is the exact sensor.

Sorry for the long list of questions, but why do you want to use an external ADC?

Because there are not enough adc's built in to the microcontroller. And I need four or five of the infrared sensors for my robot.


9 years ago

There are thousands of different A to D converters out there. You could fill several volumes with technical data. I think the best place to start, aside from a good electronics textbook, is to check the manufacturer websites. Try Analog Devices and Texas Instruments to start - spend a while reading some application notes. Once you understand how various A to D converters work, it should be pretty easy to figure out how to interface one with a microcontroller.