## Friday, July 31, 2009

### Mini Function Generator: Part 3

<< [Part 1 2 3]My first prototype of the mini function generator is now officially done. It's a tight fit inside that Hammond 1593 project box from All Electronics, but it works.

I like this box a lot and will probably order several more. It has a removable front panel, a sliding battery door to fit AAs or 9V batteries, and is sizeable enough to fit my various Radio Shack prototype PCBs.

The BNC connectors come from one of the accursed Global Specialties 2002 function generators that I have been trying to get working. I gave up on that one and decided to part it out.

My first attempt to install a slide switch on the top failed miserably. I cannot cut square, straight, correctly sized holes without better equipment and more patience. Everything was wrong with this attempt (the picture doesn't even begin to convey how bad it turned out). To cover the mess I made, I used a larger power switch that covers up the original holes. Then I figured why not dress the thing up with a brass insert.

Clearance for the switch and the BNC connectors is pretty tight and I had to saw out some unused parts of the PCB to get it to fit. The PCB is mounted with plastic standoffs from Radio Shack. The plastic standoffs are glued to the case with Sure Hold Plastic Surgery. Note that this isn't my idea, but rather came from this Sparkfun Tutorial.

I swapped out the LM258N Op Amp for an LM1458CN which apparently has a faster slew rate as the trace is substantially cleaner than the one generated by the 258. No spikes, no ringing, etc.

So despite some challenges met with kludges, I'm fairly happy with the results. I haven't actually built anything in an enclosure in 15 years or more. And, sadly, while I'm no better at it now, at least I stuck my toe back in the water and I can jump in when it comes time to build an oscilloscope calibrator. But more on that another time.

<< [Part 1 2 3]

## Friday, July 24, 2009

### Mini Function Generator: Part 2

<< [Part 1 2 3] >>

The Mini Function Generator is coming along nicely. I successfully prototyped it on a pre-printed PCB from good ol' Radio Shack. Works like a charm. You can see the board on the desk with the "on" indicator lit, oscilloscope probes, and the o-scope trace on the left (the scope is sitting under the desk).

Here's the circuit diagram below. The actual circuit uses LM258N operational amplifiers. I won't pretend that I am some kind of electronic guru. As mentioned previously, the design comes from Circuit Suggest.

When an op amp uses a capacitor as a feedback, it integrates the input signal. IC1B is integrating the square wave output from IC1A. As every person on the street knows* the integral of a square wave is a triangle wave.

The IC1A op amp acts as a voltage comparator, and provides a high output when the input voltage exceeds the reference, and a low voltage otherwise. The triangle wave output is the input to the square wave generator. This waveform's voltage is above the threshold half the time, and below the threshold the other half. So IC1A generates a square wave.

The square wave is the input to the triangle wave generator. It's a bit of a weird chicken and egg sort of problem. From what I've read so far, noise is what initially gets the circuit going. The threshold is set by VR1 which therefore sets the symmetry of the wave form, too.

Anyway, here's the prototype circuit, front and back sides of the board (feel free to critique my iffy soldering skills)

[EDIT 2017] At the time I wrote this BatchPCB was the only option available and it was kind of expensive. Now we all have OSHPark and costs are quite low. This sort of circuit could be built on a 2 square inch board for $10 for a set of three. Meanwhile, the next step is to mount the prototype above into some kind of small project box with a switch, and battery holder. The board itself is 1-7/8" (45mm) square so it's still fairly compact. I can live with that for the moment... I'll post again when I get the enclosure finished and continue this series at a later time when I come up with an enhanced version on a custom PCB. * One of my old math professors used to say this all the time. Bastard. << [Part 1 2 3] >> ## Monday, July 20, 2009 ### TGIMBOEJ Arrived! A couple days ago I discovered a large, heavy, conspicuously labeled, rattly sort of package on my doorstep. A TGIMBOEJ has arrived for me chock full of cool electronic gadgets! Here's a teaser pic of the contents... More to come on what's in it, what is coming out, and what is going in! ## Saturday, July 18, 2009 ### Mini Function Generator: Part 1 [Part 1 2 3] >> Right off the bat I'll admit this Mini Function Generator electronics project isn't strictly a robotics thing. But, having a tiny square wave and triangle wave generator on hand could be useful for audio repair and for demonstrating oscilloscopes I put up for sale. My main goals for this project was to try out a short run, cheap PCB fabrication company, play around with Eagle circuit CAD and LTspice simulation, and make a simple, small, but useful function generator. I started with a single supply function generator op amp circuit diagram from Circuit Suggest and then drafted it up in Eagle and then wired it up on a breadboard. Originally I was going to include a switch to select multiple frequencies. Without going into the gory details, suffice it to say that, in the real world, getting a multiple frequency function generator to work is a bit trickier than LTspice led me to believe. So instead of trying to do everything at once, I decided on an ultra simple, one frequency generator. With a little trial and error and with help from my oscilloscope I got it set up where I want. The device will output a 2kHz square and triangle wave signal which is right in the middle of the audio frequency range. I can't use it for ESR testing but maybe I'll add that capability in the next version. It'll be really small. The circuit board is the size of a 9V battery. Because that is what's going to power it. A 9V battery holder will attach to the bottom of the PCB, while the top contains components, including a on/off switch, LED power indicator, waveform symmetry adjustment potentiometer, and... two BNC connectors. This is gonna be one de-luxe device! Since I want to use this thing with an oscilloscope, I thought it'd be really handy to just build in a pair of BNC connectors from the get go. It also has wire pads in case I want to hook up anything else. Like RCA connectors for injecting signals into audio gear, perhaps? So, I've submitted it to BatchPCB (which I discovered browsing Sparkfun) and looks like it'll cost$7 for the board. I put together a project (collection of components) at Mouser using their cool Project Manager tool. Cost for components is about $11 before shipping. So I am going to pull the trigger and order the board soon. I'll tell you more about the circuit in the next part. Hopefully I'll have it built and working by part 3... [Part 1 2 3] >> ### Edward Isaac Bot: Sketchbook 3 Here's some sketches on motor encoders. The basic idea is that, back in the day when MCUs weren't affordable or readily available (the Motorola 68HC11 was popular, I recall), one had to plan for a pretty high chip count. In this case, some type of counter that would track the number of slots in a disc that whizzed by an IR LED/Phototransistor pair. Remember I was still in college, with limited tools, etc. So building any kind of precise disc with slots in it was not going to happen (since I didn't know any MechE's that could get me into their fancy machine shops). But there are other ways to build encoders. You can print up an encoder wheel with a laserjet/inkjet printer and use an IR pair to read reflected light. Or you can print a transparency hoping that the printed spots are dark enough to block light. Or you can use one of these drain strainers or some other piece of hardware that has precisely predrilled holes. Of course now, too, you can use an MCU to count pulses, and you can by tiny SMD-based IR circuits like those I used on Pokey. And what about broken inkjet printers? The two I've disassembled have excellent transparent encoder discs and IR sensors. (Not to mention a slew of motors, gears, rubber pieces, etc.) ## Saturday, July 11, 2009 ### Edward Isaac Bot: Sketchbook 2 Here's more silliness from the dusty Edward Isaac Bot sketchbook archives. This is a sort of block diagram for the robot. Various audio functions like record/playback and sound generation, as well as basic motor control of head, arms, wheels. And the obligatory water pistol. Uh... right. Truth be told, a water pistol feature would come in handy right now to discipline one of our cats who keeps jumping up on the kitchen counter. And in fact I am seriously thinking about refitting Pokey for that sort of guard duty... but that is another story for another blog entry. So, yeah this was kind of crazy brainstorming. I mean, an air purifier? Really? ## Tuesday, July 7, 2009 ### SPICE: Part 5 << [Part 1 2 3 4 5] To continue with the SPICE experimentation, let's take a look at doing a better job of simulating a motor while using the SPICE subcircuit (.SUBCKT) feature. Here's a fantastic article from eCircuit Center on modeling not only the electronic but the mechanical properties of a DC motor in SPICE. I'll let you read the article yourself to get all the nuances. Subcircuits Overview Meanwhile, I'd like to take the motor definition and turn it into a subcircuit in SPICE. Subcircuits provide a way to encapsulate a lot of electronics detail, like that of an op amp, logic chips, or other integrated circuits, while presenting a simple interface to the main model in the form of external nodes, aka input/output pins. Our motor subcircuit will have two external nodes: A, and B. These represent the two terminals you'd see on this on any DC brush motor. This subcircuit will plug into our earlier circuit in place of the resistor RM that we were using to simulate a motor. Here's what the motor simulation circuit looks like. For a full explanation of the circuit see the article referenced above. This diagram was drafted in LTspice, a Windows-based implementaiton of SPICE that makes it extremely easy to draft and simulate circuits. I sort of spontaneously decided to give LTspice a try. A side note: if you work this out yourself, I ran into some glitches trying to build a current controlled voltage source in LTspice, the E_EMF above. If you use the drawing above, you get a different motor simulation than if you use the file provided on the DC motor simulation website. Before we go further, I want to talk a little about LTspice. Quick LTspice Overview Linear Technology's LTspice is a Windows program that combines circuit CAD and SPICE simulation. I've been hearing about it over on Electro-Tech-Online forums and with the recent series on SPICE using MacSpice I figured why not give this little gem a try. Having just acquired a$5 1.5GHz Pentium 4 tower with Windows XP at a nearby yard sale, there's no time like the present.

Not to make this into an LTspice user guide or extensive tutorial, here is a quick overview of the software. In short, the process for using the program is:
2. Define component models,
3. Wire components, and
4. Set up and run the simulation.
On the right side of the toolbar is the set of icons for adding components to the schematic.

Resistors, capacitors, inductors, ground, and diodes each have their own symbol.

Transistors and voltage supplies are added via the add component button which looks like an AND gate. From a dialog box, you select the type of component such as, LED, MOSFET, JFET, Comparator, Voltage Regulator, and more, even custom components (more on this later). In our case above, an NPN transistor, and voltage (supply).

Once you have components in place, you specify details about them, like passive component values, supply voltages, supply types (PULSE, SINE, etc), transistor models (e.g., 2N3055 or 2N2222), and so forth.

Wire the components with the pencil icon which features a handy vertical & horizontal alignment rules. You can easily move or drag components or entire sections of the circuit, rotate them, mirror them. It's really simple to quickly create your circuit diagram and lay it out just how you want.

Once you have your circuit defined you can run a simulation. In this case I chose to run an operating point simulation. Just select Edit simulation command from the Simulate menu. Select the DC op pnt tab and click Ok. Place the .OP directive onto the sheet. A similar procedure would be used for transient, AC or DC analysis.

Now select Run from the Simulate menu or click the run icon in the toolbar and you get very readable outputs (unlike the raw SPICE or MacSpice output). Now that we've briefly reviewed LTspice, let's get back to improving the motor simulation.

Subircuits

To encapsulate all the motor simulation circuitry above into a subcircuit, the circuit description is wrapped in SUBCKT / ENDS cards. The SUBCKT line specifies the name of the subcircuit and the number of externally accessible nodes ala:

.SUBCKT SUBNAM N1 [ N2 N3 ... ]

.ENDS

Subcircuits are referenced (called) by the pseudo-element identifier X followed by whatever name assigned to the instance of the subcircuit:

XYYYYYYY N1 [ N2 N3 ... ] SUBNAM

For our motor, we'll use the following sort of template.
.SUBCKT motor A B** Motor description cards go here*.ENDS
Node A will be the + voltage supply terminal, and B will be the - terminal.

Motor Model

Here's the SPICE deck for the motor from the website above, less the voltage supply and analysis/print directives and with the .SUBCKT / .ENDS cards added. I also changed the series resistance, RA, to 16Ω. While I was toying with all this, it seemed that 16Ω resulted in a motor with current requirements compatible with our original circuit, about 490mA, steady-state:
.SUBCKT MOTOR A B* MOTOR VOLTAGERA A 2 16LA 2 3 0.0015H_EMF 3 4 VSENSE2 0.05VSENSE1 4 B DC 0V** MOTOR TORQUE BASED ON INERTIA AND FRICTIONH_TORQ 6 0 VSENSE1 0.05LJ 6 7 0.00025RB 7 8 0.0001VSENSE2 8 0 DC 0V** MOTOR POSITIONFPOS 0 11 VSENSE2 1CPOS 11 0 1RPOS 11 0 1MEG.ENDS
As mentioned before, the subcircuit generated by LTspice looks a bit different:
.subckt motor a bLM a N002 0.0015LJ N001 N004 0.00025RM N002 N005 16RB N004 N006 0.0001E_EMF N005 N007 N006 0 0.05VSENSE1 N007 b 0VSENSE2 N006 0 0H_TORQ N001 0 VSENSE1 0.05FPOS 0 N003 VSENSE2 1CPOS N003 0 1RPOS N003 0 1m.ends motor
Including Files

SPICE also lets you include entire files via the .INCLUDE card. I used this when simulating the motor in MacSpice.

.INCLUDE filename

So let's put the motor subcircuit in a separate file, motor.cir, and we can include it into our earlier circuit description.

Custom Components in LTspice

Whereas in MacSpice one has to manually set up files with subcircuits to be included, LTspice front ends these SPICE features by enabling the user to create new components. First, you draft a new symbol, in this case, I drew a typical motor symbol (right), a circle with an M in it, and two pins (ports) shown as squares.

Next you set up a schematic in the same directory and with the same base name as the symbol (e.g., motor.asy and motor.asc). You associate the pins/ports on the drawing with those in the schematic and now you can just plug in this component by adding it out of your directory just like you'd add any other LTspice component. This is a pretty clever approach that gives LTspice a lot of flexibility and power.

Behind the scenes, LTspice is using subckt cards to implement this hierarchy. It puts the subcircuit description inline in your circuit diagram when you load a component. You can see how this is implemented by selecting View>SPICE Netlist.

Hooking up the Motor

In MacSpice you can include the subcircuit file, so after adding the appropriate file include card, we replace RM, connected to nodes 1 and 3, with a call to the motor subcircuit (XMOTOR 1 3 MOTOR). So here's what we end up with:
Motor driver** Sources*V9 1 0 9.0V5 2 0 5.0** Simulate motor as resistor*.INCLUDE Macintosh\ HD:Users:mes:Desktop:motor.cir*RM 1 3 18XMOTOR 1 3 MOTOR* Measure motor currentVM 3 4 0...
The complete MacSpice circuit file is here: ex5.cir

Meanwhile, using LTspice, I created the motor driver circuit, replacing RM with the motor custom component and then added a few extras. To simulate the 5V logic signal from the AVR microcontroller, I installed a voltage controlled switch, S1, which is repeatedly turned on and off by a 5V PULSE voltage supply.

When I simulated the circuit at first, as expected, I was seeing massive voltage spikes across Q1 because of the motor's inductance. The cure for this is to place a schottky diode across the motor itself, D1 above.

Note that when using a MOSFET driver instead of a BJT, a diode is present within the transistor package. However, I've seen advice to use an external diode just in case the current or voltage is too high for the internal diode to handle.

Finally, I added a pull up resistor, R1, to ensure that the transistor is off when the AVR MCU's output pin is in a high impedance state (during reboot, MCU off, etc.). The only way to turn on the motor is if MCU sets the pin low to sink current.

Simulation Results

In LTspice, to do operating point analysis, I bypassed the S1 switch and hardwired the base resistor to ground. Here are the results. The interesting results are in blue.
V(m2):     0.111133     voltageV(n005):     0.496462     voltageV(n004):     4.95934     voltageV(m1):     9     voltageV(n002):     4.04701     voltageV(n001):     5     voltageV(n003):     0     voltageIc(Q3):     -0.0495875     device_currentIb(Q3):     -0.00861067     device_currentIe(Q3):     0.0581982     device_currentIc(Q1):     0.555551     device_currentIb(Q1):     0.0495875     device_currentIe(Q1):     -0.605139     device_currentI(D1):     -3.17e-005     device_currentI(R1):     0.005     device_currentI(R3):     0.00861067     device_currentI(R2):     -0.0495875     device_currentI(S1):     0     device_currentI(Vsw):     0     device_currentI(V2):     -0.0631982     device_currentI(V1):     -0.555551     device_currentIx(1:A):     0.555519     subckt_currentIx(1:B):     -0.555519     subckt_current
Note that LTspice displays all currents without having to insert 0v supplies -- it does this behind the scenes. A nice feature!

Also, it usually displays voltages by referencing the device name, rather than the associated node names, making it a bit easier to understand the results.

Finally it displays the values as decimals rather than scientific notation so there's no mental calculations (e.g., let's see 0.1E-2 is, uhh... 1mA...)

