Arduino Mega - Potentiometer

This guide shows you how to use an Arduino Mega with a potentiometer. Here is what we will learn:

Arduino Mega potentiometer

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Potentiometer
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Alternatively, Potentiometer Kit
1×Alternatively, Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Potentiometer

A knob called a rotary potentiometer, or a rotary angle sensor, lets you change settings by turning it, such as stereo volume, lamp brightness, or oscilloscope zoom.

Potentiometer Pinout

Pinout

A potentiometer usually has three pins.

  • GND pin: connect to ground (0 volts)
  • VCC pin: connect to 5V or 3.3V
  • Output pin: sends voltage to the Arduino Mega's input pin
Potentiometer Pinout

※ NOTE THAT:

You can swap the ground pin and the power pin.

How It Works

The potentiometer knob can turn from 0° (closest to GND) to the maximum angle (closest to the VCC pin), called ANGLE_MAX.

The output pin voltage can vary from ground to the supply voltage. The output voltage changes directly as you turn the shaft.

  • If the angle is 0 degrees, the output voltage is 0 volts.
  • If the angle equals ANGLE_MAX, the output voltage is VCC.
  • If the angle is between 0 and ANGLE_MAX, output_voltage = angle times VCC divided by ANGLE_MAX.

※ NOTE THAT:

ANGLE_MAX value varies by manufacturer. Usually we don’t worry about ANGLE_MAX unless we need to calculate the rotation angle (see the use cases section).

How Potentiometer Works

Arduino Mega - Rotary Potentiometer

The A0 to A5 pins on the Arduino Mega can be used as analog inputs. They read a voltage from 0 volts up to the supply voltage (VCC) and turn it into numbers from 0 to 1023. These numbers are called ADC values (analog-to-digital converter values).

If you connect the potentiometer's output pin to an analog input pin on the Arduino Mega, you can program the Arduino to read the analog value and turn it into a useful number.

The number the Arduino Mega gets isn’t an angle or a voltage. It’s a whole number from 0 to 1023.

We read the value from the analog input pin and change it to another value. Now, let's see how we use it.

Use Cases

  • Convert the ADC value to an angle.
  • Convert the ADC value to a voltage.
  • Convert the ADC value to a control value (for example, volume, brightness, or motor speed). This is the most common use.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0VVCC
ADC valueread by Arduino Mega 01023
Other valueconverted by Arduino Mega VALUE_MINVALUE_MAX

Wiring Diagram

The wiring diagram between Arduino Mega Potentiometer

This image is created using Fritzing. Click to enlarge image

If using the potentiometer module:

The wiring diagram between Arduino Mega Potentiometer Module

This image is created using Fritzing. Click to enlarge image

How To Program For Potentiometer

  • Use the analogRead() function to read the value from the pin connected to the potentiometer’s output.
adc_value = analogRead(A0);
  • Turn the ADC reading into the potentiometer angle using the map() function.
angle = map(adc_value, 0, 1023, 0, ANGLE_MAX);
  • Turn the ADC value into a voltage:
voltage = map(adc_value, 0, 1023, 0, VCC);
  • Turn the ADC value into a simple level you can use (for example, to control the stereo volume, brightness, or the speed of a DC motor).
value = map(adc_value, 0, 1023, VALUE_MIN, VALUE_MAX);
  • Example: adjusting how bright an LED is. You control brightness with a PWM value from 0 (off) to 255 (fully on). So we can match the ADC value to the LED brightness (from off to the brightest) like this:
brightness = map(adc_value, 0, 1023, 0, 255);

※ NOTE THAT:

map() converts an analog value to an int or long. If you need a float value, use floatMap() instead.

The floatMap function:

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-potentiometer */ float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { // Remap a floating-point value from the input range to the output range. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } void setup() { // Initialize serial communication at 9600 baud. Serial.begin(9600); } void loop() { // Read the analog value from pin A0. int adc_value = analogRead(A0); // Convert the ADC value to a voltage in the 0-5 V range. float voltage = floatMap(adc_value, 0, 1023, 0, 5); // Print the measured value and its calculated voltage to the serial monitor. Serial.print("Analog: "); Serial.print(adc_value); Serial.print(", Voltage: "); Serial.println(voltage); // Pause for one second before the next loop iteration. delay(1000); }

Detailed Instructions

Do these steps one by one.

  • Connect the potentiometer to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Select the correct Arduino Mega board (for example, Arduino Mega) and the port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
Arduino IDE Upload Code
  • Open the Serial Monitor
  • Turn the pot
  • Look at the Serial Monitor to see the result
COM6
Send
Analog: 0, Voltage: 0.00 Analog: 0, Voltage: 0.00 Analog: 126, Voltage: 0.62 Analog: 281, Voltage: 1.37 Analog: 517, Voltage: 2.53 Analog: 754, Voltage: 3.69 Analog: 906, Voltage: 4.43 Analog: 1023, Voltage: 5.00 Analog: 1023, Voltage: 5.00
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!