Arduino Analog-to-Digital Converter: How Arduino Interacts With Real-World Voltage Variables

In this article, we will look at how the Arduino analog-to-digital converter generates digital signals from analog inputs in your project. Computers can only work with binary signals. In this system, a high voltage is …

An Arduino board with six analog pins A0-A6

Home » Arduino Analog-to-Digital Converter: How Arduino Interacts With Real-World Voltage Variables

In this article, we will look at how the Arduino analog-to-digital converter generates digital signals from analog inputs in your project.

Computers can only work with binary signals. In this system, a high voltage is a binary high, and a low voltage is a binary low.

It is a straightforward system, albeit with one problem; the real world does not operate that way.

Signal inputs usually vary across a broad range, not just low and high.

So Arduino features an ADC to handle such inputs, and here’s all you need to know about this feature.

Table of Contents

What Is an Analog to Digital Converter?

An analog-to-digital converter is a component used to convert analog voltage signals to digital values.

The conversion enables you to use analog data in the computations in your code to get meaningful data.

Arduino boards usually have several analog pins that vary depending on the microcontroller.

For instance, the Arduino UNO has six analog pins, while the Arduino Mega has 16.

The pins usually have the letter A before the number, such as A0 and A5, for easy identification.

The Arduino ADC is usually a 10-bit type, meaning it can handle 1024 (2^10) discrete values to represent the analog input voltage.

Some microcontrollers have higher bit ADCs (such as 16-bit), enabling them to convert analog values more precisely into digital values.

Those with lower bit values are less accurate.

Using the example above, the 16-bit ADC has 65536 digital levels (2^16), while 8-bit ADCs only have 256 discrete levels.

Arduino Mega and UNO boards (Note the 16 analog pins on the Mega)

How An Analog to Digital Converter Works

ADCs come in several types, each with complex electronics that create different techniques for handling the analog-to-digital conversion.

But most of them have an internal capacitor and resistor.

With this setup, the analog voltage charges the internal capacitor.

After that, the microcontroller measures the clock cycles taken to discharge this capacitor across a resistor.

This number of clock cycles forms the digital output.

An analog sine wave with a corresponding digital signal after conversion

An analog sine wave with a corresponding digital signal after conversion

Conversion Equation Between the Analog and Digital Values

The digital output produced by the ADC is a ratiometric value of the analog input signal.

As stated earlier, the Arduino ADC is a 10-bit type that generates 1024 discrete values (0-1023).

And the board’s analog pins can only handle 0-5V of analog voltage.

So the ADC assumes 5V is 1023, and anything between 0V and 5V is a value between 0 and 1023. The ratio is as follows.

ADC ResolutionSystem Voltage=ADC ReadingAnalog Voltage Reading

So to get the ADC, we use:

ADC Reading=ADC Resolution x Analog Voltage ReadingSystem  Voltage

For instance, if the analog voltage is 2V, the ADC reading will be:

ADC Reading=1023 x 25=409

So you can recalculate the analog sensor voltage by multiplying the ADC reading in the code by 5/1023.

This multiplication reverses the conversion, giving you data for mapping functions or displaying the actual voltage.

Remember, if your ADC is a 3.3V type, replace the system voltage in the ratio (1023/3.3).

Similarly, you should match the resolution to the ADC bits.

For instance, if you have a 16-bit ADC, replace 1023 with 65536.

Arduino Analog to Digital Conversion Example

We’ll need the following components for this example.

  • Arduino UNO board
  • Potentiometer, voltage divider, light sensor, or any device capable of generating an analog voltage
  • Jumper wires
  • Breadboard

Connect the Arduino UNO and potentiometer as shown below.

Arduino UNOPotentiometer
5VVoltage Input
GNDGND
A0Signal Output (Middle Pin)

Circuit Diagram

As you turn the potentiometer knob, you alter the resistance between the supply and ground.

This process sends a varying voltage from the signal pin to the A0 pin on the Arduino UNO board. 

When the resistance decreases, the voltage output nears 5V. And as it increases, the voltage goes closer to zero.

The ADC maps these values to numbers between 0 and 1023 in real-time as you turn the knob.

You only have to use the analog read function to activate the analog-to-digital conversion, then store the discrete value as an integer. Here’s an example.

int discrete_sensor_value = analogRead(A0);

If you want to get the actual voltage reading from the potentiometer, multiply this discrete value by 5/1023 to reverse the conversion.

The resulting value will not be an integer, so store it inside a float variable.

float voltage = (discrete_sensor_value * 5) / 1023;

Next, print the voltage on the screen using this line.

Serial.println(voltage);

Here’s what the complete program should look like.

Code

We always begin with “Serial.begin(9600);” in the setup() function to initiate serial communications.

This line ensures the board and the serial monitor in your computer communicate at 9600 bits of data each second.

Once you get the analog voltage in your code, you can use the map function to create a meter for measuring things like wind speed (anemometer project), car speed, etc.

An Arduino project with a potentiometer and push button

However, you don’t have to convert the discrete values to analog signals in digital signal processing or music recording projects.

But these real-world applications require high-resolution ADCs.

For instance, most music recording studios use 24-bit ADCs.

These devices ensure the digital signal is as close to the analog input signal as possible.

So the studios maintain the audio quality when encoding the data into compact discs.

Is It Divide By 1023 or 1024?

When you go through various articles online, you might get confused about whether you should divide by 1023 or 1024 to convert the data from bits to voltage.

According to the ATMega328P datasheet section 23.7, it should be 1024. So in a 5V board, a single bit has a 4.88mV voltage step.

But the maximum ADC bit value is 1023, meaning the maximum ADC value the system can report is 4.9951V.

1023 x 51024=4.9951

This result matches the details in the microcontroller’s datasheet, which states the following.

  • 0x000 represents the ground (analog)
  • 0x3FF represents the selected reference or system voltage less one LSB

However, most people divide by 1023 to give a more rounded figure of 5V. It is wrong, but the offset value is negligible.

Plus, the hardware rounds up the result, so you might as well use 1023.

But if you want maximum accuracy, divide by 1024.

You can check this forum to get different views from other Arduino users.

FAQs

What if You Connect an Analog Signal Sensor to a Digital Pin?

You won’t damage any hardware, but the analog read function won’t work on digital pins.

The code will compile, but you will get a value that does not make sense with this line.

int sensorValue = analogRead(7);

What if You Connect a Digital Signal Sensor to an Analog Pin?

Again, you won’t damage any hardware, but the result given by the ADC will make some sense.

Let’s say you have a switch.

In the ON state, the analog read function will give values close to 1023.

These values will represent a binary one or 5V, which is true.

In the OFF state, the function will give you values close to zero, representing a binary zero or 0V. Still true.

Wrap Up

In conclusion, the Arduino ADC is a critical feature in an Arduino board; It enables you to build projects that can compute data from analog input channels.

We hope this article has been insightful and will guide you when using this feature.

Contact us if you need further guidance, and comment below to let us know what you think.

Categories How