## 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 ADCs 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 for represent the analog signal (this defines the resolution of the ADC). It can be 8-bit, 11-bit, 16-bit etc..
The values of the continuous 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.

Let us consider an example of an ADC with 8-bit resolution and 5V as it's reference voltage. With 8-bits, you can have bits represent 256 (0 through 255) different values. Now we divide 5V reference to 256 equal parts which comes to  0.01953125V or 19.531 mV (milli volt). This is what is resolution of the ADC meaning that the ADC at hand can only differentiate voltages which are 19.531 mVs apart. Now then next task is to represent these voltages in numbers ranging from 0 to 255. Most logical thing to do is the linear mapping. What this means is that is the voltage applied at the ADC input is 0, the numerical representation would be zero. The representation would be one if the voltage is (1/256)*5V (or 19.531 mv). If the voltage applied is in the range 19.51mV  to 2*19.531mV (remember the quantization?)  then the ADC would output 2 and so on. As a simplified view, ADC can be seen as linear function mapping the input voltage in following manner:

In Galileo, the reference voltage is set 5V via the shield and the ADC is of 12 10 bit resolution and hence the maximum value output by the ADC is 1023.
[edit: The ADC on Gen2 is 10-bit and not 12 bit thanks Thanh for pointing this out]

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 on Galileo/Edison
part1: GPIO output
part2: GPIO input(button)
part3: pwm
part5: Temperature sensor
part6: Light sensor

1. […] Python to program Galileo: part0: This post part1:GPIO output part2:GPIO input(button) part3:pwm part4:adc part5:Temperature […]

2. […] part0: Getting started on Galileo/Edison part1:GPIO output part2:GPIO input(button) part3:pwm part4:adc part5:Temperature […]

3. […] Getting started on Galileo/Edison part1: GPIO output part2: GPIO input(button) part3: pwm part4: adc part5: Temperature […]

4. […] Python on Intel Gali… on Python on Intel Galileo/Edison… […]