Op Amp PWM output

Discussion in 'The Projects Forum' started by mhtraylor, Jan 30, 2011.

  1. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    In the circuit below, I tried out an idea I had by sending the output of an OP AMP square wave generator to another OP AMP wired as a simple comparator. After reading on this site and elsewhere about 555 PWM circuits sent to a comparator, I decided to try to save on parts by using a quad OP AMP.

    The square wave generator was found in Mims "Engineer's Mini-Notebook" and the comparator basically comes from the LM324 datasheet. The diodes in series were added later, as I found the LED never fully turned off (I found that solution as well on this site in the 555 PWM section).

    The ultimate goal is to generate two variable frequency PWMs to drive two 6V 100mA (max) motors in as small a form factor as I can manage. However, this circuit does not appear to have enough power to operate a single motor.

    My questions are: how can I change this to drive a 6V motor? Should I go with a 555/comparator circuit instead? I'd really like to do the work myself, maybe somebody could just point me in the right direction? I'm just now beginning to understand Ohm's Law and the like, maybe someone could tell me how to calculate for this circuit.

    Attached Files:

  2. thatoneguy

    thatoneguy AAC Fanatic!

    Joined:
    Feb 19, 2009
    Messages:
    6,356
    Location:
    Midwest USA
    The transistor is kept on from the 330Ω pull up resistor on the op amp output, since it is open collector, it is required.

    A 555 solution may be overall smaller in component count, what are you using for the input to determine PWM Duty Cycle?

    Output would probably best as an N-Channel MOSFET switching the ground side on/off, rather than losing a bit through a transistor.

    Lastly, 9V, as in rectangular small battery? Their capacity is very low, consider using 4 AAs instead (6V).
  3. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    Thanks for the reply, thatoneguy. According to Mims' notes, the potentiometer controls the pulse width. I haven't acquired a MOSFET yet, I was just testing out this circuit with the parts I have handy, but I hope to eventually get one.

    The circuit will dim an LED, but I haven't been able to get a reading on the frequency, duty cycle, or pulse width with my mulitmeter. However, I did read 20mA from the 2n2222. I have been unable to find any further specifications for the Radio Shack 6V motor, so I do not know what it draws or what its optimum settings would be.

    I'm using the Radio Shack lab kit breadboard and it supplies up to 9V from AA batteries.

    If a 555 circuit is more productive and less parts, I can certainly go with that. I was just hoping to get by with just one IC if possible. Should I go with the 555 (or 556, since I want to generate two PWMs) and a better power supply?
  4. thatoneguy

    thatoneguy AAC Fanatic!

    Joined:
    Feb 19, 2009
    Messages:
    6,356
    Location:
    Midwest USA
    In the final application, I'd suggest 6v, you can get pretty close with ideas on the proto board though.

    Search the forums for PWM, there are a lot of ways of producing it, it's mostly an issue of what parts you have on hand. By looking through the various circuits posted, you'll get an idea of the many ways speed control can be handled.

    In the end application, will a sensor of some type be controlling the motor speed, or is this for a "set to slow speed and leave it" application?
    mhtraylor likes this.
  5. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    I'm shooting for having a photoresistor in place of the potentiometer as the sensor. The final project should be a vehicle that finds and approaches light.
  6. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    Gee, you must have made some mistakes when you generated the schematic from your circuit, as I don't see how you possibly could have obtained a PWM output.

    You have the noninverting (+) input of the right LM324 connected to +9v. The LM324 has a common-mode input that goes to the negative rail, but it can't "see" within a volt or so of the positive rail. So, your output would never change. The noninverting input should have been biased at a point somewhere between +9v and ground.

    The LM324 is an opamp, not a comparator. Comparators are not internally compensated which makes them fast at switching, and frequently have open-collector outputs that require pull-up resistors for a source of current, as they can only sink current. However, the LM324 does not have an open-collector output; it can both source and sink current.

    The 330 Ohm pull-up resistor sources too much current for the LM324 output to sink, besides gobbling up your battery in a big hurry. There should have been a resistor to limit the base current of the 2N2222 transistor. If you were intending to use the transistor as a voltage follower, you succeeded. However, you might have wanted to use it as a saturated switch instead, which means that you would need to move the LED to between the 470 Ohm resistor and the collector of the 2N2222.
  7. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    SgtWookie, thanks for the help. It was my first attempt to generate a schematic, but the error is also in the circuit itself: when combining the two circuits I forgot a potentiometer between ground, noninverting input, +9V. I tested the square generator by itself, it produces expected output, so yes my problems lie after that, and likely in the whole design.

    I think, in addition to my limited beginner knowledge, I combined too many ideas from different circuits without understanding the theory behind the changes.
  8. thatoneguy

    thatoneguy AAC Fanatic!

    Joined:
    Feb 19, 2009
    Messages:
    6,356
    Location:
    Midwest USA
    My bad for the post above. I had the LM339 in my head when replying. :rolleyes:
  9. Bill_Marsden

    Bill_Marsden Moderator Staff Member

    Joined:
    Mar 24, 2008
    Messages:
    19,247
    Location:
    Dallas, TX (GMT-5 w/ DST)
    mhtraylor likes this.
  10. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    Take a look at the attached schematic and simulation.

    The left-hand opamp U1a is a square wave generator at the output, which both sets the trip level on the noninverting input via the R1/R2/R3 voltage divider network (see the green "noninv" trace on the right), and also charges/discharges C1 via R4 (see the yellow "c1" trace on the right). As shown, the output frequency is roughly 150 Hz. This is fast enough so that you shouldn't see a flicker if you are using the circuit to provide PWM for an LED.

    The signal present on C1 is fed to U1b's noninverting input, and VR1 provides a reference level to the inverting input (see the grey "n003" trace). I've used R5 and R6 to keep most of the pot's range useful; without them there would be rather large "dead bands" on either end of the pot swing; either 0% PWM or 100% PWM and the pot would not seem to have any effect.

    Notice that when the signal on C1 goes higher than the reference level on the inverting input (n003), the output goes high; if lower, the output goes low.

    From this point, you could connect a resistor to PWMout to drive the base of an NPN transistor who's emitter is connected to ground, and have an LED connected to the collector, getting its' current supplied via a limiting resistor.

    You need to calculate the base resistor to make certain that it is saturated for best results. For most transistors, use Ib=Ic/10 - that means, for every 10mA of collector current that you need, use 1mA of base current.

    The formula for calculating a base resistor is:
    Rbase = (Vin - Vbe) / (Ic/10)
    where:
    Vin = the voltage that will be on the other side of the resistor, referenced to the transistors' emitter.
    Vbe = the voltage on the base referenced to the transistors' emitter when it is ON. For up to about 1/3 of the transistors' rated collector current, use 0.7v for this number.
    Ic = the desired collector current.

    You're driving an LED; if your desired maximum current is 20mA, then you will need 2mA base current.

    Since the PWM output goes to about 7.5v when high, and Vbe will be around 0.7v, then Vin = 7.5v - 0.7v = 6.8v
    6.8v / 2mA = 3400 Ohms. This is not a standard E12 or E24 value of resistance.

    A decade table of standard resistance values is here:
    http://www.logwell.com/tech/components/resistor_values.html
    Bookmark that page so that you don't lose it.
    Refer to the (yellow) E12 and (green) E24 columns. 330 is shown, the next value is 360 for E24 values, 390 for E12 values.

    So, 3.3k is the closest value to your requirement.

    Note that there are also E48, E96 and E192 values, but those are only used when high precision is necessary; they are much too expensive for general use. Most of the time, if you're within 10%, you're OK.

    Attached Files:

    Last edited: Jan 30, 2011
    mhtraylor likes this.
  11. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    Bill, I have read the LED flasher notes and have gained a lot information from them. I did build a 555 PWM similar to one of your designs in your Cookbook, found at DPRG.org (which I've seen posted here a few times) using diodes. Hopefully, as I learn a bit more about the theory of these designs I'll be able to choose which kind (555s or op amps) fits my application. I was trying to get by with only one IC to generate two signals, hence the quad op amp.

    SgtWookie, I've only had time to build the square wave portion of your circuit with the exact components. I did test it with my multimeter, it outputs a frequency of 134.5 Hz (I'm guessing the difference here between 150 Hz is probably due to the +/-20% tolerance of my capacitor, right?), and a duty cycle of about 57%. I'm about to build the rest of the circuit and do the numbers for the transistor. Will this circuit be able to power a motor, or will I need that MOSFET instead of the 2N2222? Thanks for the tips, I really need to learn this process and the proper equations as I want to figure this stuff out for myself.

    Thanks again, guys.
  12. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    The 150Hz I mentioned is approximate; your mileage may vary. In the simulation, all of the components are "ideal"; it's a starting place to get an idea of what a real-world circuit might act like. Your 134.5Hz is 89.7% of 150Hz, and close enough to what the simulation shows.

    You can increase the frequency by decreasing R4. 30k would give you around 240Hz. Conversely, you can increase R4 to slow things down if you'd like.

    The duty cycle of the output of U1a will have no impact on the duty cycle of the output of U1b; as only the signal on C1 is used past U1a. If you wanted it to be closer to a 50% duty cycle for some reason, you could increase R2 to 62k, but you would also need a corresponding increase in R5, otherwise you might not be able to get the full PWM range from U1b.

    OK, good - run through the math a few times with different scenarios.

    Keep in mind that opamps generally have a somewhat limited output current sink/source capability; at 20mA many have hit their limit. It's possible to use a transistor or two on the opamps' output as an emitter follower to increase the sink/source capacity, but usually you're better off to go to a more capable opamp.

    It depends upon your motors' required current. A 2N2222 is good for up to around 500mA collector current, but that means you need to supply 50mA base current. MOSFETs have gates that are charged and discharged rather like capacitors. Once the MOSFET gate is charged or discharged, it requires no further current to maintain its' state.

    Basically, transistors are controlled by current, and MOSFETs are controlled by voltage.
    Last edited: Jan 30, 2011
  13. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    Okay, I did the math. My motor draws about 84mA at 6v, which would mean about 8.4mA base current for the transistor and 6.8v / 8.4mA = .81 so the closest resistor I have would be 1k. Since the motor is rated at 6v 120mA max, I should recalculate the components of this circuit to run from 6v, correct? How do I go about that?
  14. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    Rbase = (Vin - Vbe) / (Ic/10)
    The highest the output of the LM324 will go with a 2K Ohm load is Vcc-1.5v; so 6v-1.5v=4.5v
    Rbase = (4.5v - 0.7v) / (120mA/10)
    Rbase ~= 317 Ohms

    That's too much of a load for the opamp. You'll either need to use a MOSFET, a Darlington, or add a transistor driver for the base current.

    Adding an NPN as a voltage follower will amplify the opamps' output current. It's collector resistor would need to be about...
    Rcoll = (6v - (0.7v*2))/(12mA) = 383 Ohms. 390 is the closest standard value.
    Not using it as a saturated switch, so it's base current could be around
    Rbase = (4.5v - (0.7v*2))/(12mA/30) ~=7.7k. 7.5k is the closest standard value.

    Adding a Schottky diode across the motor protects the switching transistor from getting blasted with the motors' reverse EMF.

    See the attached.

    Attached Files:

    Last edited: Feb 1, 2011
  15. Bill_Marsden

    Bill_Marsden Moderator Staff Member

    Joined:
    Mar 24, 2008
    Messages:
    19,247
    Location:
    Dallas, TX (GMT-5 w/ DST)
  16. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    I'll have to order a Schottky diode, my local Radio Shack doesn't have any. I have the rest of the parts, including a couple of MOSFETs. Thanks for the help.

    I find PWM interesting, so I'm sure I'll be asking some general theory questions later, such as: with a 555 (and in our current circuit), why the need for a comparator? To adjust the pulse width? I ran sim of an astable 555 in LTSpice, and it outputs 0v to 6v pulsed square wave. I'm just asking because in my research I've found a lot of PWM generators, some using three op amps (two to generate a triangle wave, one as a comparator), hex inverters, etc. Not only am I looking for the best one for my application, but knowledge of why so many options.
  17. Bill_Marsden

    Bill_Marsden Moderator Staff Member

    Joined:
    Mar 24, 2008
    Messages:
    19,247
    Location:
    Dallas, TX (GMT-5 w/ DST)
    Actually a comparator isn't needed for PWM. It does seem to be the cleanest circuit I've come across for it though. It was one I came up with several years ago, but like most simple circuits I'm sure it was invented independently elsewhere long ago. This is something that keeps happening to me, I have a Eureka moment, do the experiments, then find out it is old hat.

    My cookbook has several other ways to accomplish the same thing using diodes and 555s. Unfortunately the simplicity suffers (read that as predictability, since the math models get more complex). You might say I collect circuits. Indeed, a long term project for this site would be a "cookbook" of circuits. If the albums were tweaked a bit by the writers of this forum software (VBullitien) so entries can be organized (which is currently not possible) I think it would be eminently practical.

    PWM is one of those circuits that is only finding more uses as time progresses. Digital is taking over the applications of analog one by one, and PWM is the way it is happening. There was a time when vacuum tubes (AKA valves) were king. Slowly, one by one, all the devices using tubes, CRTs, LASERs, etc have been replaced with solid state counter parts that do a better job. Same thing is happening with digital IMO.
    mhtraylor likes this.
  18. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    SgtWookie, so I've been studying up on how to understand the theory behind your design, and why mine did not work. I'm guessing that I used split-power supply designs for a single power op amp as part of the problem. It looks like you used (in your original schematic) R1 and R2 to create a virtual ground, correct?

    I'm just trying to learn all I can about op amps, signal generation, and circuit design in general.
  19. mhtraylor

    mhtraylor Thread Starter Member

    Joined:
    Jul 21, 2009
    Messages:
    11
    Location:
    Mississippi, USA
    Bill, I'm trying to understand the ideas behind all these PWM circuits, mainly because it interests me. I don't necessarily have any practical applications, besides driving some Braitenburg vehicles. But I would like to know why, if a simple 555 astable circuit delivers pulses, do we need diodes, comparators, etc?

    The only reason I chose op amps for my application was to ensure a low part count; every circuit I found used 555s and comparators. I was trying to get by with just a quad op amp (to deliver two PWMs).
  20. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    Is the schematic you posted to start the thread your actual circuit, and it didn't work? I'm somewhat confused now, as I thought you said it was working, but the schematic you posted could not have produced a PWM output.

    Actually, R1, R2 and R3 are a summing network.
    When the output of the opamp is low (near 0v), R1 and R3 are effectively in parallel. So, you have 47k (R3) on the high side, and 47k/2 = 23.5k on the low side to ground; the voltage at node Noninv will be 1/3 of Vcc.

    When the output of the opamp is high, R2 and R3 are effectively placed in parallel (not quite, because the output of this particular opamp won't reach to the positive rail - but let's just pretend it does for the moment to keep thing simple).
    So, the formula gets flipped; 47k/2 = 23.5k on the high side, and 47k on the low side, so that the voltage at node Noninv is 2/3 Vcc.

    With real parts, the output of the opamp won't reach to Vcc, so the high threshold will be about 0.75v shy of 2/3 Vcc.

    Keep at it. :) Have you been reading in our E-book? It's available at the top of every page on this website.

    If you want to go more in-depth from a technicians' aspect, read through the Navy NEET texts, available for free, download the .pdf files here:
    http://www.phy.davidson.edu/instrumentation/NEETS.htm
Similar Threads
Forum Title Date
The Projects Forum Mapping 4017 output to LEDs Tuesday at 12:13 PM
The Projects Forum Power Supply resister died out, half output after change Oct 14, 2014
The Projects Forum Is it possible to modify this voltage booster to regulate input voltage instead of output voltage? Oct 14, 2014
The Projects Forum MAX1978 Peltier Controller - Unstable Output Sep 30, 2014
The Projects Forum Inverting amplifier noise output Sep 15, 2014

Share This Page