So we're seeing about 556mA through the motor. When I used the subcircuit file given by the DC Motor simulation website, apparently the back emf behaves a bit differently than what I drew up in LTspice because current is quite a bit lower, about 217mA. So, time permitting I'll play around with different values of the components in the motor simulation. At some point it would be handy to be able to model any motor based on datasheet specs. But that's best saved for a whole 'nother article.

Next, I performed transient analysis in LTspice. The software makes it easy to graph the results; run the transient analysis, then click on areas of the circuit with your virtual 'probe' to read voltage/current.

MacSpice plots looked the same, in case you were wondering. Notice the voltage clamping effect of the schottky diode. The motor is still generating small voltage spikes at turn on and shut off, but they are orders of magnitude smaller than before. One concern I had was that motor current would spike (stall current) when turning on. Not so.

Instead, both spice simulations seem to show a gradual ramp-up of current. Is this because the Q1 transistor is going out of saturation when the motor first fires up, because IC is so high initially? (IB less than IC ÷ ß). Something else I can investigate later.

Motor Driver Next Steps

So I'm pretty happy with the simulations at this point. My next step is to gather real data on the motor, particularly its steady state current requirement with the fan attached. I think using the complex motor subcircuit isn't necessary to model a motor that simply turns on for a few seconds to blow out a candle.

