Rotary Encoder Using Arduino Nano

Introduction: Rotary Encoder Using Arduino Nano

About: someone who likes electronics Yt channel : https://www.youtube.com/c/mrsottong IG 1 : sottong_teknik

Hi everyone,

In this article I will make a tutorial on how to use a rotary encoder using Arduino Nano. To use this Rotary encoder you don't need an external library. So we can directly create programs without adding libraries first. ok

Let's start the tutorial.

Step 1: Required Components

The component that you need:

Step 2: Assemble All Components

see the picture above for a guide to assemble it.

Arduino to Rotary Encoder

GND ==> GND

+5V ==> +

D6 ==> CLK (PinA)

D7 ==> DT (PinB)

Step 3: Programming

Please download the sketch I have prepared below.

Step 4: Result

When the Rotary Encoder is turned to the left, the resulting value will be smaller.

When the Rotary Encoder is rotated to the right, the resulting value will be even greater.

Be the First to Share

    Recommendations

    • Micro:bit Contest

      Micro:bit Contest
    • Plastic Challenge

      Plastic Challenge
    • Rocks, Gems, and Stones Speed Challenge

      Rocks, Gems, and Stones Speed Challenge

    2 Comments

    0
    ChuckM43
    ChuckM43

    10 months ago

    Hello All,

    The lack of details was bugging me... So, I made a sketch that will work for this annoying post. Hope it helps someone!!!
    (FYI - This is my first Arduino project so I am assuming there are flaws, but it works.)


    //SKETCH STARTS BELOW
    int pinA = 6;
    int pinB = 7;
    int pinALast;
    int counter = 0; //Min of 0 and Max of 19 due to one full encoder turn
    unsigned long currentTime;
    unsigned long loopTime;
    unsigned char encoder_A;
    unsigned char encoder_B;
    unsigned char encoder_A_prev=0;

    void setup() {
    pinMode (pinA,INPUT);
    pinMode (pinB,INPUT);
    pinALast = digitalRead(pinA);
    Serial.begin (9600);
    currentTime = millis();
    loopTime = currentTime;
    Serial.print("Let's get started - Rotary Encoder is at position ");
    Serial.print(counter);
    Serial.println();
    }

    void loop() {
    // get the current elapsed time
    currentTime = millis();
    if(currentTime >= (loopTime + 5)){
    encoder_A = digitalRead(pinA); // Read encoder pins
    encoder_B = digitalRead(pinB);
    if((!encoder_A) && (encoder_A_prev)){
    // A has gone from high to low
    if(encoder_B) {
    // B is high so counter-clockwise
    counter --;
    if(counter < 0){
    counter = 20;
    Serial.print("Turned Counter Clockwise: Position reseting to ");
    Serial.print(counter);
    Serial.println();
    }
    else
    {
    Serial.print("Turned Counter Clockwise: Position decreased to ");
    Serial.print(counter);
    Serial.println();
    }
    }
    else {
    // B is low so clockwise
    counter ++;
    if(counter > 19){
    counter = 0;
    Serial.print("Turned Clockwise: Position reseting to ");
    Serial.print(counter);
    Serial.println();
    }
    else
    {
    Serial.print("Turned Clockwise: Position increased to ");
    Serial.print(counter);
    Serial.println();
    }
    }

    }
    encoder_A_prev = encoder_A; // Store value of A for next time
    loopTime = currentTime; // Updates loopTime
    }
    }

    0
    jlau001g
    jlau001g

    Question 1 year ago on Step 3

    The download link for the Arduino sketch incorrectly points to the same general description of the project instead of the code.