I’ve created a simple quadrature encoder using two low cost optical sensors from Deal Extreme. The encoder works very well by using interrupts to increment or decrement the position of the motor.

You can see a video here: http://istribute.com/e/B10X4qCOFr

There are three steps to creating a quadrature encoder. The first one is to create the encoder wheel. I made mine out of a black plastic plate that I cut into form. Second step is to attach each of the sensors so that the beam is broken when the plastic plate comes in the way. Finally, I’ve attached the C-code for counting revolutions.

Extending the drive shaft of the motor

Since the main shaft of the motor is where my gears are attached, I decided to attach my encoder wheel at the “back” of the motor.

This process was the most difficult part. I don’t know how to weld stuff, but for this project no real torque will be applied. Basically, I used a simple hacksaw to cut the nail and make the motor shaft less “smooth”. Then I used ordinary solder to attach it. I spun the motor to make sure the nail was fairly in line with the motor shaft.

Creating the optical encoder wheel

Basically, find a light weight thin plastic plate and cut it so that it is similar to my picture. If you’re really skilled with the knife, you can make more “wings” on the encoder wheel. The more wings, the higher resolution you get. My encoder wheel gives me 4 steps per revolution. Theoretically, I could get 8 steps per revolution if I can afford to use another interrupt for the second sensor.

The hole is from the nail I attached to the motor shaft.

Putting the motor and encoder wheel together I get this:

Placing the optical sensors

Each of the optical sensors need to be placed fairly close together. One of the sensors will act as a trigger, while the other sensor will tell us the direction. If it rotates one way, the other sensor will be HIGH, while rotating the other way makes the other sensor LOW.

As you can see, the optical encoders are placed really close to each other. You don’t have to place them this close, but the closer they are, the more “wings” you can have on your encoder wheel. If you have a proper encoder wheel, there will be a lot of small holes. What’s important then, is to place the encoders at a distance that is not equal to the distance between the holes. This will allow you to get direction as well as velocity.

Connecting the optical sensors to the Arduino

Hooking up the sensors to the Arduino is very simple. Both sensors have a +5V and a GND connection, which obviously needs to be connected to their corresponding power sources.

The center OUT pin on each encoder must be connected to pin 0 and pin 7 if you have the Arduino Leonardo and you are using my code below. Refer to this page if you have a different model: http://arduino.cc/en/Reference/attachInterrupt.

The Code

I just hacked together this code to see that it was working. For my final robot, I’ll probably put everything into a library.

int INTERRUPT_ID = 2; // Pin 0 on Arduino Leonardo int DIRECTION_PIN = 7; // Use any available pin int pos = 0; // The current motor 1 position, should be set to 0 whenever reading void setup() { // put your setup code here, to run once: attachInterrupt(INTERRUPT_ID, fall, FALLING); } void fall() { if(digitalRead(DIRECTION_PIN)==HIGH) pos++; else pos--; } void loop() { Serial.println(pos); delay(20); }