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 we 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 DigitalConverter.
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 no discontinuity in the 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 tsecondsand(t+1)seconds. In other words, there is no gap in the information that the signal presents within any two arbitrarily selected times.
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 allow to capture the digital events (button press, can be off or on), The ADCs are for capturing the analog events that are happening around.
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. Since the analog signals are continuous and to record all these information we will need infinite amount of RAM (as there are infinite amount of information between two points in time). To overcome this problem, we sample the signal at regular intervals so that the continuous signal now becomes"discrete". Nyquist rate dictates the rate at which the signal is to be sampled so that we do not lose any critical information. As long as Nyquist rate is met, we are safe.
Once an analog signal is applied, the signal is represented with a numerical value relative to what is known a “reference voltage”(Vref). Hence the maximum numerical value as output by ADC is same as the voltage applied at the reference voltage. Also this implies that with a given reference voltage, the maximum voltage that you can measure is the reference voltage, Vref.
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 represent256(0 through 255) different values. Now we divide 5V reference to 255 equal parts (Maximum value representable is 255 on 8-bit and 0 is already taken) which comes to 0.01960784V or 19.6078 mV (milli volt). This is what is resolution of the ADC meaning that the ADC at hand can only differentiate voltages which are 19.6078 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/255)*5V (or 19.6078 mv). If the voltage applied falls in the range of 19.6078 mV to 2*19.6078mV (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:
ADC_8bit_5Vref(inputVolatge) = floor(inputVoltage/19.6078 mV)
In Galileo/Edison, the reference voltage is set 5V via the shield and the ADC is of 10 bit resolution and hence the maximum value output by the ADC is 1023 (10 bit ADC 2^10=1024).
Grove Rotary angle sensor, also know as the potentiometer(pot) is a electro-mechanical device with a knob. This knob controls the resistance of the pot. It is a three terminal device, first terminal is connected to Vcc (5V or 3.3V) middle pin is connected to the ADC input, in our case and the last pin is connected to the ground.
The knob controls resistance at the ADC input point (the middle pin) there by changing the voltage as measured in the middle pin and the ground in accordance to the Ohm's law. The Ohm's law simply states that the voltage across a circuit is directly proportional to the current flowing through the circuit. The constant in this relation is the resistance. Mathematically put, it is V=I*R where V stands for voltage, I and R stands for current and resistance respectively. As you can easily see varying R has a direct impact on the voltage across the device. The knob controls the position of the probe labelled as A0 in the above diagram this positions the"probe"in different positions, changing resistance which results in varying voltage measured across the pins A0 and ground. This change in voltage across these points (A0 and GND) is what will be measured by the Galileo/Edison.
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(A0).
The “Rotary angle sensor” modifies the voltage applied across it depending on the position of the rotor(knob) 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, A0 in our case as input. On the Aio object the “read()” method is used to get the ADC value.
#!/usr/bin/pythonimport mraaimport timePWM_PIN = 5ADC_PIN = 0 # Analog in pinROT_MAX = 1024.0 # Max value as measured by ADC when pot is connected# Set up the PWMpwm = mraa.Pwm(PWM_PIN)pwm.enable(True)pwm.period_us(5000)# Set up the ADCadc = mraa.Aio(ADC_PIN)while 1: value = adc.read() # Read the ADC value led_intensity = value/ROT_MAX # Determine the duty cycle based on ADC value pwm.write(led_intensity) time.sleep(0.5)
You can find the above script on Github here: https://github.com/navin-bhaskar/Python-on-Galileo...