Humans live in an analogue world but computers are digital. Therefore, if we wish to measure elements of our analogue world, we need a way of converting these measurements to digital. This is the role of the process known as Analogue to digital conversion (ADC). With digital signals, we often deal with binary signals, such as a switch: is it ON or OFF? However, there are many times when we want to look at a condition that has many outcomes, such as temperature, light levels, humidity, speed etc. To sample these, instead of using two voltage levels (such as 0V and 5V), we can look for voltages that fall within a range. In this lesson I will be working with 5V. If I was making a digital circuit I might allocate 0V as 0 and 5V as 1. However, I will use be looking for a range of voltage between 0V and 5V. This range could be broken up into any number of steps, this is known as resolution. If I broke it up into 5, we could have steps of 0V, 1.25V, 2.5V, 3,5V and 5V – this would be a resolution of 5-bits. We could break it up into 10, with steps of 0v, 0.625V, 1.25V, 1.875V, 2.5V, 3.125V, 3.75V, 4.375V, 5V, 5.625V, 6.25V, 6.875V, 7.5V, 8.125V, 8.75V, 9.375V and 10V – this would be a resolution of 10-bits.

RESOLUTION vs ACCURACY

Please note that when I am talking about resolution, I am not talking about accuracy – they are not the same thing! If we were measuring temerature, accuracy would be about how close we are to the actual value. The resolution would be about the smallest change that could be measured.

Arduinos have a built-in ADC. Not all pins can be used, the ones that can be used have an A before their number. The Arduino Uno has 6 pins (A0 to A5) that can be used for ADC. ADCs have different levels of resolution. The Arduino Uno has an ADC with a resolution of 10 bits. This means that it has the ability to detect 2^10, which is 1024 discrete analogue levels. This means that we can now provide an input and sample 1024 different levels. The levels are divided between the operating voltage. If the operating voltage is 5V, then the levels will be mapped between 0V and 5V with 4.9mV per level.

1023 or 1024?

analogReference()

This level can be changed by using analogReference(type). The following can be used (by replacing type:
Using INTERNAL uses an built-in 1.1V reference. Note that you can not use this with a MEGA.
For MEGA use INTERNAL1V1.
The MEGA also has a built-in 2.56V reference that can be used by specifying INTERNAL2V56.
You can also specify a voltage by applying an external voltage to the AREF pin, which must be between 0V and 5V.

Do not use less than 0V or higher than 5V!

You must not apply a voltage less than 0V or higer then 5V.

Set analog reference to external before calling analogRead()

When using an external reference, you MUST set the analog reference to external before calling analogRead(). This is because you will short the internally generated reference voltage and the AREF pin, which could damage the microcontroller!

From this we can see how the analogue input has been converted into a digital reading. Let’s build a circuit that shows how this could be used in a real-world situation. I will build a bargraph that turns on LEDs according to a range that the values fall into. Bargraphs are very useful and have numerous applications, where a level needs to be visualised, such as a display of volume level.

Successive approximation

There are a few techniques for making an ADC. Arduinos use a technique called successive approximation. Successive approximation uses binary search. Binary search goes through different possible levels before settling on an output for each conversion. This is achieved by first using a sample and hold circuit that acquires the input voltage. The job of the sample and hold circuit is to check the voltage without any fluctuation, which could alter the result. This then passes to a comparator that compares the input voltage to the output of an internal DAC. The output of this is passed to a successive approximation register (SAR). The SAR provides a digital approximation of the input voltage. An internal reference DAC supplies the comparator with an analogue voltage equal to the digital output of the SARin.