Introduction: Rotary Encoder Switch

This Instructable covers the shaft switch on mechanical incremental rotary encoders.

There are linear encoders, as well as optical, magnetic, etc. encoders. These later encoders are relatively expensive, and not as common as the more usual inexpensive mechanical incremental rotary encoders, and they are not as popular with microcontroller makers.

The coding for the encoder section is covered in many Instructables. However, if Instructable users feel this coverage is not adequate, I would be happy to write a tutorial addressing this topic. Just let me know in the comment area after this Instructable.

Fortunately, almost all rotary encoders come with shaft switches, and this is what is covered here.

The two main types of rotary encoders are “absolute” and ”incremental”. In this Instructable, we will address only the shaft switch on incremental encoders. These encoders are less costly, and perhaps that is the reason they are more widely used than absolute encoders.

The output of an absolute encoder provides the angular position when the power is applied. While the output of an incremental rotary encoder provides information on the amount of movement and direction, clockwise or counter-clockwise, which can then be processed, in a sketch, to provide information about the number of times the shaft was rotated and in what direction, and this information used to find the angular location of the attached shaft from some starting reference point.

Incremental encoders can be low or high precision. In our examples, we will use low precision encoders which have only twenty (20) fixed positions/detents for 360 degrees. Thus, these encoders have a precision of only 18 degrees. However, the precision of the encoder section does not effect how the shaft switch works.

The advantage of low precision incremental rotary encoders is that they are relatively inexpensive. At the time of writing this Instructable, I was able to obtain them from China, in bulk, for approximately USD 0.45, i.e about 45 cents each . These low cost encoders none-the-less come with shaft switches

The shaft switch on a rotary encoder can be thought of as a Momentary Button Switch, providing a HIGH or (1) if not pressed and a LOW or (0) when pressed.

Often inexpensive incremental encoders provide enough precision for the task for which they are needed. High precision incremental rotary encoders are also available, and often have an angular precision in a single digit. As the precision of the encoder section is irrelevant to the shaft switch, it was appropriate to use relatively inexpensive encoders for this Instrucable.

The rotary encoder devices used in this Instructable have five (5) pins, as can be seen in the attached photograph.

These are respectively from left to right: GND, +, SW, DT, and CLK.

The first two pins GND and + are the power pins. GND connects to any ground on our Arduino UNO, or any microcontroller, and plus connects to 5 VDC .

SW stands for switch. The switch is what this Instructable addresses. The shaft on the encoder can be pushed in and provides the switch.

As noted earlier, the switch gives a High, or one (1) when it is not pressed, and a LOW, or zero (0), when it is pressed. When pressed the shaft makes a satisfying click. This audio que also confirms that the shaft has been pressed.

The switch and the encorder can, and probably should, be considered as two separate devices, as is done in this Instructable.

The last two pins DT (data) and CLK (clock) are the two wipers for our encoder. In the detent positions the wipers are connected to 5 VDC. When they both are not connected to 5 VDC they are connected to ground. These wipers have no interaction with the shaft switch.

Thus, this Instructable does not address the wipers.

Supplies

Supplies are discussed with each sketch, as they are different for different sketches.

Step 1: Incremental Rotary Encoder Switch-Example 1

The supplies needed for this example are
- An Arduino UNO clone or UNO

- Three (3) male to male Dupont wires

- An incremental rotary encoder device

- An experimental platform (optional)

- A small breadboard (optional)


/*
* Programmer R. Jordan Kreindler * October 9, 2020

* Sketch to check if switch is pressed

* If switch not pressed value = 1 or HIGH

* If switch pressed value = 0 or LOW

*/

#define Switch 8

#define delay1 500


