Project tutorial

Quadrature Encoder Demo © CC BY

Use a 3D printed encoder wheel, LED, and 2 photocells to demonstrate how a quadrature encoder works.

  • 3,790 views
  • 3 comments
  • 11 respects

Components and supplies

Necessary tools and machines

3drag
3D Printer (generic)
09507 01
Soldering iron (generic)

About this project

This will make a quadrature encoder demo (or the world's worst dimmer switch). I originally created this project to demonstrate how optical encoders work as part of a YouTube video for SparkFun.

Print Encoder Wheel

Head to https://www.thingiverse.com/thing:1147656 and download the encoder disc files. Use a 3D printer to print the disc with 5 holes. I recommend scaling the print up to 3x size.

Mount Encoder Disc

Use a bolt, dowel, etc. to allow the encoder wheel to spin. Mount it to some kind of structure. I used an Actobotics part.

Build Circuit

Follow the Fritzing diagram to connect the various components.

Mount the photocells on the opposite side of the encoder disc as the LED. You want the LED to shine through the holes in the encoder disc.

Upload Code

Copy the code to an Arduino sketch and upload it to the Pro Mini.

Run!

As you spin the encoder disc in one direction, it causes the blue LED (listed as "lamp" in the code) to get brighter. Spin the disc in the other direction, and the LED gets dimmer.

Going Further

This is obviously a project for demo purposes only. You can buy real encoders that perform much better (and probably for less money, too). If you want to challenge yourself, see if you can make the encoder respond to both edges on the in-phase detector, instead of just a falling edge.

Code

Quadrature Encoder DemoArduino
Copy this into an Arduino sketch
/**
 * Quadrature Encoder Demo
 * Author: Shawn Hymel (SparkFun Electronics)
 * Date: July 20, 2017
 *
 * Two photocells look for light changes through an encoder
 * disc (can be 3D printed or cut out of cardboard). As the
 * encoder disc spins in one direction, the connected LED
 * ("lamp") gets brighter. If the encoder dic is spun in the
 * other direction, the LED gets dimmer.
 *
 * License: Beerware (https://en.wikipedia.org/wiki/Beerware)
 */

// Parameters
const int threshold = 600;
const float multiplier = 1.4;

// Pins
const int i_pin = A1;
const int q_pin = A0;
const int led_pin = 8;
const int lamp_pin = 9;

// Globals
int i_prev = 0;
int q_prev = 0;
float brightness = 1;

void setup() {

  // Set up pins
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, HIGH);
  pinMode(lamp_pin, OUTPUT);

  // Set up Serial
  Serial.begin(9600);

  // Measure initial Q
  i_prev = (analogRead(i_pin) > threshold) ? 1 : 0;
}

void loop() {

  // Measure
  int i_state = (analogRead(i_pin) > threshold) ? 1 : 0;
  int q_state = (analogRead(q_pin) > threshold) ? 1 : 0;

  // Look for falling edge on I
  if ( (i_prev != i_state) && (i_state == 0) ) {

    // If I and Q are the same, CW, otherwise CCW
    if ( i_state == q_state ) {
      brightness *= multiplier;
      if ( brightness >= 255 ) brightness = 255;
    } else {
      brightness /= multiplier;
      if ( brightness <= 1 ) brightness = 1;
    }

    // Print counter
    Serial.println(brightness);

    // Set LED
    analogWrite(lamp_pin, (int)brightness);
  }

  // Save state for next sample
  i_prev = i_state;

  delay(10);
}

Schematics

Quadrature Encoder Demo
Q encoder bb xrotxcmhtu

Comments

Similar projects you might like

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 454,706 views
  • 42 comments
  • 237 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 285,521 views
  • 95 comments
  • 671 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 229,423 views
  • 38 comments
  • 237 respects

OpenCat

Project in progress by Team Petoi

  • 196,029 views
  • 154 comments
  • 1,363 respects
Add projectSign up / Login