Skip to main content

Menu

Rotary Encoder Module Arduino Compatible

ARD2-2240
Arduino Compatible ARD2 Rotary Encoder Module

$3.95

QTYPrice
1 $3.95

Description

This Rotary Encoder Module is a rotary input device that provides an indication of how much the knob has been rotated and what direction it is rotating in. It is a great device for stepper and servo motor control. You could also use it to control devices like digital potentiometers.

A rotary encoder has a fixed number of positions per revolution. These positions are easily felt as small 'clicks' you turn the encoder. This module has thirty of these positions.

Test Code

int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
int aPin = 6;
int bPin = 7;
int buttonPin = 5;
int state = 0;
int longPeriod = 5000; // Time at green or red
int shortPeriod = 700; // Time period when changing
int targetCount = shortPeriod;
int count = 0;

 

void setup() {
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}

void loop() {
count++;
if (digitalRead(buttonPin)) {
setLights(HIGH, HIGH, HIGH);
}
else {
int change = getEncoderTurn();
int newPeriod = longPeriod + (change * 1000);
if (newPeriod >= 1000 && newPeriod <= 10000) {
longPeriod = newPeriod;
}
if (count > targetCount) {
setState();
count = 0;
}
}
delay(1);
}

int getEncoderTurn () {
// Return -1, 0, or +1
static int oldA = LOW;
static int oldB = LOW;
int result = 0;
int newA = digitalRead(aPin);
int newB = digitalRead(bPin);
if (newA != oldA || newB != oldB) {
//Something has changed
if (oldA == LOW && newA == HIGH) {
result = - (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}

int setState() {
if (state == 0) {
setLights(HIGH, LOW, LOW);
targetCount = longPeriod;
state = 1;
}
else if (state == 1) {
setLights(HIGH, HIGH, LOW);
targetCount = shortPeriod;
state = 2;
}
else if (state == 2) {
setLights(LOW, LOW, HIGH);
targetCount = longPeriod;
state = 3;
}
else if (state == 3) {
setLights(LOW, HIGH, LOW);
targetCount = shortPeriod;
state = 0;
}
}

void setLights(int red, int yellow, int green) {
digitalWrite(redPin, red);
digitalWrite(yellowPin, yellow);
digitalWrite(greenPin, green);
}

Features

  • Converts the angular position or motion of a shaft or axle to an analog or digital code
  • Perfect for stepper & servo motor control
  • Applications include industrial controls, robotics, computer input devices, controlled stress rheometers, and rotating radar platforms

Specifications

Board Colour

Black

Material

PCB

Resources

You Might Also Like

All prices are in Australian Dollars and include GST. GST will be removed from your order if you are ordering from outside Australia.
Products may differ from pictured.
All trademarks and trade names are the properties of their respective owners. Wiltronics disclaims any proprietary interest in trademarks and trade names other than its own.

Rotary Encoder Module Arduino Compatible

ARD2-2240

$3.95

We use cookies to ensure you have the best experience on our site. If you continue to use this site you consent to the use of these cookies. OK

Find out more in our Privacy Policy