Quadrature Encoder

This installment of Scott's bread boarding adventures, involves a quadrature encoder.  I'm not sure where I got this one, and there are no part numbers, so no datasheet.  My version is a 20 steps per revolution, no detent, and a momentary push button on top.  I bent the two structural legs out to the side to make affixing easier.

The easy part of the project is the code.  There are a lot of ways to get this running, but the easiest and most reliable is to grab a copy of Keith Neufeld's excellent library.




I found that my encoder uses a maximum of 0.3mA.
Use a current limiting resistor on the momentary switch side to reduce power.  I used a 20k resistor.  On some designs you might need to include a pull-up resistor, but I used the internal resistors of the ATMEGA168 processor.


extern \"C\" {
  #include \"WConstants.h\"
#include \"Quadrature.h\"

unsigned int encoder1_pos;
unsigned int encoder1_pos_old;
Quadrature encoder1(7, 8);          // Connected to pins 7 and 8
const int pin_reset = 9;            // Button on encoder sunk on pin 9 with current limiting resistor (22k)

void setup() {
  pinMode(pin_reset, INPUT);
  digitalWrite(pin_reset, HIGH);    // Turn on internal pull up resistors
  Serial.begin (115200);

void loop() {
  char buf[127] = \"\";
  if (digitalRead(pin_reset) == LOW) {
    //Check for change in position
  encoder1_pos = encoder1.position();
  if (encoder1_pos != encoder1_pos_old) {
    encoder1_pos_old = encoder1_pos;
    for (int j=0; j<=encoder1_pos; j++) {
      buf[j] = \'=\';

created: Dec. 1, 2013, 1:01 a.m.
modified: April 14, 2019, 12:50 a.m.