Analog to Digital conversion in Arduino
Last Updated :
30 Apr, 2024
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:
- Arduino Uno R3
- Signal source ( 0 to 5 V )
- Jumper Wires
Circuit Diagram:
Analog pin circuit diagramNote: 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:
- Connect the signal source to any of the analog channels Ex: A0.
- Connect the ground of the signal source to the GND pin.
- Upload the code to the Arduino.
- 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 ).
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:
- Digital Signal Processing
- Analog sensor reading ( Temperature sensor, Light sensor, etc.)
- Voltage sensing ( Voltmeter )
Similar Reads
Electronic Devices & Components
Avalanche PhotodiodeAn Avalanche photodiode (APD) is a highly sensitive semiconductor detector that uses the photoelectric effect to convert optical signals into electrical signals. In case of conventional photodiodes, PIN photodiodes operate in a linear mode and the output current is very small and also has limited ga
4 min read
PIN DiodeA PIN diode, abbreviated as a Positive-Intrinsic-Negative diode, is a semiconductor tool that belongs to the diode family. Power electronics play an essential role in converting and controlling electrical strength in numerous packages, ranging from electricity components and motor drives to renewabl
15+ min read
Schottky DiodeThe Schottky diode is popularly known as a hot-carrier diode, low-voltage diode, or Schottky barrier diode. It is a type of diode formed by the junction of a semiconductor with a metal. We know that in a PN junction diode, p-type and n-type semiconductors are joined collectively to form a PN junctio
13 min read
Solar CellA Solar Cell is a device that converts light energy into electrical energy using the photovoltaic effect. A solar cell is also known as a photovoltaic cell(PV cell). A solar cell is made up of two types of semiconductors, one is called the p-type silicon layer and the n-type silicon layer. So Solar
8 min read
Power Semiconductor DevicesIn electronic components, Power Semiconductor Devices are electronic devices that need an external power source to operate actively. Materials used to make semiconductor devices are neither excellent insulators nor conductors. They mainly manipulate, amplify, switch, or control the flow of electric
15+ min read
Digital Circuits & Logic
Analog & Circuit Behavior
RectifierA rectifier is an electronic component that converts alternating current (AC) into direct current (DC), ensuring a unidirectional flow of electric charge. As we know, electricity reaches our homes from power grids in the form of AC, but most commonly used electric appliances, such as mobile phones,
10 min read
Diode As a SwitchThe diode is an electronic instrument of great importance, one of them being using the diode as a switch. It is interesting to note that under certain conditions we can use a diode as a switch. In this article, we will study what is an electrical switch and how it differs from a mechanical switch. W
11 min read
Forward BiasForward Bias is a term commonly used in semiconductor electronics, particularly concerning diodes and transistors. A diode is considered to be in forward bias when its p-side is connected to the positive side of a power supply and its n-side to the negative side. In the case of an NPN transistor, th
9 min read
Schmitt TriggerWe use Schmitt Triggers in digital electronics. It is used for rejecting the noise from the input signals by using the hysteresis curve. To provide accurate and more stable results, it uses two threshold voltages i.e., upper threshold voltage (VUT) and lower threshold voltage (VLT). It is mainly use
11 min read
Integrated CircuitsIntegrated circuits (ICs) also called microelectric chips are used to create a device that can perform certain electrical operations such as signal amplification which is called a transistor from the combination of words of the transfer and resistor. Solid-state electronics is the name given to the
8 min read
Solid-State Devices
Solid-State LightingIn this article, we will study solid-state lighting, which is the use of solid-state materials as a source of illumination, usually light-emitting diodes (LEDs). Solid-state lighting, as opposed to conventional incandescent or fluorescent lighting, which depends on thermal processes, produces light
7 min read
Avalanche PhotodiodeAn Avalanche photodiode (APD) is a highly sensitive semiconductor detector that uses the photoelectric effect to convert optical signals into electrical signals. In case of conventional photodiodes, PIN photodiodes operate in a linear mode and the output current is very small and also has limited ga
4 min read
Avalanche PhotodiodeAn Avalanche photodiode (APD) is a highly sensitive semiconductor detector that uses the photoelectric effect to convert optical signals into electrical signals. In case of conventional photodiodes, PIN photodiodes operate in a linear mode and the output current is very small and also has limited ga
4 min read
MOSFET CharacteristicsMOSFETs (Metal-Oxide-Semiconductor Field-Effect Transistors). It is a transistor that controls the flow of electric current in the circuit. The MOSFETs have 3 main terminals: the Gate (G), the Drain (D), and the Source (S). They are popular because they work efficiently. MOSFETs are able to handle h
6 min read
Junction Field Effect Transistor or JFETIn electronics, the junction field effect transistor is a fundamental component, operating on the principle of electric field control. It is a three-terminal device, and it consists of the gate, drain, and source terminals. There are two types of JFETs: N-channel JFET and P-channel JFET. The N chann
11 min read
Communication Systems
Signal Processing