Introduction to ADC
When interfacing sensors with a microcontroller, the sensor’s output is often analog. However, microcontrollers process digital signals, which is why we use an Analog-to-Digital Converter (ADC) to bridge this gap. The ADC converts the analog signal into a digital format that the microcontroller can process.
ADC finds applications in various fields such as biometric systems, environmental monitoring, and gas leakage detection.
The Arduino Uno has 6 built-in ADC channels, capable of reading analog signals in the range of 0 to 5V. It features a 10-bit ADC, which means it can output a digital value ranging from 0 to 1023 (2^10). This value range is known as the ADC’s resolution, indicating the number of discrete values it can generate within the range of analog inputs.
Digital Output Value Calculation
The resolution and digital output are calculated as follows:
ADC Resolution= Vref/ADC Resolution
Digital Output = Vin/Resolution
Where:
- Vref: The reference voltage, which is the maximum voltage the ADC can convert.
- For simplicity, we assume Vref to be 5V.
Example Outputs:
- For ( V_{in} = 0 )V, the digital output is 0.
- For ( V_{in} = 5 )V, the digital output is 1023 (10-bit).
- For ( V_{in} = 2.5 )V, the digital output is 512 (10-bit).
ADC Pins of Arduino Uno
Analog Functions for Arduino ADC
analogRead(pin)
This function reads the analog value from the specified analog pin.
- pin: The number of the analog pin you wish to read from.
- Returns: A digital value between 0 and 1023, corresponding to the input voltage.
Example:
analogRead(A0); // Reads the analog value at pin A0
analogReference(type)
This function configures the reference voltage for analog inputs.
let’s see How to Read Analog values using Arduino
Let’s write a program to read the varying analog value generated by a potentiometer connected to the A0 analog pin. The program will display the digital value obtained from the Arduino ADC on the Serial Monitor.
Potentiometer Interfacing with Arduino Uno
Potentiometer connected Arduino ADC Channel
Arduino Code for reading analog value
int sensorPin = A0; // input pin for the potentiometer
int digitalValue = 0;// variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
digitalValue = analogRead(sensorPin);// read the value from the analog channel
Serial.print("digital value = ");
Serial.println(digitalValue); //print digital value on serial monitor
delay(1000);
}
Output on Serial Monitor
Note: If nothing is connected to the analog input channel, the analogRead()
function will return fluctuating, noisy values.
Read Analog Voltage Using Arduino Uno
Since the ADC provides a digital output that is proportional to the input analog value, we can convert the digital value back to the corresponding analog voltage using a simple formula.
To convert the digital value to the analog input voltage:
Aout = digital value * (Vref/2^n – 1)
Where:
- Aout is the output analog voltage.
- digital value is the value returned by the
analogRead()
function. - Vref is the reference voltage (typically 5V for Arduino).
- n is the resolution of the ADC (10-bit for Arduino Uno, so ( n = 10 )).
Example Calculation:
For a digital value of 512, with a 10-bit ADC and a 5V reference voltage:
Aout = 512 * (5 V/ 1023)
This simplifies to:Aout=2.5VA_{out} = 2.5VAout=2.5V
So, the analog voltage corresponding to the digital value of 512 is 2.5V.
Code for Read Analog Voltage using Arduino
int sensorPin = A0; // select the input pin for the potentiometer
int digitalValue = 0; // variable to store the value coming from the sensor
float analogVoltage = 0.00;
void setup() {
Serial.begin(9600);
}
void loop() {
digitalValue = analogRead(sensorPin);// read the value from the analog channel
Serial.print("digital value = ");
Serial.print(digitalValue); //print digital value on serial monitor
//convert digital value to analog voltage
analogVoltage = (digitalValue * 5.00)/1023.00;
Serial.print(" analog voltage = ");
Serial.println(analogVoltage);
delay(1000);
}
Output on Serial Window
For more implementation, you can visit