banner
Home / News / Potentiometers vs. Rotary Encoders and How to Use Them With Arduino
News

Potentiometers vs. Rotary Encoders and How to Use Them With Arduino

May 22, 2023May 22, 2023

Potentiometers and rotary encoders looks almost identical at first glance, but they work in different ways. Discover how to use both with an Arduino.

Among electronic user control components, rotary knobs stand out as some of the most satisfying to use. They can complement touchscreens and other input devices, as well as working well with buttons and switches. But how can you add a knob to your own DIY Arduino projects?

You have two main options: a potentiometer or a rotary encoder. These components may look similar, but the methods for using them with a device like an Arduino microcontroller board are very different. Let's see how they compare against each other.

Most of the potentiometers and rotary encoders that DIY enthusiasts will encounter come in a similar form factor. They have a cuboid or cylindrical base with connector legs attached, and a round shaft that twists and has cut-outs for a cap to sit on.

Some potentiometers do look different, such as the ones that come in the form of long slides, like those found on music mixing decks. When it comes to the rotary kind, however, at first glance they look almost identical to rotary encoders, so you'd be forgiven for thinking they are the same.

A potentiometer is in essence a variable resistor. As the shaft is turned, the resistance inside the potentiometer changes, allowing a user to alter the properties of a circuit without having to rebuild it. Potentiometers can be both analog and digital, but digital potentiometers mimic analog ones and this makes them very similar to use.

Potentiometers always have a defined start and end point where the shaft can no longer be turned. Some potentiometers have a bumpy feel when turned, but many are also smooth, like those found on old stereos.

Despite being analog, potentiometers work well with microcontrollers. You can easily set up a potentiometer with a Raspberry Pi Pico or Arduino.

Rotary encoders determine the position of their shaft using a sensor to provide an analog or digital signal to the device they are connected to. This tells the device which position the encoder is in. Alongside the rotating shaft, rotary encoders usually also have a built-in button that is actuated by pushing the shaft downwards.

Unlike potentiometers, rotary encoders can turn without stopping, and they almost always have tactile bumps for each of the shaft's positions. Many modern cars use rotary encoders to control their entertainment systems.

Thanks to their simple design, using a potentiometer with an Arduino is simple. Your potentiometer has three connectors: ground, output, and vref. The ground and vref pins connect to the GND and 5V connectors on your Arduino, respectively, while the potentiometer's output pin connects to one of the analog inputs on your board.

Your Arduino potentiometer code starts with the basic setup() and loop() template that you will see when you create a new file in the Arduino IDE. First, add a const int variable at the start of the code to register the pot's analog pin connection—in this case, A0.

Following this, the setup() function is simple: you just need to declare your potentiometer's pin as an input. You can also start a serial connection if you want to send data to your PC for diagnostics.

Next, it's time to set up the loop() function. Start by creating an int variable using the analogRead() function to store the position of your potentiometer. Following this, you can use the map() function to reduce the size of the value you are dealing with—in this example to match PWM specifications, for instance to control the brightness of an LED. Add a short delay to ensure stability.

Now that you have the position of your potentiometer, you can use it with other parts of the code. For example, an if statement would work well to trigger code when the potentiometer is in a specific position.

Rotary encoders require more intricate code than potentiometers, but they are still fairly easy to work with. Your rotary encoder has five pins: ground, VCC, a button pin (SW), output A (CLK), and output B (DT). The ground and VCC pins connect to the ground and 5V connectors on your Arduino, respectively, while the SW, CLK, and BT pins connect to individual digital connectors on the Arduino.

To make our code simpler and easier to work with, we will use the SimpleRotary Arduino library created by MPrograms on GitHub. Make sure that you have this library installed before you start working on your code.

Much like your potentiometer code, you can start your rotary encoder script with the basic Arduino setup() and loop() function template. Start by declaring the SimpleRotary library and assigning your encoder pins in this order; CLK, DT, and SW.

You don't need to add anything to your setup() function unless you want to use the serial monitor to diagnose your rotary encoder.

The loop() function is a different story. Determining the rotation of the encoder shaft starts with a rotary.rotate() function call that is assigned to an int variable. If the result is 1, the encoder is turning clockwise. If the result is 2, the encoder is turning counter-clockwise. The result will always be 0 if the encoder hasn't turned since the last check.

You can use if statements to trigger other code depending on the direction of the encoder's rotation.

You also need to add some code for your encoder's button to the loop() function. This process is very similar, except you will use the rotary.push() function, rather than rotary.rotate().

This script is quite simple, and you can do a lot to make it your own. It's well worth checking out the SimpleRotary project documentation to ensure that you are using all of its key features. Once put together, your encoder code should look like this.

As you can see, rotary encoders and potentiometers work quite differently. Both of these components give you new ways to control your electronics projects, but which should you choose?

Potentiometers are affordable and easy to use, but only allow a limited input range. This makes them great when you want to control the brightness of an LED, or raise and lower the power going to specific components, and other similar tasks.

Rotary encoders provide a lot more scope than potentiometers. The inclusion of a push button means that they are great for menu control systems, as seen in many modern cars. This type of component has become very popular in the mechanical keyboard building space. You can even build a small macropad with an encoder built-in.

With all of this information under your belt, you should be ready to get started on an electronics project with a potentiometer or rotary encoder. These components can give you loads of control over the circuits you build, but you need to make sure that you choose the correct option for your project.

Samuel is a UK-based technology writer with a passion for all things DIY. Having started businesses in the fields of web development and 3D printing, along with working as a writer for many years, Samuel offers a unique insight into the world of technology. Focusing mainly on DIY tech projects, he loves nothing more than sharing fun and exciting ideas that you can try at home. Outside of work, Samuel can usually be found cycling, playing PC video games, or desperately attempting to communicate with his pet crab.

MAKEUSEOF VIDEO OF THE DAY SCROLL TO CONTINUE WITH CONTENT setup() loop() const int setup() loop() int analogRead() map() if setup() loop() setup() loop() rotary.rotate() int if loop() rotary.push() rotary.rotate()