Continuing education

PIC projects

Shortwave radio

Broadcast radio

High voltage

Accessories

Miscellaneous



Gray Encoder + PIC = Intelligent Turns Counter

After I had completed the coil winder and tried it out on a few of my "patients", a problem arose. It's not easy to get the turns fall evenly side by side so that the outcome is uniform and professional looking. Turns overlap or fall on top of one another, then you have to unwind all the way down to the faulty spot, push the wire in the right track and keep on cranking. This tends to throw off the turns count, at least with me. Wouldn't it be nice to have some kind of entity keep close watch over this and always remember how many turns were already wound, how many had to be undone due to the latest mishap, and so on? An entity like a microprocessor. And it wouldn't it be extra nice to have the ability to catch the change in direction at a fraction of a turn, instead of than a full turn?

It all boils down to sensors. A modern MCU with all its advanced math capabilities is only as good as its eyes, its sensor. If it can't see, it can't drive. Designing a counter for my coil winding jig presented some special challenges. I wanted to be able to move the shaft, which needed its turns counted, freely in and out of the machine. Therefore, the sensor couldn't permanently attach to it, and, I didn't want to use a more exotic sensor types because that would have complicated the construction at home. Being that, my machine is hand-cranked, the sensing speed wouldn't be an issue.

Left on the table was using the Gray encoder as my sensor and implementing it optically without any mechanical contact. Wikipedia has an excellent page on this topic. It can "see" both forward and backward and if built correctly, provides for sufficient accuracy. I decided to separate it into two parts. The two diode-phototransistor pairs, placed 90 degrees apart on rotating circumference are permanently mounted on the inside surface of one of the spindle brackets. The counting vanes that pass between the pairs (which turn the phototransitors on and off) are permanently attached to the shaft as depicted in the photo on the left.

Here's CorelDraw v14 graphic of vanes I used. If you choose to replicate them exactly, you can download them here, otherwise you'll have to come up with your own dimensions. If you have three "blades" and two sets of photo sensors it turns out that both speed and direction can be sampled in 12 points during every full rotation - or every 30 degrees.

I never measured it, but assessment by eye rendered a top speed of 3 turns/second. For the MCU, that's triggering the encoder check subroutine every 27.7 ms. Assuming 4 MHz crystal and therefore 1 μs internal interval to carry out most instructions, it should be plenty of time for the MCU to handle it.

 

Here's the schematic. I chose PIC16F84 as my MCU running at 4 MHz externally. PORTB four lower bits accommodate a 2x16 character LCD in 4-wire mode. PORTB next two higher bits, RB4 and RB5, strobe LCD's RS and Enable signals. The encoder E1 sits on PORTA bits RA0 and RA1 which are pulled up by two 10 KOhm resistors R3 and R4. Being that the encoder states are relatively slow changing as calculated above, I used a simple bit-banging routine to check their respective states. However, should my reader want to process far speedier signals, say a motor spindle, I would have the MCU's interrupt service routine deal with the encoder instead.

U2 is a standard 5V regulator, powering both the MCU and the LCD. I used cheap and readily available LM78L05ACZ in TO-92 package.

And finally, as my projects are rarely 100% complete, I built in a in-circuit programming capability, consisting of R5, C2, R6 and socket ISP.

This being a very simple project, the circuit was again built on a piece of prototyping board, as I didn't see the need for a PCB. With the exception of the encoder and the LCD, all components are soldered onto the prototyping board. If you take a look at my coil winder page, you'll see where everything is located. The LCD was mounted in the middle of the base stock for the express purpose of making it as visible as possible.

The LCD can be any basic character mode, 2x16 symbols. You can buy it from Sparkfun Electronics if you don't want to do it my way and yank it out of a junked fax machine. To make the optical Gray encoder I used an older DELL optical mouse. Pry it open and on the PCB, find the two pairs of photodiode-transistors. Use an ohmmeter to identify the pinout. Finally, you can get the hex for the PIC16F84 here.

 

Happy counting, de Brian.