Arduino Potentiometer - Circuit and Code Example (2024)

by Øyvind Nydal Dahl

This Arduino Potentiometer circuit is a simple example that shows you how analog inputs work, and how you can use the Serial Monitor to learn about what is going on inside the chip.

In this quickstart guide, you’ll learn how to connect a potentiometer to an Arduino board and read out the voltage. This is a great practice circuit when you’re learning Arduino. The code is straightforward and the potentiometer connections are simple.

Parts Needed

  • Arduino Uno
  • Breadboard (and some breadboard wires)
  • Potentiometer (Any value above 200 Ω will work)

Arduino Potentiometer Circuit

To connect a potentiometer to an Arduino, connect the middle pin of the potentiometer to an analog input pin on the Arduino. Then connect the outer pins to 5V and GND.

Connecting On a Breadboard

Here’s how you can connect a potentiometer to an Arduino by using a breadboard and some cables:

Arduino Potentiometer- Circuit and Code Example (2)

Arduino Potentiometer Code

This Arduino code is an example of reading the voltage from the potentiometer (connected to analog pin A0) and then printing the value of the analog reading to the Serial Monitor.

An analog pin will give you a value between 0 and 1023, where 0 means 0V and 1023 means the maximum voltage possible (usually 5V).

As with all Arduino code, you have the two main functions setup() and loop():

  • Inside setup(), you need to configure the Serial port so that you can read out values.
  • Inside loop(), you need to read the analog input and print this value out on the Serial port.

Check out the complete code:

Arduino Potentiometer- Circuit and Code Example (3)

Get Our Basic Electronic Components Guide

Learn how the basic electronic components work so that circuit diagrams will start making sense to you.

void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600);}void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read in the Serial Montitor: Serial.println(sensorValue); delay(1); // delay in between reads for stability}

How the Code Works

Inside the setup() function there is only one line: Serial.begin(9600); This line sets up the serial port of the Arduino so that it’s possible to send data out from the Arduino and into your computer.

Inside the loop() function there are three lines:

  1. int sensorValue = analogRead(A0);: This line reads the analog voltage present at analog pin A0. It returns a value between 0 and 1023, representing the voltage level on the pin relative to the reference voltage (usually 5V for most Arduino boards). The value is stored in the variable sensorValue.
  2. Serial.println(sensorValue);: This line prints the value of sensorValue to the Serial Monitor so that you can read it.
  3. delay(100);: This line adds a small delay of 100 milliseconds between each reading and printing. This delay prevents the code from running too quickly. This way it’s easier to read the readings on the Serial Monitor.

The end result of this code is that it continuously reads the analog voltage at pin A0, prints the reading (a number between 0 and 1023) to the Serial Monitor, and repeats the process in a loop.

When you turn the potentiometer, the voltage out from the potentiometer will change, and you can observe the changing values in the Serial Monitor. This is a helpful way to visualize the data and debug when things aren’t working as expected.

As you continue learning about Arduino, you can use this as the basis to control other things with the potentiometer, such as in the Arduino Servo Motor guide.

Using The Serial Monitor

To use the Serial Monitor to check the results from the code above, follow these steps:

  1. Connect your Arduino board to your computer using a USB cable.
  2. Upload the provided code to your Arduino board using the Arduino IDE.
  3. Open the Serial Monitor by clicking the magnifying glass icon or using the keyboard shortcut Ctrl + Shift + M (Windows/Linux) or Cmd + Shift + M (Mac).
  4. Set the baud rate in the Serial Monitor to 9600 (or the same value as in the Serial.begin() function in the code).
  5. Observe the continuous stream of numbers in the Serial Monitor. These numbers represent the analog readings from pin A0.
  6. Adjust the potentiometer to see how the readings change in real time.
  7. Close the Serial Monitor when you’re done by clicking the “x” button in the top-right corner.
Arduino Potentiometer- Circuit and Code Example (4)

More Arduino Tutorials

  • What is Arduino?
  • Arduino Programming Basics
  • Arduino Blink LED Example
  • Arduino ButtonExample
  • Arduino PotentiometerExample
  • Arduino Libraries and How To Install Them
  • Arduino RGB LED Example
  • Arduino Speaker Tutorial
  • Arduino Buzzer Tutorial
  • Arduino Sound Sensor Guide
  • Arduino Light Sensor Example
  • Arduino PIR Sensor Circuit
  • Arduino Motor Guide: DC Motor
  • Arduino Servo Motor Guide
  • Arduino Rotary Encoder Tutorial
  • Arduino Thermistor Guide
  • Arduino Ultrasonic Distance Measurement
  • Arduino Hall Effect Sensor Guide
  • Arduino Remote Control
  • Arduino IR Transmitter Circuit and Code Example
  • Arduino IR Receiver Circuit
  • Arduino Oscilloscope (old version)
  • Updated Arduino Oscilloscope with 7 lines of code
  • Controlling a Laser with Arduino
  • Controlling a UWB Radar with Arduino
  • How to build a robot – Club de Arduino
  • From Arduino Prototype to Manufacturable Product
  • Arduino Shield Tutorial – Make Your Own Shield