So modeling the motor as a simple resistor would be good enough to let me modify the circuit above to drive the motor. Or, better yet, find a different driver transistor since the TIP3055 (2N3055) is total overkill and model it, instead. Whenever I get around to driving a 15A(!) motor I might put one of these to use again. :)

Another thought is to use a MOSFET transistor but an N-channel would need some driver hardware to get the gate voltage above the drain voltage. Another idea is to use a dedicated motor driver IC but I kind of want to stay old school with discrete components.

Of course, I can also use a different motor. It'd be simpler to use the robot's existing supply voltage and a 5-6V motor than having to tack on a 9V battery. It's more of a challenge to design around all the constraints of using parts you have on hand.

That's It

So, that's it for the SPICE series. Hope it's been helpful and/or interesting. Thanks for reading!

This isn't the last time I use or post about SPICE. I want to improve the IR LED flame detection circuit soon. SPICE will, I think, provide a good way to model what is happening and work on improving the amplifier part of this circuit.

Also, since I didn't get around to talking about converting between Eagle circuit drawings and SPICE decks, I'll post about that in a separate article at some point.

Special thanks to the electronics gurus at Electro-Tech-Online for some input and advice on BJT saturation.

<< [Part 1 2 3 4 5]

## Friday, July 3, 2009

### TGIMBOEJ