void setup() {

pinMode (Switch, INPUT_PULLUP); // Set the switch as input, so that when not pressed the value equals one (1) Serial.begin(9600); }

void loop() {

Serial.print("Encoder Switch = ");

Serial.println(digitalRead(Switch)); // print current value of Switch

delay(delay1);

}

Step 2: Incremental Rotary Encoder Switch-Example 2

In this step we have almost the same sketch as in the Step above.

However, here we display the results on an LCD so that we are not tethered to our computer, where we need the display console.

In addition to the supplies for the Step above, i.e,:

- An Arduino UNO clone or UNO

- Three (3) male to male Dupont wires

- An incremental rotary encoder device

- An experimental platform (optional)

- A small breadboard (optional)

We also need an

- I2C 1602 display, and

- Four (4) female to male Dupont wires


/*
* Programmer R. Jordan Kreindler

* October 12, 2020

* Sketch to check if switch is pressed and display reuults on LCD

* If switch not pressed value = 1 or HIGH

* If switch pressed value = 0 or LOW

*/

#include LiquidCrystal_I2C.h // liquidCrystal_I2.h should be enclosed in < and > brackets

LiquidCrystal_I2C lcd(0x27, 16, 2);

// set the LCD address to 0x27 for a 16 chars and 2 line LCD display


#define Switch 8

#define delay1 500


void setup() {


pinMode (Switch, INPUT_PULLUP);

// Set the switch as input, so that when not pressed the value equals one (1)


lcd.init(); // Initialize LCD

lcd.backlight(); // Turn backlight On

lcd.setCursor(0, 0); // Set cursor at position zero(0) on first line of 1602 LCD

lcd.print(" Encoder Switch ");

}


void loop() {

lcd.setCursor(0, 1); // Set cursor at position zero (0) on second line of 1602 LCD

if(digitalRead(Switch) == 1) {

lcd.println(" is unpressed ");

}

if(digitalRead(Switch) == 0) {

lcd.println(" is pressed ");

}

delay(delay1);

}

Step 3: Incremental Rotary Encoder Switch-Example 3

The supplies needed for this example are

- An Arduino UNO clone or an Arduino UNO

- Six male to male Dupont wires

- An incremental rotary encoder device

- A LED (here I used a 10 mm Red LED, but a 3, 5, or 8 mm LED of any color will work as well).

- A current limiting 330 Ohm resistor (here I used a one watt resistor, but almost any wattage and a resistance from 220 ohm to 470 ohm should work as well)

- An experimental platform (optional)

- A small breadboard (optional)

Here we use the switch on the incremental rotary encoder to turn On an LED.

/*

Programmer R. Jordan Kreindler

October 10, 2020

Sketch to light LED if incremental rotary switch is pressed

If switch is not pressed value = 1 or HIGH

If switch is pressed value = 0 or LOW

*/

#define Switch 8

#define LEDPin 9

byte val = 0;

void setup() {

pinMode (Switch, INPUT_PULLUP);

// Set the switch as input, so that when not pressed the value equals one (1)


pinMode(LEDPin, OUTPUT);

}

void loop() {

val = digitalRead(Switch);

if (val == 1) {

digitalWrite(LEDPin, LOW); // turn the LED Off

}

if (val == 0) {

digitalWrite(LEDPin, HIGH); // turn the LED On

}

}

Step 4: Incremental Rotary Encoder Switch-Example 4

The sketch here is similar the sketch in the Step above in that the switch is read and action taken based on that reading.

However, here there is a display produced by multiple LEDs rather than the use of a single LED as in the previous Step.

The supplies needed for this example/Step are
- An Arduino UNO clone or an Arduino UNO

- Thirteen male to male Dupont wires

- An incremental rotary encoder device with shaft switch

- Six LEDs (here I used 10 mm LEDs but 3, 5, or 8 mm LEDs of any color will work as well).

- Six current limiting 330 Ohm resistors (here I used one watt resistors, but almost any wattage, and a resistance from 220 ohm to 470 ohm should work as well)

- An optional experimental platform

- An optional small breadboard.


/*

Program by R. Jordan Kreindler

Date: October 22, 2020

LED Display after rotary encoder switch is pressed

*/

#define Switch 9

byte val = 2;

int delay1 = 75;

#define firstLED 2

#define lastLED 7

void setup() {

for (int i = firstLED; i <= lastLED; i++) {

pinMode(i, OUTPUT);

}

}

void loop() {

val = digitalRead(Switch);

if ( val == 0) {

for (int i = firstLED; i < lastLED; i++) {

digitalWrite(i, HIGH);

delay(delay1);

digitalWrite(i, LOW);

}

for (int i = lastLED; i >= firstLED; i-- ) {

digitalWrite(i, HIGH);

delay(delay1);

digitalWrite(i, LOW);

}

}

}

Step 5: Afterwards

If you have come to this point - congratulations. You should now have a basic understanding of some of the capabilities of the incremental rotary encoder shaft switch covered in this tutorial. I hope you found this Instructable interesting and useful.

If you have any comments, suggestions, or questions related to this tutorial, please be kind enough to add your comments below, if there are any, or start this section.

If you have any thoughts or questions related to the shaft switch not covered in this tutorial, or any suggestions for how I could improve this, I would be pleased to hear from you.

You can reach me at transiintbox@gmail.com. (Please replace the second 'i' with an 'e' to contact me).