Arduino Potentiometer- Circuit and Code Example (5)

Free Course: Blinking a Light

Join my free email course on how to build a circuit that blinks a light. Enter your details below and you'll receive the first lesson right away:

Arduino Potentiometer - Circuit and Code Example (2024)

FAQs

What is the function of a potentiometer in an LCD and Arduino connection? ›

A 'potentiometer' is typically used to implement a variable voltage divider which otherwise would require two resistors. In this case the potentiometer is used to provide a voltage at pin 3 that is used to adjust the contrast of the LCD.

What is the range of potentiometer in Arduino? ›

The potentiometer will output a value between 0 and 1023.

What is an example of potentiometer circuit? ›

You primarily use a rotating potentiometer to adjust the supply voltage to a particular portion of an electronic circuit. A radio transistor's volume controller is an example of a rotary potentiometer that regulates the power supply for the amplifier in audio equipment using its rotary knob.

How do I get an Arduino code example? ›

Built-in Examples are sketches included in the Arduino Software (IDE); to open them, click on the toolbar menu: File > Examples. These simple programs demonstrate all basic Arduino commands. They span from a Bare Minimum Sketch to Digital and Analog IO, to the use of Sensors and Displays.

How to use a digital potentiometer with Arduino? ›

Digital Potentiometer with Arduino
  1. Wire 5V to VDD (8) and PA (5)
  2. Connect the ground to PB (7) and VSS (4)
  3. Next, connect a resistor in the 100Ω range to PW (6), then to an LED.
  4. The LED should connect to ground to connect the circuit.
  5. Arduino pin 10 goes to CS (1) on the MCP41XXX.
  6. Pin 11 will connect to SI (3)
Feb 5, 2019

What is the color code for potentiometers? ›

Most poten- tiometer terminals are either numbered or color coded as follows: 1 Yellow Always the counterclockwise ele- ment limit. 2 Red Wiper (or collector). 3 Green Always the clockwise element limit.

How to use a potentiometer to control LEDs? ›

Spin the knob of the potentiometer, and the voltage at pin A0 will change. Then convert that voltage into a digital value (0-1024) with the AD converter in the control board. Through programming, we can use the converted digital value to control the brightness of the LED on the control board.

What is a potentiometer also known as in Arduino? ›

A potentiometer is also known as a variable resistor.

What are the pins of a potentiometer on Arduino? ›

All potentiometers have three pins. The outer pins are used for connecting power source (Vref and gnd). The middle pin (output) give us the variable of resistance value.

What is the maximum input voltage of a potentiometer? ›

The maximum voltage is limited by the Wattage rating. If the potentiometer is rated at 1 Watt, you can only apply a maximum of 100 volts.

What are 2 real world uses for a potentiometer in a circuit? ›

Potentiometers are commonly used to control electrical devices such as volume controls on audio equipment. It is also used in speed control of fans. Potentiometers operated by a mechanism can be used as position transducers, for example, in a joystick.

What is the formula for potentiometer circuit? ›

For a primary circuit of the potentiometer the potential gradient remains the same. Potential gradient is calculated as K = V/L, where V is the voltage across the potentiometer wire and the L is the length of the wire in the potentiometer. So the unit of potential gradient is volts/meter.

Is a potentiometer AC or DC? ›

Hint: The potentiometer is an electronic component. It is used to measure voltage. A difference between the ac potentiometer and dc potentiometer is that ac potentiometers measure the magnitude and also the phase of unknown voltage whereas dc potentiometer measures only the magnitude of the unknown voltage.

What is the code value of a potentiometer? ›

Potentiometer values are often marked with a readable string indicating the total resistance, such as "100k" for a 100 kΩ potentiometer. Sometimes a 3 digit coding system similar to SMD resistor coding is used. In this system the first digits indicate the value and the last digit indicates the multiplier.

How do you write power in Arduino code? ›

Example Code

Calculate the value of x raised to the power of y: z = pow(x, y);

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6209

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.