Rotary Encoder module with Arduino Nano

Rotary Encoder module with Arduino Nano

Hello, welcome back. In this tutorial, we will learn what is the rotary encoder module and how to work with an Arduino nano. With this rotary encoder, we can get the angular position as a signal. That is, it can be obtained in the form of an analog value or a digital value. This is similar to the shape of a potentiometer. The output can be obtained by rotating the knob here. Also, we can use this module for making CNC machines and robots. I hope the knowledge in this tutorial will be more useful for your Arduino project. Keep reading.

How does a rotary encoder work?

These are mainly two types.

  1. Absolute.
  2. Incremental.

In this tutorial, we will talk about the incremental type. It also contains a disk that connects 3 terminals. That is, includes a ground pin and two output pins. When we turn the knob here, it connects to the ground and from time to time. It is as follows.

Rotary Encoder module with Arduino Nano

Then two signals are generated through these two output terminals. Also, these two signals come in two forms, clockwise and counterclockwise, depending on the direction in which we turn the knob. These two signals are displaced 90 degrees from each other. These are as follows.

1.Clockwise.

When the knob is rotated clockwise, the first output is ahead of the second output.

Rotary Encoder module with Arduino Nano

2.Counterclockwise.

When the knob is rotated counterclockwise, the second output is ahead of the first output.

Rotary Encoder module with Arduino Nano

PIN structure of this module

Rotary Encoder module pin structure
PIN structure of Rotary encoder module

OK, let’s learn how to connect this module to the Arduino step by step. The required components are given below.

Step 1

Firstly, identify these components.

Arduino Nano board

Rotary encoder

RGB LED bulb

LED bulb

LCD display

I2C module

10k resistor

180ohm resistor

Breadboard

Jumper wires

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

Rotary Encoder module with Arduino Nano

Step 3

Thirdly, let’s creates the program for this project. It is as follows.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DT 11
#define CLK 12
#define SW 10
int counter = 0;
int a;
int b;

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  pinMode(DT, INPUT);
  pinMode(CLK, INPUT);
  pinMode(SW, INPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  a = digitalRead(CLK);
}
void loop() {
  Switch();
  b = digitalRead(CLK);
  if (a != b) {
    if (digitalRead(DT) != b) {
      counter++;
    } else {
      counter--;
    }
    if (counter == 2 || counter == 3) {
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
    } else if (counter == 4 || counter == 5) {
      digitalWrite(3, HIGH);
      digitalWrite(2, LOW);
      digitalWrite(4, LOW);
    } else if (counter == 6 || counter == 7) {
      digitalWrite(4, HIGH);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
    } else {
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
    }

    lcd.setCursor(0, 0);
    lcd.print("Position : ");
    lcd.print(counter);
    lcd.print("  ");
  }
  a = b;
}
void Switch() {
  bool s = digitalRead(SW);
  if (s == 0) {
    digitalWrite(5, HIGH);
  } else {
    digitalWrite(5, LOW);
  }
}

Code explanation

First, the I2C library is included.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

Secondly, the rotary encoder pins are defined. Also, three variables have been created to support the program.

#define DT 11
#define CLK 12
#define SW 10
int counter = 0;
int a;
int b;

In the setup function, the LCD display starts. Also, the LED pin and RGB LED pins are set as OUTPUT.

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  pinMode(DT, INPUT);
  pinMode(CLK, INPUT);
  pinMode(SW, INPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  a = digitalRead(CLK);
}

In the loop function, first the switch function called.

Switch();

Next, the values of the module are read and put into the “Counter” variable.

  b = digitalRead(CLK);
  if (a != b) {
    if (digitalRead(DT) != b) {
      counter++;
    } else {
      counter--;
    }

And then, RGB LED colors are activated one by one using these values.

   if (counter == 2 || counter == 3) {
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
    } else if (counter == 4 || counter == 5) {
      digitalWrite(3, HIGH);
      digitalWrite(2, LOW);
      digitalWrite(4, LOW);
    } else if (counter == 6 || counter == 7) {
      digitalWrite(4, HIGH);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
    } else {
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
    }

After, this code prints the values of the rotary encoder module on the LCD display.

    lcd.setCursor(0, 0);
    lcd.print("Position : ");
    lcd.print(counter);
    lcd.print("  ");

This is the push button function. Using this function we can turn the LED on and off.

void Switch() {
  bool s = digitalRead(SW);
  if (s == 0) {
    digitalWrite(5, HIGH);
  } else {
    digitalWrite(5, LOW);
  }
}

Step 4

Lastly, select board and port. After, upload this code to the Arduino board.

OK, enjoy this tutorial. The full video guide is given below. So, we will meet in the next tutorial.

Rotary Encoder module with Arduino Nano

Similar Posts

Leave a Reply

Your email address will not be published.