Project description

This project shows how to use an old hard drive motor as a rotary encoder, i.e. to detect when the motor is turned by hand, by how much and in which direction. The principle is that turning the motor generate small currents in the coils of the motor. These small currents are amplified and fed in a micro controller to determine in which direction and by how much the motor turns.

The application is a small electronic toy which lights up a circle of 8 LEDs according to the rotations of the motor (see video here and more pictures there).

Some technical details

Here is the schematic of the amplification stage. The motor I used had three wires. One is connected to ground through a 10k resistor and used a reference. The two others are amplified using operational amplifiers. The output of the amplifier is the fed in two digital inputs of a microcontroller (the PCO and PC1 pins of an ATMega168 in this case but any kind of microcontroller could be used).

The listing below shows the code used to read the inputs from the motor and make call to two different functions for each step in one direction or the other.

//The outputs of the amplifiers are connected to PC0 and PC1
#define ENCVAL (PINENC & 0x03) // PC0 and PC1

//Default callback that does nothing.
void nothing() { }

//Function pointers to register the callbacks
void (* enc_inc)() = &nothing;
void (* enc_dec)() = &nothing;

//Values read last time the encoder was pooled
int enc_last = 0;

//Function to be called every 1ms or less to read the encoder
void pool_enc() {
	int val =  ENCVAL;
	if (val == enc_last) return;
	else if (enc_last == 1 && val == 3) (*enc_dec)();
	else if (enc_last == 1 && val == 0) (*enc_inc)();
	else if (enc_last == 2 && val == 0) (*enc_dec)();
	else if (enc_last == 2 && val == 3) (*enc_inc)();
	enc_last = val;
 }

Download

  • Complete schematic and code for the project. The microcontroller showed on the diagram is an ATMega8 but I have used an ATMega168. These two are nearly identical and the application should run on both (maybe some minor adjustments needed for power management). When it comes to the rotary encoder very similar code should run on any AVR microcontroller.

Links


Contact

Franck FLEUREY
SINTEF IKT,
Po. Box 124 Blindern,
0314 Oslo
Norway

franck@fleurey.com

Links

edit