A rotary encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to an analog or digital code. There are two main types: absolute and incremental (relative). The output of absolute encoders indicates the current position of the shaft, making them angle transducers. The output of incremental encoders provides information about the motion of the shaft, which is typically further processed elsewhere into information such as speed, distance, and position. Rotary encoders are used in many applications that require precise shaft unlimited rotation—including industrial controls, robotics, special purpose photographic lenses,[1] computer input devices (such as optomechanical mice and trackballs), controlled stress rheometers, and rotating radar platforms.
10cm x 8cm x 6cm (3.94in x 3.15in x 2.36in)
Pinout of 1000ppr 5V Encoder ABZ Pulses Hollow shaft incremental encoder digital encoder:
- Red +5v
- White GND
- BLUE A
- GREEN B
- YELLOW
Package Include:
- 1 x 5V Hollow Shaft 1000PPR Rotary Incremental Encoder
Arduino Example code to measure Length.
//====================
#include
volatile unsigned long threshold = 10;
signed long pulse_counter = 1000;
#define encoder_a 2 // pin 2 only pin 2
#define encoder_b 8 // pin4 can be change
#define power_fail 4 // pin3 can be change
int calibration_pulses=0;
int mm_calibration=0;
int update_flag=0,debug_flag=0;
int current_mm_value=0,current_cm_value=0,old_cm_value=0;
//====================================================
void int0() {if ( digitalRead(encoder_b)) pulse_counter++;else pulse_counter–;}
void int1()
{
EEPROMWritelong(4, pulse_counter);
}
//====================================================
void setup()
{
Serial.begin(9600);Serial.setTimeout(50);
pinMode(encoder_a, INPUT);
pinMode(encoder_b, INPUT);
pinMode(power_fail, INPUT);
digitalWrite(power_fail, HIGH);
digitalWrite(encoder_a, HIGH); // enable internal pull up
digitalWrite(encoder_b, HIGH); // enable internal pull up
attachInterrupt(0, int0, RISING );
pulse_counter=EEPROMReadlong(0);
mm_calibration=EEPROMReadlong(4);
calibration_pulses=EEPROMReadlong(8);
calculate_variables();
old_cm_value=current_cm_value;
}
//============================================
void loop()
{
setting();
calculate_variables();
if(update_flag==1) {update_flag=0;Serial.println(current_cm_value);}
if(debug_flag==1) {debug_flag=0;updater_all_variables();}
if((current_cm_value>old_cm_value) || (current_cm_value// if(digitalRead(power_fail)==0){EEPROMWritelong(4, pulse_counter);while(digitalRead(power_fail)==0);}
int input_voltage = analogRead(A0);if(input_voltage<400){EEPROMWritelong(0, pulse_counter);while(analogRead(A0)<400)Serial.println(“LOW_VOLTAGE”);}
//Serial.println(input_voltage);
}
//============================================
void calculate_variables()
{
float cmm=mm_calibration;
float cp=calibration_pulses;
float float_pulse_counter=pulse_counter;
float float_mm=pulse_counter;
float_mm=float_pulse_counter*cmm/cp;
current_mm_value=float_mm;
current_cm_value=current_mm_value/10;
}
//===========================================
void updater_all_variables()
{
calculate_variables();
Serial.println(” “);
Serial.print(” pulse_counter=”);
Serial.print(pulse_counter);
Serial.print(” mm_calibration=”);
Serial.print(mm_calibration);
Serial.print(” calibration_pulses=”);
Serial.print(calibration_pulses);
Serial.print(” CURRENT MM=”);Serial.print(current_mm_value);
Serial.print(” CURRENT CM=”);Serial.println(current_cm_value);
}
//==============================================
void setting()
{
if(Serial.available())
{
String command=Serial.readString();
if(command==”clear”){Serial.println(“Clear”);pulse_counter=0;}
// else if(command==”calibrate”) calibration_pulses= pulse_counter;
else if(command==”update”) update_flag=1;
else if(command==”debug”) debug_flag=1;
else if(command==”set_last_pulse”)
{
Serial.println(“PLEASE ENTER LAST KNOWN PULSE”);while(Serial.available()==0);
command=Serial.readString();Serial.println(command);
pulse_counter= command.toInt();// String to Int
}
else if(command==”mm_calibration”)
{
Serial.println(“PLEASE ENTER VALUE”);while(Serial.available()==0);
command=Serial.readString();Serial.println(command);
mm_calibration= command.toInt();// String to Int
Serial.println(mm_calibration); // )>
calibration_pulses=pulse_counter;
EEPROMWritelong(0,pulse_counter); //Save current pulses
EEPROMWritelong(4,mm_calibration);//Save Exact value
EEPROMWritelong(8,calibration_pulses);//Save Exact value
Serial.println(“saved”);
}
}
}
//==============================================
void EEPROMWritelong(int address, long value)
{
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
//==============================================
long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
//==============================================