Arduino UNO R4 - Potentiometer

This tutorial instructs you how to use Arduino Uno R4 with a potentiometer. In detail, we will learn:

Arduino UNO R4 potentiometer

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Potentiometer
1×Potentiometer Kit
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor 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 rotary potentiometer, also known as a rotary angle sensor, is used to manually change settings such as stereo volume, lamp brightness, or oscilloscope zoom level.

Potentiometer Pinout

Pinout

A potentiometer typically has three pins.

  • GND pin: connect to GND (0V)
  • VCC pin: connect to VCC (5V or 3.3V)
  • Output pin: sends voltage to Arduino UNO R4's input pin.
Potentiometer Pinout

※ NOTE THAT:

The GND pin and the VCC pin can be swapped.

How It Works

The potentiometer's shaft can turn from 0° (closest to the GND) up to a maximum angle (closest to the VCC pin), named ANGLE_MAX.

The voltage at the output pin varies from the voltage at GND to the voltage at VCC. The output voltage changes directly with the angle the shaft is turned.

  • When the angle is 0 degrees, the voltage at the output pin is 0 volts.
  • When the angle equals ANGLE_MAX, the voltage at the output pin matches the VCC's voltage.
  • If the angle is in between 0° and ANGLE_MAX, output_voltage = angle × VCC / ANGLE_MAX

※ NOTE THAT:

The value of ANGLE_MAX varies depending on the manufacturer. Usually, we don't pay much attention to the value of ANGLE_MAX unless we need to compute the angle of rotation (refer to the use cases section).

How Potentiometer Works

Arduino UNO R4 - Rotary Potentiometer

The pins A0 to A5 on the Arduino UNO R4 can be set up as analog inputs. These pins change the voltage, which ranges from 0 volts to VCC, into whole numbers between 0 and 1023. These numbers are called ADC values or analog values.

By connecting the output pin of a potentiometer to an analog input pin on the Arduino UNO R4, we can program the Arduino to read the ADC value and convert it into a useful number.

The value received by the Arduino UNO R4 is not an angle or voltage; it is an integer that ranges from 0 to 1023.

We take the number from the analog input pin and convert it into a different number. Now, let's see how it's used.

Use Cases

  • Converting the ADC value to the angle.
  • Converting the ADC value to the voltage
  • Converting the ADC value to a controllable value (like volume of stereo, brightness, or motor speed). This is the most frequently used scenario.

Rescale Range

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

Wiring Diagram

The wiring diagram between Arduino UNO R4 Potentiometer

This image is created using Fritzing. Click to enlarge image

How To Program For Potentiometer

  • Use the function analogRead() to read the value from a pin that is connected to the potentiometer's output pin.
adc_value = analogRead(A0);
  • Convert the ADC value to the angle of the potentiometer using the map() function.
angle = map(adc_value, 0, 1023, 0, ANGLE_MAX);
  • Convert the ADC value to the voltage:
voltage = map(adc_value, 0, 1023, 0, VCC);
  • Convert the ADC value to a manageable level (for example, the volume of the stereo, the brightness, or the speed of the DC motor).
value = map(adc_value, 0, 1023, VALUE_MIN, VALUE_MAX);
  • For example, adjusting the brightness of an LED. The brightness of an LED can be controlled using a PWM value from 0 (always OFF) to 255 (always ON). Therefore, we can map the ADC value to the LED brightness (from OFF to the brightest) as follows:
brightness = map(adc_value, 0, 1023, 0, 255);

※ NOTE THAT:

The map() function is used to change an analog value to either an int or long type value. If you need to work with a float type value, you should use the floatMap() function 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 UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-potentiometer */ float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { // Map a float value from one range to another. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } void setup() { // Begin serial communication with a baud rate of 9600: Serial.begin(9600); } void loop() { // Retrieve analog value from pin A0: int adc_value = analogRead(A0); // Convert the analog value to a voltage (0-5V range): float voltage = floatMap(adc_value, 0, 1023, 0, 5); // Output the analog value and corresponding voltage to the serial monitor: Serial.print("Analog: "); Serial.print(adc_value); Serial.print(", Voltage: "); Serial.println(voltage); // Wait for a second before repeating the loop: delay(1000); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the potentiometer to Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it with Arduino IDE
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4
Arduino IDE Upload Code
  • Open the Serial Monitor
  • Turn the potentiometer
  • Check the Serial Monitor for 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

※ 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!