While I'm working on the last couple of installments of the SPICE series, I thought I'd take a little break. TGIMBOEJ stands for "The Great Intermet Migratory Box Of Electronic Junk." (image comes from their wiki). It's a neat idea where various boxes of gadgets, windings, whatzits, and thingamajigs get sent around between random people who sign up to receive it. They take stuff out, put cool stuff in, and send it onto the next vic--err, recipient.

I just ordered another pile of electronic parts from a neato guy on eBay and so I am thinking I am now extremely well stocked to supply neato-keen componentry should I ever receive this mystical container of goodies. Let's see what I've gotten thus far...Ermm

So far I've purchased a giant bag of about 300 ceramic caps, 300 regular caps, a bazillion zener diodes (18V and ... I don't remember what the others were), a boat ton of 2N4401 transistors, a pile of 1N4004 rectifier diodes.

Some of those giant yellow capacitors are really cool looking. I have no idea how/if I am going to ever use them, unless I make some really funky looking BEAM robots. I will save some of these for the box if/when it arrives. I don't need all those zeners to make power supplies, just a few... likewise the 4004's.

Oh, in the upper left are five ultra retro LED 7-segment displays like they used to use in those new fangled digital handheld calculators back in the late 70's! (Image from Wikipedia) I got these from Oatley Electronics in Australia. Hm, one or two of these may be a candidate for the box...

I've also got a box of parts from dismantling broken inkjet printers. Some great motors, encoder wheels and sensors, and gears and whatnot.

Would love to hear from you--what's your electronic miscellaneous box look like?

## Thursday, July 2, 2009

### Edward Isaac Bot sketchbook

While I'm working on SPICE modeling and now learning LTspice for the next article in the SPICE series, I thought I'd post up some sketches from "back in the day" when Paul and I were thinking about building a robot. Maybe they're worth a chuckle :) Or maybe there's something interesting in there, I don't know.

One of these days I suppose I will actually try to build this robot...