Open In App

Analog to Digital conversion in Arduino

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about the working of analog input in Arduino Uno R3. Also, learn how to read and write analog data using Arduino Uno R3.

Arduino:

It is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontroller in the Arduino. It provides Arduino IDE to write code & connect the hardware devices like Arduino boards & sensors.

Analog pins in Arduino:

Arduino has 6 analog channels for reading analog signals of 0 to 5 Volts. Each channel has a separate analog pin.  ATmega328P microcontroller has an inbuilt Analog Digital converter with 10-bit resolution. Analog pins in Arduino only can read the analog input. It cannot reproduce the analog voltage.

                        Channel                                 Pin      

Channel 0

A0

Channel 1

A1

Channel 2

A2

Channel 3

A3

Channel 4

A4

Channel 5

A5

Resolution of ADC:

Digital data is in the form of 0 or 1 ( LOW or HIGH ). Each bit has two possible combinations. If the resolution is N bit, the binary combinations will be 2 power of N.

Resolution = ( 2^N ) – 1

In the case of 10-bit ADC,

The resolution of the 10-bit ADC is ( 2^10 )-1= 1024 – 1 = 1023. The digital value is a zero-based index. So the range is 0 to 1023.

Voltage Range:

The voltage range of the ADC is varied as per the reference voltage. The reference voltage is the maximum voltage that can be measured using the ADC. The reference value can be varied based on the requirement or manufacturer.

However, We can’t change the reference voltage of the Arduino Uno R3 which is 5 Volts.

Therefore the range of inbuilt ADC in the Arduino R3 (ATmega328P) is 0 to 5V.

Analog to Digital Conversion:

Formula:  Digital value = ( Input value / Reference voltage) * Resolution

Example calculation:

If the Analog input is 2.8 Volts, the digital value for 10-bit resolution can be calculated as follows.

Digital value = ( 2.8 / 5 ) * 1023 = 573 (Approximately)

Likewise,

For input of 0 volts, the Digital value is 0,

For an input of 5 volts, the Digital value is 1023.

Components Required:

  1. Arduino Uno R3
  2. Signal source ( 0 to 5 V )
  3. Jumper Wires

Circuit Diagram:

Analog pin circuit diagram

Note: Function generator is used here for understanding purposes. You can any voltage between 0 to 5 volts to convert the analog signal to a digital value.

Function Generator:

A function generator is test equipment used to generate different waveforms like Square waves, Triangular waves, Sine waves, etc.

Setup:

  1. Connect the signal source to any of the analog channels Ex: A0.
  2. Connect the ground of the signal source to the GND pin.
  3. Upload the code to the Arduino.
  4. Monitor the serial output at a 9600 baud rate.

Note: Grounding is very important. In case the signal source is a battery, connect negative terminal of the battery to GND. If the signal source is from any circuit, connect Ground of the circuit to the GND of the Arduino.

Syntax:

analogRead(channel);

// The analogRead function returns the digital value of the analog input signal.
// It has 6 six channels ( 0 to 5 ) 
// Example: analogRead(0); // Selected channel is A0

Arduino Code:

C++




void setup()
{
    Serial.begin(9600); // Serial monitor at 9600 baudrate
}
void loop()
{
    int digitalValue
        = analogRead(0); // Channel 0 (A0) is selected
    Serial.print(
        "Digital value: "); // Print text in serial monitor
    Serial.println(
        digitalValue); // Print Digital value in serial
                       // monitor ( 0 to 1023 )
}


Output: 

The sinusoidal wave is given as input. The peak-to-peak voltage level is 5 volts. The nature of the sine wave is AC. Which contains negative voltage  ( 2.5 to -2.5 ). So Offset voltage is set to 2.5V (half-cycle voltage ). It raises the entire sine wave into the positive side without changing the shape of the sine wave. The voltage of the sine wave varies from 0 to 5 Volts ( Peak to Peak ).

analog Read output

The increment and decrement of Digital value occur due to sine wave input. 

Note: Output will be varied based on the input voltage to an analog pin. A function generator is used for understanding purposes.

Output Analysis:

 

Removed “Digital Value: ” text from the code to plot the point using serial plot.

The output values are plotted in the Serial plotter. Obtained Digital values make a sine wave that is similar to the input. The output varies from 0 to 1023 as a Sine function. 

Applications of ADC:

  1. Digital Signal Processing
  2. Analog sensor reading ( Temperature sensor, Light sensor, etc.)
  3. Voltage sensing ( Voltmeter )


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads