Lesson 10: Reading Analog values with Arduino

Introduction:

Hello and welcome to circuit academy, in this article, we’ll explore how to read analog values using an Arduino, understand what’s happening under the hood, and even build a basic voltmeter. But before all of this lets understand the difference between analog and digital values.

Digital Signals Vs Analog Signals:

Up until now, we’ve dealt with digital signals having binary states, HIGH or LOW, like a basic light switch having only two states ON and OFF. So while digital signals can be considered as a basic light switch having only two values, analog signals offers a continuous range of values-like a dimmer switch. For example, temperature is an analog signal. It’s not limited to just two states like extremely hot or extremely cold—instead, it has a continuous range of values between these two extremes. Similarly, audio signals, such as the sound of our voice, is also analog in nature. When we speak, we don’t produce just two tones; instead, our speech contains a wide spectrum of frequencies and variations in volume, making it a perfect example of an analog signal.

How Arduino Reads Analog Signals (Meet the ADC):

Our Arduino is equipped to handle analog voltage values ranging in between 0 to 5V DC, through its 6 analog input pins, denoted A0 to A5. The hardware component responsible for reading these analog voltage values on the ATMEGA 328 chip is called the ADC (Analog-to-Digital Converter). This crucial component converts analog voltage values into digital ones, as our Arduino operates solely with digital values.

The ADC on the UNO has a resolution of 10 bits. Essentially what it means is that it uses a 10-bit binary number to represent voltage values ranging from 0V to 5V which is the supply voltage of the ATMEGA chip. So the range spans from the smallest 10-bit binary number(0000000000), equal to 0 in decimal, representing 0V. To the largest 10 bit number(1111111111), equal to 1023 in decimal, representing 5V.

In practical terms, this means that the 10-bit ADC maps a voltage signal between 0 and 5V, present on any of the six analog inputs, into integer values between 0 and 1023. Consequently, a voltage of 0V corresponds to a value of 0, while a voltage of 5V corresponds to a value of 1023. For any voltage value in between these extremes, we can calculate the corresponding ADC value using the formula shown:

Where:
🔸Vin = input voltage (0 to 5V)
🔸Vref​ = reference voltage (typically 5V on most Arduino boards)
🔸1023 = maximum value for a 10-bit ADC

So suppose if we have a voltage signal of 3.3V on one of our analog inputs than the corresponding value that our ADC will generate is calculated as shown below,

The analogRead() Function:

Now that we’ve explored the hardware side of reading analog signals—namely the ADC (Analog-to-Digital Converter)—let’s shift our attention to the software side. In the Arduino environment, the function used to read values from the ADC is called analogRead(). This function returns ADC values which are integer values corresponding to analog voltage values at a specified pin. This function takes a single parameter that is the analog pin number (e.g., A0, A1, etc.). and its syntax is shown below,

analogRead(Analog_Pin);

Example 1: Building A Simple Voltmeter

Now let’s see the analogRead() function in action with a short Arduino sketch that turns our Arduino UNO into a mini voltmeter, capable of reading voltage values up to 5V DC and displaying the output on the serial monitor. The code for this example is shown below,

int ADC_value;
float voltage;

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
}

void loop() {
  ADC_value = analogRead(A0);                   // Read raw ADC value
  voltage = (ADC_value * 5.0) / 1023.0;          // Convert to voltage
  Serial.print("Voltage: ");
  Serial.print(voltage);
  Serial.println(" V");
  delay(500);
}

We first have an integer variable named ADC_values and a float variable named voltage. In the setup, we designate pin A0 as an input pin and set the baud rate for serial communication to 9600 .

In the loop function, we’ll read voltage values from analog pin A0 using the analogRead() function and save its output in the ADC_value variable. Then, we’ll convert this ADC value into corresponding analog values using the same formula we discussed above,

but here we are interested in voltage so we will rearrange this formula to read voltage as shown below,

The output of this formula is stored in the voltage variable of the float data type. We then print this variable on the serial monitor, appending the suffix ‘V’ to denote it as a voltage value.

After uploading the code to our Arduino and opening the serial monitor, we’ll initially see 0V when nothing is connected. Connecting a 5V source to pin A0 will result in a reading close to 5V. Similarly, connecting a 3.7V lithium cell will yield a voltage close to that, and the same goes for a 1.5V AAA cell.

As you follow along with this lesson, it’s important to keep two key considerations in mind. First, ensure that you connect the positive end of each device you want to measure the voltage of to analog pin A0, and connect the negative end to the ground terminal of the Arduino. Additionally, make sure not to measure any voltage higher than 5V to avoid damaging your Arduino.

Leave a Comment

Your email address will not be published. Required fields are marked *