Jump to content
abecedarian

3.3 PWM to 0-5v

Recommended Posts

I've read about it, using PWM to 'emulate' a simple DAC using an RC filter on the PWM signal output, but that is limited to the output voltage from the PWM; i.e. if PWM swings 0-3.3, 3.3v is the maximum output voltage one can expect.

 

But I need 0-5v output. So, rather than turning to DAC over SPI or whatnot, is it possible to do this?

 

Maybe something like (PNP transistor):

5v
|
 \ (Emitter)
  \
   |-(Base)-- < PWM from microcontroller
  /
 / (Collector)
|
+----- > out to RC filter
|
GND

Share this post


Link to post
Share on other sites

Not as you have it sketched.

 

Your sketch will short your 5V supply to ground until the I/O or the processor fry from a connection to the 5V supply through a diode.

 

Background: The base-emmitter junction of a bipolar junction transistor acts like (and actually is) a diode. The base current sets a limit, based on the gain of the transistor, for the collector current. If the external circuit limits the current, then the transistor acts as a switch. If the external circuit doesn't limit the current, then the transistor does and it operates in linear mode. These rules are approximations. Good ones, but still only approximations.

 

The circuit you sketched will have the diode (base-emmitter junction) between your 5V supply and the I/O. Diodes do not limit current. (Ok, they do, but not like a resistor.) They act more like a constant voltage drop of about 0.6 to 0.7V when current flows. This means that you will be essentially applying 4.3V with no current limiting to the output of the microcontroller.

 

Also, you show hte collector tied to ground. This will both prevent the collector from changing voltage, defeating your goal of PWM, and also shorting the power supply to ground through the transistor when there is a base current.

 

I'll follow up in a few minutes with a circuit that may do it for you, but I would recommend doing some reading about transistors. If you don't have it, I strongly recommend Horowitz and Hill's _The_Art_of_Electronics_... It has been in print for about 30 years and is still about the best text out there for serious students that are not going to be electrical engineers, adn for those that are, as it is practical. It has the reputation of being a technicians text, but it is really a lab/research physicists text. Some things are a little out of date, but still quite relevant.

Share this post


Link to post
Share on other sites

So maybe more like (NPN transistor?):

5v
|
+------/\10K/\-+------------------> output
 \ (Emitter)    \--|100nF cap|----> GND
  \
   |-(Base)-- < PWM from microcontroller
  /
 / (Collector)
|
GND
I'd have to invert the PWM in the MCU so 100% = 0v out and 0%=5v?

 

Should probably mention that it only needs to supply ~ 5mA current.

Share this post


Link to post
Share on other sites

Attached is an image (MSPaint... Sorry. Not at a good machine right now) with two options. There are a ton of others.

 

First: NPN transistor. Resistor on base maybe 100K, collector maybe 100K or 20K, depending on what you are driving for a filter/current amp.

 

This will invert the PWM, and will not go full to ground. Gets to about 0.2V with a 2n3904 and 100K collector resistor. For full high output, leave microcontroller pin at low. For closest to low it will do, microcontroller pin is high.

 

Second: select your favorite rail to rail 5V capable (or powered from a bipoler supply) op-amp. Lots of options. This amplifies 3.3V to 5V. For lower speed applications.

 

If you put your low pass filter ahead of this, it will amplify your 0-3.3V filtered PWM to a 0-5V output.

 

Option 2 is my preference, in general. Simple RC low pass does the job for many applications.

 

If you need more info, just ask.

post-30450-0-13960900-1394247336_thumb.png

Share this post


Link to post
Share on other sites

For 5mA, you will likely need a buffer of some type. Full rail to rail an op-amp is best. If you can lose a little, an emmitter follower  will do.

 

If you can go with an op-amp, the filter on the new sketch is set using t=RC for the time constant. For example, for 1KHz  PWM rate set RC<1/1000sec, maybe at 1/500, so R=100K and C=0.022uF.

 

What are you driving? What freq of PWM? What other constraints? That will help figure out what is best and easiest

post-30450-0-60957200-1394247606_thumb.png

Share this post


Link to post
Share on other sites

@@enl Thanks for replying.

 

I'm working on an engine simulator, sort of. It's nothing that exactly simulates an engine but allows things to be connected to an engine controller and by either spinning potentiometers or using actual sensors, those signals get sampled by the MCU for logging or other purposes and are re-transmitted to the controller. It will also output digital camshaft and crankshaft 'trigger' signals based on RPM controlled by an as-of-yet determined mechanism, and those need to go from 0 to 5v as well... or at least close enough to either rail to register as a signal.

 

post-26656-0-60601100-1394248979_thumb.png

 

The screw terminals at the top would be the outputs to the engine controller. PWM would be coming from the inboard right header, pins 40-35 according to what the MSP430F5529LP has to offer as PWM outs. I'm also planning another board to mount on top of this with a display showing near real-time values.

 

