Rotating Sprocket Wheel Generator

From Wiki
Jump to: navigation, search


The content of this page is by Matt Bennett, whose original page is located here. I have duplicated this content into this wiki page because has gone off-line a couple times, and I think this project is just too cool to lose. All content is copyright by Matt Bennett, and all work is his. Think of this page as a back-up :)


My friend Arthur, in his lifelong quest for circuits that don't really do anything useful, but are really neat nonetheless, built a really neat Lissajous pattern generator. This Lissajous pattern is not your traditional circle, oval, or figure-8, it is a circle with varying amplitude, in a pattern that makes it look like a sprocket. Not satisfied with that, he also made it so that the sprocket rotates.

An interesting side-effect is that this circuit very clearly shows some of the shortcomings of digital oscilloscopes. Whenever an oscilloscope salesman comes around, he whips out this box with a switch and 2 BNC jacks. It has been known to render speechless the best salesmen from HP and Tektronix. They usually dismiss it as a toy, but I think that is just because they have to follow the party line: Analog scopes are dead. I have seen a digital scope or two accurately mimic the analog, most notably the Digital Phosphor scopes from Tektronix, but in general, the RSWG breaks digital scopes.

I moved to another job about 1500 miles away from where Art spends his time being surly, making cool circuits and serenading passersby with his Theremins. After a short time at my new lab I was called upon to specify some oscilloscopes. I decided that I needed my own RSWG. Arthur has been so kind as to publish the schematics and theory behind his RSWG. His circuit works great, but it is pretty complex, and the construction would tax my patience, since he has managed to construct his RSWG with nothing but discrete ICs and a handful of ancillary components.

RSWG Trace.jpg
I, on the other hand, like most engineers, decided I would rather start from the ground up and roll my own. My first objective is to reduce parts count. When I was working for the Army, I made a really neat circuit that was among other things, a frequency synthesizer. It would generate two sine-waves in quadrature with a minimum of external components (just a D/A converter). The problem with re-using that code was that I no longer had easy access to an Altera development system. On the other hand, I had been using a PICStart+ for some of my other projects at the lab, and if all else fails, I would have the POHPIC Programmer that I designed myself. I decided to try basing it on a Microchip PIC, the 16F84. The 'F84 has some features that are completely unnecessary for this project, notably non-volatile memory. But the big advantage of it is that 1) I had some handy, and 2) the flash program memory supports my style of simulate, burn, crash, repeat embedded programming, without the hassle of an EPROM eraser. This code would easily port over to one of the PICs without flash memory (such as the 16C61).

The Circle

First of all, I had to see if I could even draw a simple circle on the oscilloscope screen. To do this, you must put out 2 sine waves in quadrature (a fancy term for 90° out of phase). When you feed these two waves into the X and Y inputs of an oscilloscope, you draw a circle on the oscilloscope's screen.

RSWG R2R.gif
A sine wave is about as analog a signal as you can get. In case you didn't realize it, the 'F84 only has digital outputs, so I needed to convert a digital set of values to an analog voltage. I could use a D/A converter, but I didn't have any handy. What I did have handy was a huge number of precision resistors. Just the recipie for an R/2R resistor ladder, which is what you see to the left. To use an R/2R D/A network, you place the digital word you want to convert to an analog voltage on the input pins. The closer the 1s are to your supply voltage, and the 0s are to ground, the better, since these voltages are added together through these resistor dividers to generate the analog voltage. The most significant bit goes on the upper-most input of the diagram to the left, and the least significant bit goes into the bottom-most input.

The 16F84 has 13 outputs. All 13 can serve as inputs, but only 12 are normal outputs, one is an open-drain. An open-drain output has no active pull up- you have to provide an external (passive) pull up. Since the 'F84 is CMOS based, this leaves us 12 outputs that have good, low impedance 1's or 0's. We want two channels, 12/2 = 6 bits/channel. Using the rough approximation of 6 dB/bit, 6 bits/channel * 6 dB/bit gives us approximately 36 dB/channel of dynamic range. This is still very visible on a scope, so I needed some filtering.

I put this all together, and spent some time banging out the PIC assembly code to generate the quadrature sine-waves. To make it simple, I decided to use look-up tables to generate the waves. PICs are 8 bit RISC microcontrollers. They have a lot of limitations, one major one is that you can only do a 254 element look up table without pain. To generate two sine waves truly in quadrature, I had to offset the two sine-wave look up tables by 25% of their full length. It would have been really easy if I could use a 255 element look up table, since 255/4=64. The next smaller number that is a multiple of 4 is 252, so I decided to use a (63*4=) 252 element table to generate the quadrature sine waves.

I wrote a spreadsheet to generate the values I needed for these lookup tables, packing them together because of the organization of the output registers on the PIC. I'm using bits 5-0 of port B for one channel, and bits 3-0 of port A, and bits 7-6 of port B for the second channel. This is 12 bits of information, so I made 2 look up tables, one lookup table for port A, the other for port B. This is a bit inefficient in terms of memory use, but it is quick.

Next I put the PIC part of the circuit together- nothing special here, I just used a 10MHz crystal since it is the fastest that the PIC is speced for, and I wanted to make sure I had as much speed as possible. Don't ever forget the de-coupling capacitors, locate them as close as possible to the PIC with as short a lead length as you can arrange. I try to solder the de-coupling capacitors directly to the socket.

