# Introduction

ADC stands for Analog to Digital Converter. In the real world, the value of signals like light or sound can usually be measured in an analog way but, for these values to be processed by a micro-controller, they must be converted into digital information. The ADC does this work and is embedded in all modern micro-controllers.
So an ADC circuit is the most common way of sensing the outside world but it's not so smart. It can only measure voltage levels at a certain moment in time and you need another piece of hardware, a sensor, in order to convert your analog value into a voltage to work with the ADC and become digital information. Some common types of analog sensors are those for light, sound, temperature, humidity, various type of gas, electricity sensor, and many others.
What the ADC circuit does is to take samples from the analog signal from time to time. Each sample is converted into a number which represents the value at a certain moment in time of the waveform of the analog signal.
To use a well known example, a CD contains digital samples of music, which are the analog to digital conversion of the audio (which in reality is a continuous waveform) with a sampling rate of 44.1Khz. This mean that, 44,100 times a second, its audio waveform is sampled into a number with a resolution of 16 bits: a number from 0 to 65,535. Why 65,535? Because each sample is represented as 16 bits, each of which can take one of two different value (0 and 1) and that gives you 216 = 65,536 different possible values, which are all the numbers from 0 to 65535.
In every analog to digital conversion, we have this two important parameter, the frequency or sampling rate and the number, the sample itself, with a fixed resolution in bits. The resolution is the accuracy of the sampled data.
Mizar32’s ADC makes samples at a resolution of 8 or 10 bits, which gives 256 or 1024 different possible values. That’s less precise than the high fidelity of an audio CD but it is precise enough for measuring temperatures, pressures, the intensity of light and most other physical signals.
To explain the concept of resolution better, the value of each sampled point represented in our input waveform will be stored in a fixed-length variable. If this variable uses eight bits, this means it can hold values from 0 to 255 (2$^8$ = 256). If this variable uses 10 bits, this means it can hold values from 0 to 1023 (2$^{10}$ = 1024). In the case of 10 bits resolution, the number 0 represents the lowest voltage and the number 1023 the highest.
The type of ADC used in AVR32 is a Successive Approximation Register (SAR) ADC, which repeatedly compares the signal it is sampling with its current best estimate until it has the closest value and will find the correct digital value for the sample in 10 clock cycles in the case of 10 bit resolution.
Another advantage of this type of circuit is the use of an output buffer, which allows the circuit that is fed by the ADC to read the digital data while the ADC is already working on the next sample.

# Hardware view

The Mizar32 has one analog-to-digital converter that is multiplexed between up to eight inputs, ADC0 to ADC7.
The ADC channels can be set in hardware to perform 8-bit or 10-bit conversions, and the range of input voltages is from 0V to VDDANA (BUS1, pin 9) which is tied to 3.3V on the Mizar32 main board via R3, a 0-ohm resistor.
| Signal | GPIO | Bus pin | PicoLisp | Notes |
| ADC0 | PA21 | BUS5 pin 5 | 'PA_21 | |
| ADC1 | PA22 | BUS5 pin 6 | 'PA_22 | |
| ADC2 | PA23 | BUS5 pin 7 | 'PA_23 | |
| ADC3 | PA24 | BUS2 pin 3 | 'PA_24 | (1) |
| ADC4 | PA25 | BUS6 pin 4 | 'PA_25 | |
| ADC5 | PA26 | BUS6 pin 5 | 'PA_26 | |
| ADC6 | PA27 | BUS6 pin 6 | 'PA_27 | |
| ADC7 | PA28 | BUS6 pin 7 | 'PA_28 | |
(1) ADC3, which shares a bus pin with the Ethernet interrupt, can be used as as an ADC input when an Ethernet add-on board is not present.

# Software view

Hempl's ADC module is used to configure and read the ADC inputs. A minimum of two function calls is required: one to start the conversion and another to read the resulting values, which range from 0 (at 0V) to 1023 (at 3.3V).
In PicoLisp:
If an ADC pin is not used as an ADC input, it can be used as as a generic PIO pin by calling the setdir function in the PIO sub-system. For example, to use BUS5 pin 7 (ADC5) as a PIO output instead of an ADC input, you could use the following:
Note that ADC's setclock function is not implemented on AVR32. See issue #25.

# Further reading

* The Atmel AT32UC3A datasheet Chapter 33: Analog-to-Digital Converter (ADC).