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() {
  encoder1.minimum(0);
  encoder1.maximum(100);
  encoder1.position(50);
  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) {
    encoder1.position(50);
  }
    //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] = '=';
    }
    Serial.println(buf);
  }
}

Dullbits.com

©DullBits.com 2017, All rights reserved and stuff like that...
Now featuring newly broken pages.