Created on: 20 January 2022
Read an analog input with Arduino Uno and Arduino MEGA 2560 boards. How to get or read the analog value on an Arduino analog input pin set by a potentiometer. Connect a potentiometer to an Arduino analog input pin in this part of the Arduino tutorial for beginners. The potentiometer sets a voltage between 0V to 5V on an Arduino analog pin. Thereafter a sketch reads the analog value on the pin and displays it in the Arduino IDE serial monitor window.
This tutorial uses two built-in example sketches. Firstly a sketch gets the raw analog value and displays it. Secondly a different sketch gets the raw analog value from the analog pin and converts it to the voltage found on the pin. Mathematics in the sketch calculates the voltage. The Arduino IDE serial monitor window displays the calculated voltage.
Part 9 of the Arduino Tutorial for Beginners
Before reading an analog input with Arduino, it is important to know what a potentiometer is. In addition it is important to know what an Arduino analog input is.
A potentiometer is a variable resistor. Turn the shaft or screw of the potentiometer to change its resistance. The potentiometer article on this website has more information on potentiometers for beginners. This article shows different potentiometer symbols used in circuit diagrams. In addition, it provides basic information on potentiometers.
Use a potentiometer in this part of the Arduino tutorial for beginners in order to read an analog input with Arduino. The potentiometer varies or changes the voltage on the Arduino analog input pin.
Arduino Uno boards have six analog input pins, labelled A0 to A5. At first glance A0 to A5 looks like five pins. Because pin numbering starts from 0, there are actually six pins. In other words, A1 to A5 are five pins. Hence A0 to A5 are six pins.
Arduino MEGA 2560 boards have sixteen analog input pins, labelled A0 to A15. As explained above, pin numbering starts from 0. Therefore, A0 to A15 are sixteen pins and not fifteen.
Arduino analog input pins read voltages between 0V to 5V by default, with a certain resolution. Ten bit binary values represent the voltages read. On the other hand, digital input pins only read two logic levels of 0 or 1.
The easiest way to connect a potentiometer to an Arduino is to use Dupont wires. Basically, the Dupont wires connect to the potentiometer pins. The other end of the Dupont wires connect directly to the Arduino pins or connectors, as explained next.
Use Dupont wires with a pin on one end and a socket on the other to connect a potentiometer to an Arduino board. To clarify, these wires are male-to-female Dupont wire. The following image shows three 10cm male-to-female Dupont wires at the top. Below the wires is a potentiometer. Specifically, this is a 5k potentiometer.
B5K is the marking at the top of the above potentiometer. Face the shaft with the pins pointing down to see the marking. This means that it is a 5kΩ, or 5,000 ohm linear potentiometer. The value of the potentiometer for this tutorial can be anywhere from around 5k to 20k. Arduino examples usually use a 10k potentiometer. The first page of this Arduino tutorial for beginners lists the above wires and potentiometer.
Plug the socket ends of the three male-to-female Dupont wires onto each of the pins of the potentiometer. The following image shows one wire connected per pin.
Connect each male pin to the Arduino as shown next for an Uno and then MEGA 2560. Firstly connect a wire from one of the outer pins of the potentiometer to 5V on the Arduino. Secondly, connect the opposite outside pin to GND of the Arduino. Finally, connect the middle pin of the potentiometer to the A0 analog Arduino pin.
The following image shows a potentiometer connected to an Arduino Uno. The Arduino Uno 5V pin connects to one potentiometer outer pin via a red wire. Thereafter, the other outer potentiometer pin connects to an Arduino GND pin via a black wire. Finally, the middle potentiometer pin, or wiper, connects to analog input pin A0 via a blue wire. For an alternate circuit, see the Analog Read Serial example on the Arduino website. By comparison, the Analog Read Serial example uses a trim pot type potentiometer and a breadboard.
Use red wire for positive voltage connections if available. Use black wire for GND or 0V connections. Both of these colors are used by convention and preferred when available. Use any other colored wires for positive and GND voltages when red and black are not available.
An equivalent circuit to the above connects a potentiometer to an Arduino MEGA 2560 as shown below. Once again, connect the outer pins to 5V and GND. Afterwards, connect the center pin to A0.
Below is the circuit diagram of the above two circuits. As can be seen in the circuit diagram, the center wiper of the potentiometer is connected to Arduino analog input pin A0. Outer pins of the potentiometer connect to 5V and GND. Rotate the shaft of the potentiometer to set the voltage on the A0 pin. Move the wiper of the potentiometer closer to the pin connected to 5V, and the voltage on A0 increases. Rotate the wiper towards GND, and the A0 voltage decreases. When the wiper is all the way to the 5V side, 5V appears on A0. Likewise, when the wiper is all the way to GND, 0V appears on A0.
Use the AnalogReadSerial example sketch to get the raw analog value currently on A0. In the Arduino IDE, select File → Examples → 01.Basics → AnalogReadSerial on the top menu bar. As a result, the AnalogReadSerial example sketch opens in a new Arduino IDE window. Upload the sketch to the target Arduino board.
When the AnalogReadSerial sketch is loaded to the target Arduino, the TX LED of the board switches on. This is because the Arduino is transmitting the raw analog value out of the USB port. Open the serial monitor window in the Arduino IDE to see the analog value. Rotate the shaft of the potentiometer and the value in the serial monitor window changes. The serial monitor window displays values between 0 and 1023.
The serial monitor window displays 0 when 0V or GND is present on the A0 pin. On the other hand, the serial monitor window displays 1023 when 5V is present on the A0 pin. Change the voltage on the A0 pin by rotating the potentiometer shaft. As a result, the serial monitor window displays raw analog values between 0 and 1023.
Inside the main microcontroller chip on an Arduino board is an ADC. ADC stands for Analog to Digital Converter. This device converts the analog voltage on an analog input pin to a 10-bit number between 0 and 1023.
The AnalogReadSerial sketch works in a very similar way to the DigitalReadSerial sketch from the previous part of this tutorial. Below is the AnalogReadSerial sketch code for reference.
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
Serial.begin() sets up the serial/USB port to operate at 9600 baud in the setup() part of the sketch. After that, the following happens in the loop() function. Firstly, analogRead() is called, and its return value assigned to the integer variable sensorValue. A0 is passed to analogRead() which gets the raw analog value from analog input pin A0. Secondly, the analog value in the sensorValue variable is sent out of the USB port when Serial.println() is called. Finally, delay() is called to add a 1ms delay to the main loop.
Refer to the Arduino AnalogReadSerial tutorial page for an alternate explanation of this example.
The ReadAnalogVoltage built-in example sketch converts raw analog input to the voltage that it represents. Select File → Examples → 01.Basics → ReadAnalogVoltage from the top menu bar of the Arduino IDE. As a result, the ReadAnalog example sketch opens in a new Arduino IDE window.
Upload the ReadAnalogVoltage sketch to the target Arduino board. Open the Arduino IDE window. As a result, the Arduino IDE window displays the voltage from the Arduino A0 pin.
While the previous sketch reads a raw analog input with Arduino, this sketch converts it to a voltage before sending it out the USB port. Both sketches read an analog input with Arduino and are almost identical. The ReadAnalogVoltage sketch code follows for reference.
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
As can be seen in the above code, the setup() function calls Serial.begin() as the previous sketch does. After this, analogRead() is called in the same way. This reads the raw analog input value found on analog input pin A0. This value is placed in the sensorValue integer variable as done before. Instead of sending sensorValue out of the USB port, some mathematics is done first. This converts the raw analog value to a voltage that represents the voltage currently on the A0 pin. This value is stored in the floating point variable called voltage. Finally, the calculated voltage is sent out of the serial/USB port by Serial.println().
See the Arduino ReadAnalogVoltage tutorial page for an alternate explanation of this example sketch.
For more information on the analog voltage calculation, see the Ultimate Arduino Uno Hardware Manual for Arduino Uno boards. Find the same information for Arduino MEGA 2560 boards in the Ultimate Arduino MEGA 2560 Hardware Manual.
The keyword float declares a floating point variable. This type of variable holds values that contain fractional parts. As a result, voltages between 0V and 5V are displayed as values that have a decimal point.
Measure the voltage on the A0 pin if you have a multimeter. Unplug the middle pin wire of the potentiometer from the Arduino and plug it into a breadboard. Connect a jumper from the breadboard to the Arduino A0 analog input pin that also connects to the potentiometer middle wire. This makes it easier to connect the multimeter. Use a jumper wire link to connect the black COM lead of the multimeter to one of the other Arduino GND pins. Use crocodile clip attachments on the multimeter leads to connect to the GND and A0 wires. Select the multimeter DC voltage scale. Finally, connect the red multimeter lead to the A0 pin via the breadboard and a jumper wire. The image below shows the above described connections.
Compare the multimeter voltage to the voltage displayed in the serial monitor window. There will be slight differences between the multimeter reading and the value in the serial monitor window. This has to do with component tolerances, voltage references and the accuracy of the USB 5V from the host computer.