ElectroPeak
Published © GPL3+

Rotary Encoder: How It Works and How to Use with Arduino

In this tutorial, you’ll get to know how to use the rotary encoder. First, you’ll see some information about the rotational encoder.

BeginnerProtip2 hours1,834

Things used in this project

Hardware components

ElectroPeak Rotary Encoder Module with Push Switch
×1

Story

Read more

Code

Code

Arduino
Determining the Position of the Rotary Encoder Shaft
/*
Rotary Encoder - get the position
modified on 23 Feb 2019
by Saeed Hosseini
https://electropeak.com/learn/
*/
#define encoderOutA 6 // CLK
#define encoderOutB 7 // DT
int counter = 0;
int State;
int old_State;
void setup() {
pinMode (encoderOutA, INPUT);
pinMode (encoderOutB, INPUT);
Serial.begin (9600);
//Read First Position of Channel A
old_State = digitalRead(encoderOutA);
}
void loop() {
State = digitalRead(encoderOutA);
if (State != old_State)
{
if (digitalRead(encoderOutB) != State)
{
counter ++;
}
else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
old_State = State; // the first position was changed
}

code

Arduino
Controlling a LED Light with Shaft Rotation
/*
Rotary Encoder - LED Brightness Control
modified on 23 Feb 2019
by Saeed Hosseini
https://electropeak.com/learn/
*/
#define encoderOutA 6 // CLK
#define encoderOutB 7 // DT
#define LED 9 // LED , must connect to pwm pin
int brightness = 0;
int State;
int old_State;
void setup() {
pinMode (encoderOutA, INPUT);
pinMode (encoderOutB, INPUT);
pinMode (LED, INPUT);
Serial.begin (9600);
//Read First Position of Channel A
old_State = digitalRead(encoderOutA);
}
void loop() {
State = digitalRead(encoderOutA);
if (State != old_State)
{
if (digitalRead(encoderOutB) != State)
{
brightness ++;
}
else {
brightness --;
}
if (brightness >= 255) brightness = 255;
if (brightness <= 0) brightness = 0;
Serial.print("brightness: ");
Serial.println(brightness);
}
old_State = State; // the first position was changed
analogWrite(LED , brightness);
}

code

Arduino
Controlling DC Motor Speed and Direction with Interrupt
/*
  Rotary Encoder - Controlling a DC Motor using L293D Shield
  modified on 23 Feb 2019
  by Saeed Hosseini
  https://electropeak.com/learn/
*/

#include <AFMotor.h>

#define CLK 2
#define DT  5
#define SW  3

AF_DCMotor motor(1, MOTOR12_64KHZ);

int motor_dir = 0;
int State;
int old_State, change;
volatile int motor_speed = 0;
volatile boolean buttonState = false;

void setup() {
  Serial.begin(9600);
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);
  old_State = digitalRead(CLK);
  attachInterrupt (digitalPinToInterrupt(CLK), encoder_detect, CHANGE);
  attachInterrupt (digitalPinToInterrupt(SW), button_detect, FALLING);
}
void loop() {
  if (!buttonState)
  {
    if (motor_speed > 0)
    {
      motor.setSpeed(motor_speed);
      motor.run(FORWARD);
      Serial.print("Move Forward: ");
      Serial.println(motor_speed);
    }
    else if (motor_speed < 0)
    {
      motor.setSpeed((motor_speed) * (-1));
      motor.run(BACKWARD);
      Serial.print("Move Backward: ");
      Serial.println(motor_speed);
    }
  }
  if (buttonState == true || motor_speed == 0)
  {
    motor.run(RELEASE);
    Serial.println("Break");
  }
}

void encoder_detect ()  {
  buttonState = false;
  State = digitalRead(CLK);
  if (State != old_State)
  {
    if (digitalRead(DT) != State)
    {
      motor_speed ++;
      if (motor_speed >= 255) motor_speed = 255;
    }
    else {
      motor_speed --;
      if (motor_speed <= -255) motor_speed = -255;
    }
  }
  old_State = State;
}

void button_detect()
{
  buttonState = true;
}

Credits

ElectroPeak

ElectroPeak

57 projects • 735 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.

Comments