Raw, the output of the R/2R D/A converter is still obviously quantized. I added a single pole passive low-pass filter to the output (a simple RC filter, where f(3dB)=1/(2piRC). After this simple filter- the circle was perfect as far as I could tell from the oscilloscope. Success.

The Teeth

Now that I had the circle, I needed to add teeth to make it a sprocket wheel. Since I wanted to keep the parts count low, I wanted to keep any amplifiers out of it. At first I thought I could use the open-drain output to pull the output down- well there's a problem with that approach. The open drain only pulls the output down to ground in one direction, down to ground. The sine wave is centered around a value, the value that I would get if I was to integrate wave over a single period. I need to attenuate the wave with respect to that value. Secondly- the output of the R/2R D/A converter is pretty high impedance. Without any impedance buffers, I would have to make the impedance of the first filter much higher than the impedance of the D/A converter. Any networks that followed the first filter would have to be even higher impedance still. I didn't see any easy way around these problems without adding more active devices.

Since I had to add some amplification, I decided to make things easy and use op-amps. First I needed to place the output of the R/2R D/A converter at a voltage in-between the rails of the op-amp. Using a capacitor and a resistive voltage divider is the most traditional way. But I already had to use a resistor and a capacitor in the low-pass filter, so why not "split" the capacitor and use it to serve as the divider? It works great, and performs two functions (low pass and voltage divider) for slightly more than the effort of one (adding a capacitor).

To actually generate the teeth I used the only I/O pin left, the open-drain, with a pull-up resistor. This goes to the control input on a 4066 bilateral switch. The resistor between the op-amps does nothing while the switch is open, but once it is closed, it serves as one of the resistors of a voltage divider. I decided that I wanted the tooth height to be one sixth of the radius of the circle. To make that so, the resistors have to be chosen to fit this equation 5/6= R30||R29/(R30||R29 + R26). Since the voltages are centered around 2.5V, R30 must equal R29. If we set R29=R30=100K, R26=10K.

RSWG Out.gif

The final op-amp is a simple voltage follower, there to buffer the outputs for the rest of the world. C12 adds a little low pass filtering to try and connect the flats and the lands of the sprocket wheel. C9 serves to remove any DC component the signal may have.

I started out with a 252 element look up table, but I ran into a problem- you can't divide a power of 2 into 252 that will leave us with a reasonable number of teeth. I really needed to have a 256 element look-up table, but that wasn't going to happen because of the architecture of the PIC, and my need for speed. I decided to change the size of the lookup table to 128, but put each element out twice. That way I could just key the tooth output off a single bit of the counter that keeps track of the pointer into the look-up table.

Now that the tooth generation was complete, I measured the actual output frequency of the RSWG. I needed this to calculate the component values for the low pass network. I measured the outputs of the RSWG to be approximately 260 Hz. For convenience sake, I chose to use 2 0.01uF capacitors as the voltage divider, since I have a far better selection of resistors than capacitors. Plugging these into the equation F=1/(2piRC), and solving for R, R=1/(2*3.14*260*[0.01uF+0.01uF]), R=30.6K. 30.1K is a convenient value, so I went with that.

Finally, we have the power supply. I wanted to power this circuit from a 9 Volt battery, so I needed to provide a regulated 5 volts for the PIC. The op-amp can accept a much wider voltage range. To somewhat center the output in the middle of the op-amp's voltage rails (directly to the battery), I lifted the "ground" of the PIC up by approximately 1.4 volts by stacking it on top of 2 diodes, D2 and D3. D1 just provides protection from the application of reverse voltage. C10 and C11 serve to decouple the transients, and while C10 could be bigger, it should be no smaller, since it is needed to guarantee stability of the LM2940-5.0. That about does it, I have built my own RSWG.

I mentioned up above that the Tektronix Digital Phosphor Oscilloscopes were pretty good at emulating an analog scope. I recently had the opportunity to evaluate one of their scopes for a few hours and was able to get these screen captures. This is one of the nicest features- the ability to go to a computer viewable image. First, here's a picture of the sine wave as generated by the circuit (after low pass filtering). Here's a picture of the sine wave with the tooth modulation. A picture of two waves in quadrature with the tooth modulation. Finally, a [[Image:RSWG_DPORSWG.gif|picture of the sprocket] as rendered by the DPO.

Below I have links to a GIF file and a postscript file that give the full schematics of my RSWG.



Source Code for RSWG


Used Part Type  Designator           
==== ========== ==================== 
4    0.01uF     C1 C2 C5 C6          
3    0.1uF      C7 C8 C9             
4    100K       R27 R28 R29 R30      
4    10K        R25 R26 R31 R32      
1    10MHz      X1                   
1    16F84      U1                   
3    1N4007     D1 D2 D3             
10   2.55K      R15 R16 R17 R18 R19  
                R20 R21 R22 R23 R24  
2    22uF       C10 C11              
2    30.1K      R33 R34              
2    330pF      C12 C13              
2    33pF       C3 C4                
2    4066       U3 U5                
14   5.11K      R1 R10 R11 R12 R13   
                R14 R2 R3 R4 R5 R6   
                R7 R8 R9             
1    9V         B1                   
2    BNC        J1 J2                
1    LM2940-5.0 U7                   
1    LM324      U6
RSWG PCBImage.jpg
Here's the actual circuit, as I constructed it, but without the BNC jacks and 9V battery. I did not try to conserve space here, though obviously, I could have. In the upper left you can see the power supply circuitry. Directly below the power supply is the PIC and its crystal. The R/2R resistor ladders are dead center, and the op-amp and 4066 are on the right side.