It's more for probing what the controller will do when presented with certain sensor signals than anything else.

Share this post


Link to post
Share on other sites

The RTL schematic you've hashed-out above is a classic solution, although it's possible to get really small CMOS drivers & level shifters these days, and that may be a better option depending on your needs.  If I were in your place, I would probably first look at a CMOS driver.  But, RTL is a good thing to learn in any case.

 

Secondly, depending on your application it is sometimes better to use PFM vs PWM.  For PFM you will need a slightly more complicated analog circuit and the response will be slower, but the noise will be less, and if you want a learning experience it might be worth comparing the two.

Share this post


Link to post
Share on other sites

I've read about it, using PWM to 'emulate' a simple DAC using an RC filter on the PWM signal output, but that is limited to the output voltage from the PWM; i.e. if PWM swings 0-3.3, 3.3v is the maximum output voltage one can expect.

 

But I need 0-5v output. So, rather than turning to DAC over SPI or whatnot, is it possible to do this?

 

As noted by @@jpnorair, for this things I am using TI LVC logic family (up to 6.5V, 100mA). If you need only 1 / 2 / 3 gates, there is also sn74lvc1g / sn74lvc2g / sn74lvc3g.

Share this post


Link to post
Share on other sites

@jpnorair: yup. Simple, reliable, and buildable from pretty much any junk box. My first thought was to get the appropriate transistor based solution up since that is where it started.

 

Second sol'n was based on what I have in my junk box and would figure likely at hand for many, and what I pick for the task when I need moderate to low output impedance. Fast enough, reliable, and easy.

 

For the straight digital lines, I would definitely go with a level shifter IC, since it looks like several lines involved. For the analog, I'd probably stick to the op-amp, since due to the spec'd 5mA or so, if the frequency is low enough. An RC filter is going to have a pretty high output impedance or a substantial electrolytic cap.  At a minimum, if a max of about 4.2 to 4.4V is ok, an emitter follower after the RC filter to source current.

 

If the PWM freq is high enough that the filter isn't really needed for the application-- it isn't always, especially if what is being fed has a filter on the input or is a mechanical device-- then the level shifter IC is ideal as well.

Share this post


Link to post
Share on other sites

I think I might have been premature in asking the question. :( But will definitely keep this in mind.

 

Since this is intended to simulate sensors, and I have the pots on the board as well as the select jumpers and screw headers between the pots to allow wiring up actual sensors, it dawned on me that this is primarily for monitoring the sensors and generating faux hall-effect-like signals. Trying to duplicate the inputs to the outputs is redundant. So the analog stuff can just go straight to the output terminals along with the generated digital signals.

 

Thanks for the feedback all. :D

Share this post


Link to post
Share on other sites

I know this thread has shifted a little away from the original question, but attached are the 2 basic bipolar transistor circuits for switching loads that require a higher a higher voltage than the microprocessor can deliver. The one on the left is used if you are able to do your switching in the ground wire of the load, as would be typical for most cases where you are driving an LED, speaker etc. The example on the right is used where you have to switch the power line - eg common earthed lamps or relays in a car.  If your load consists of LED's be certain to put appropriate current limiting resistors in.  

 

The resistor values and transistor values would need to be calculated based on the necessary current, and the hFE of the transistors. If switching high current loads, you would probably need to use a darlington transistor pair as the driver, instead of the single transistor as shown, so as to keep base currents down to values acceptable for the MSP430.  Let me know if you want me to work some examples of how to choose appropriate values for resistors & transistors.

 

post-30597-0-71455800-1394315198_thumb.png

 

How they work

The example on the left is just the classic open-collector transistor amplifier. When the MSP430 pin goes high, current travels through the base-emitter junction of the transistor, allowing current to flow through the collector-emitter. 

 

The example on the right is a little more complicated. We can't just drive the load in the emitter line of an NPN transistor, because the emitter voltage is always less than the base voltage - so regardless of the supply voltage with a 3.6v supply to the MSP430, the transistor would only allow 3V at it's emitter. We also can't just use an open-collector PNP transistor, because the MSP430 wouldn't be able to turn it off.  The MSP430 output can only drive to it's supply voltage. So if the supply voltage was 5V, having 3.6V at the base of the transistor would still allow current to flow and the transistor would still be turned on. So we need the two transistors. The MSP430 can switch the NPN fully off. With no current flowing through the NPN's Collector-Emitter, no current also flows through the PNP's Emitter-Base junction, so the PNP turns fully off. When the MSP430 goes high, the NPN turns on, allowing current to flow through the PNP's Emitter-Base junction, also turning it on and allowing current to flow to the load.  

 

Another advantage of both of these circuits is that they don't require inverted logic. The load is turned on whenever the MSP430 pin is high.

Share this post


Link to post
Share on other sites

Yes, it drifted a bit but the information is valuable nonetheless.

If I, or anyone else, comes to need it this will be a good reference.

 

Thanks again, everyone. :D

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...