Using a rotary encoder on an Arduino

How it works, and how to connect and use it on an Arduino

Melanie Chow
4 min readJun 18, 2019

For class I was given two mystery sensors, and was given the vague instructions to figure them out and create a demo. One of the sensors was a rotary encoder, and I thought I would share the demo. Our other sensor was a joystick, and the tutorial can be found here.

Rotary Encoder Sensor. Image Credits to Addicore

The following article is structured like so:

  1. Overview: Input & Output pins of the rotary encoder
  2. Understanding a Rotary Encoder: How a rotary encoder works
  3. Connecting it to an Arduino: Step-by-step wiring
  4. Demo + How to Code: Creating the demo

(Disclaimer: These tutorials were made by my wonderful team and I. The following has been edited slightly for sharing.)

Overview

A rotary encoder is a sensor used for determining the angular position of a shaft, similar to a potentiometer.

Pins and what they mean:

  • CLK: Output A (Digital)
  • DT: Output B (Digital)
  • SW: button press (Digital)
  • +: VCC — Voltage supply
  • GND: Ground

Understanding a Rotary Encoder

A rotary encoder can essentially be used for the same purpose as the potentiometer. However, a potentiometer typically has a default point past which the shaft cannot rotate, while a rotary encoder can rotate in one direction for as long as we want. To reset the position reading, we can push the shaft down to press the reset button. The rotary encoder we have produces digital signals.

Image credits to RecentSilentHyracotherium on Gfycat

The rotary encoder determines the angular position of a rotating shaft through a series of square wave pulses. It essentially has evenly spaced contact zones connected to a common pin, as well as two contact pins called A and B that are 90 degrees out of phase. When the rotary encoder is manually rotated, pins A and B make contact with the common pin, and generate the pulse. By counting the amount of pulses (or square waves) of either of these outputs, we can determine the rotary position. To determine direction and check if the pin is rotating clockwise or counterclockwise, we do the following:

If the rotary encoder is moving clockwise, output A is ahead of output B. As a result, at the same points in time, A and B will be on opposite parts of the square wave function (when A is being activated, B is inactive, and vice versa).

clockwise square wave movement

If the rotary encoder is moving counterclockwise, Output B is ahead of output A. As a result, at the same points in time, because it is moving 90 degrees ahead, A and B will both be activated and deactivated at the same time, albeit B will be ahead by those 90 degrees.

counter clockwise square wave movement

Connecting it to an Arduino

Here are what the 5 pins for the rotary encoder mean:

  • CLK: Output A (digital)
  • DT: Output B (digital)
  • SW: button press (digital)
  • +: VCC — Voltage supply
  • GND: Ground

CLK, DT, and SW should be connected to digital pins on the Arduino, + should be connected to the 5V and GND, like always, to ground.

Step by Step Instructions

1. CLK: Place one end of a wire at the CLK pin on the rotary encoder to any digital pin on the Arduino (Orange wire — )

2. DT: Place one end of a wire at the DT source pin on the rotary encoder to any digital pin on the Arduino (Yellow wire — )

3. SW: Place one end of a wire at the SW pin on the rotary encoder to any digital pin on the Arduino (Sky Blue wire — )

4. +: Place one end of a wire at the + pin on the rotary encoder to the +5V pin on the Arduino (Red wire — )

5. GND: Place one end of a wire at the GND pin on the rotary encoder to the GND pin on the Arduino. (Black wire — )

Vertical view of Arduino Setup

Demo + How to Code

This code alters pitch based on which direction the encoder is turned. When it is turned counter-clockwise we decrease the pitch, and when it is turned clockwise, we increase it.

Here is a demo video for reference:

What you will need:

  • Rotary Encoder sensor
  • Arduino
  • Piezo
  • Wires
int buzzer_pin = 3;
int pinA = 7;
int pinB = 6;
int pinC = 5;
int pitch = 956;
int aState;
int aLastState;
void setup() {
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
//pinMode (pinC,INPUT);
//digitalWrite(pinC, HIGH);
pinMode(buzzer_pin, OUTPUT);
Serial.begin (9600);
// Starts output
aLastState = digitalRead(pinA);
}
void loop() {
aState = digitalRead(pinA);
if (aState != aLastState){
// outputB != outputA state, encoder is rotating clockwise
if (digitalRead(pinB) != aState) {
movedClockwise();
//counter ++;
}
else {
//counter --;
movedCounterClockwise();
}
play_specific_pitch(pitch);
}
aLastState = aState;
}
void movedClockwise() {
if ((pitch) < 3000) {
pitch = pitch + 100;
}
}
void movedCounterClockwise() {
if ((pitch) > 100) {
pitch = pitch - 100;
}
}
void play_specific_pitch(int pitch) {
for (long i = 0; i < 300000L; i += pitch * 2) {
digitalWrite(buzzer_pin, HIGH);
delayMicroseconds(pitch);
digitalWrite(buzzer_pin, LOW);
delayMicroseconds(pitch);
}
Serial.println(pitch);
}

I hope you enjoyed this tutorial and found it thorough and useful!

--

--

Melanie Chow

Computer Science student at the University of Chicago, interested in a bunch of random things.