Wednesday, April 8, 2015

Python on Intel Galileo/Edison - Part 4: ADC

ADC is a peripheral that lets you input an analog signal and gives the digital representation of that analog signal.
The world in which we live in is surrounded by the analog signals. The temperature, sound that hear, the light that we see are all analog signals. If you want to interact or measure these signal in a digital system like Galileo/Edison, you'll have to use ADC a.k.a Analog to Digital Converter.

What is analog signal?
The most common signal in nature is analog signal. These are also what is known as "continuous" signal. If you were to record an audio signal somehow and if you looked up for the signal in any point in time of the recording, you would find a sample there. That is why these kind of signals are known as continuous i.e., there is discontinuity in signal at any point in time. It is like the infinite numbers between 0 and 1 (or any other integer pair). There are infinite numbers between those two numbers. Similarly, there are infinite number of "samples" between two times t seconds and (t+1) seconds. In other words, there is no gap in the information that the signal presents within any two arbitrarily selected times.

What is ADC?
ADC is the one way bridge from the analog to digital world. It takes the analog signal and makes it available to the digital systems like processors in a way in which it can be consumed by them. Like the GPIOs are ways to capture the digital events (button press, can be off or on), the ADCs are for capturing the analog events that are happening around.

How does it work?
The ADCs work by mapping the analog voltage (or current) signal applied to certain numerical value that can be used by the digital system at hand. It achieves this by allocating certain number of bits to represent the analog signal (this defines the resolution of the ADC). It can be 8-bit, 11-bit, 16-bit etc..
The analog signals are sampled at regular intervals which help us represent the infinite samples in a finite way(albeit at loss of some information, which is OK if the sampling is within allowed limits). Also the values of the signals are infinite, the ADC rounds it off to the nearest numerical value which best represents applied voltage this process is known as quantization.
Once an analog signal is applied, the signal is represented with a numerical value relative to what is known a "reference voltage". Hence the maximum numerical value as output by ADC is same as the voltage applied at the ADC is input same as reference voltage. Also this implies that with a given reference voltage, the maximum voltage that you can measure is the reference voltage.  
In Galileo, the reference voltage is 5V and the ADC is of 12 bit resolution and hence the maximum value output by the ADC is 1023.

Hardware connection:
The script below controls the intensity of the LED connected at port D5(using PWM), depending on the position of the "Rotary angle sensor" connected at analog input port pin 0.

The script:
The "Rotary angle sensor" modifies the voltage applied across it depending on the position of the rotor and this voltage is input to the analog input port 0.
We are going to change the intensity of the LED connected at D5 depending on the position of the rotor. Hence the PWM is pin is also setup. The maximum value output by the rotary angle sensor is 1023 and we use this in our calculations to control the intensity of the LED. The mraa module is used to create ADC object using "Aio" method which takes analog in pin as input. On the Aio object the "read" method is used to get the ADC value.

Other parts:
part0:Getting started
part1:GPIO LED
part2:GPIO button
part5:Temperature sensor


  1. "In Galileo, the reference voltage is 5V and the ADC is of 12 bit resolution and hence the maximum value output by the ADC is 1023"
    Why? 2^12 = 4096

  2. Hi,

    Thanks for pointing this out. The resolution on Galileo Gen2 (on which this post is based on) is 10 bit and not 12 bit. However the Gen1 supports both 10-bit and 12-bit resolution.
    This thread explains the confusion around this: