DAC Transistor

julianE

Senior Member
I'd like to use the digital to analog function of an 18M2 chip, is it safe to use an NPN transistor to buffer the output, the suggestion is for an op-amp but I was hoping to use less space. Also, is the voltage coming out from the DAC steady or will it vary depending on how busy the chip is. Thanks in advance.
 

hippy

Technical Support
Staff member
The DAC output should be steady but it would be worth making sure there are decoupling caps, reservoir caps, and outputs aren't switching large currents to try and keep the supply from collapsing or rippling. The Microchip datasheets may have details on accuracy and noise levels but should be good enough for most things I would have thought.

As for buffering - My understanding is that op-amps are recommended because those have high impedance inputs so they don't load the DAC output. I'm afraid my expertise isn't with analogue electronics so cannot really advise on using a transistor.
 

julianE

Senior Member
Thanks Hippy, I do have some FET transistors if the NPN is too much of a load. I don't need much of a resolution but it has to be very stable. I could always use an external DAC but like to keep things compact.
 

AllyCat

Senior Member
Hi,
I don't need much of a resolution but it has to be very stable.
An emitter follower is the "correct" (economical) configuration but will give a Vbe offset of about 600 mV (i.e. the first 600 mV of the DAC output above ground will be lost). A "source follower" will generally have a much larger (and variable) offset. If you want to use the full DAC output swing (if the DAC is stabilised from the FVR) then you need a PNP transistor. In terms of stability, the Vbe offset has temperature coefficient of -2 mV/degree C and the DAC output is proportional to the supply rail, or the FVR if that is being used. But neither of those variations are normally very significant when you only have a 5-bit DAC with 32 voltage levels !

An alternative solution is to use a low-pass filtered PWM signal, which gives 10 bit resolution (1024 levels). A simple R-C filter may be sufficient and the output impedance can be quite low. The output voltage is ratiometric (proportional) to the supply voltage (as is the default PICaxe DAC output) but I did write a code snippet to allow the program to compensate the level by using CALIBADC. Both DAC and PWM should work stably "in the background" regardless of how busy the chip is.

Cheers, Alan.
 

premelec

Senior Member
I vaguely recall an emitter follower with gain... i think there was a PNP that jacked up the NPN follower with low voltage gains of up to 5x or so. Does anyone know the name / configuration of that circuit? [the emitter of PNP went to V+ and base to NPN collector PNP collector to resistive divider to V- giving gain ratio - maybe... ]
 

AllyCat

Senior Member
Hi,

"Compound" PNP/NPN transistor pairs are (or were) quite common, for example in (bipolar) Integrated Circuits - the integrated PNPs generally have poorer performance (gain, current rating, etc.) so an NPN output is added as a helper. But the "emitter follower" configuration voltage gain is still just less than one.

I would simply have called the configuration you describe as a complementary, dc-coupled feedback amplifier. It basically uses the input transistor (base and emitter) as a differential amplifier (similar to an Op Amp). Relative to the collector output voltage, the base input has negative gain (inversion) whilst the emitter gives positive gain. But the emitter has lower impedance so that would be fed from the output (via resistors) with the base the high impedance input (increased further by the "bootstrapping" of its emitter). The gain is basically determined by the (attenuation) ratio of the feedback resistors and the Vbe offset will be increased correspondingly. It's similar to the three-terminal "variable" voltage regulators: where the output voltage divided by the ratio of two resistors is fed back to its (nominally) "ground" pin. But in that case the input-ground voltage is a tightly controlled reference voltage, not just the Vbe's default "forward silicon diode" voltage.

Cheers, Alan.
 

julianE

Senior Member
Alan and IWP, thanks for all the information. I set it up on a breadboard and all works well for my needs, I used an NPN transistor but i might try other transistors in time. Very simple circuit, a 10K resistor between the transistor Base and the DAC pin of 18M2 , transistor Collector to Vcc, 4.7K resistor from transistor Emitter to ground. Taking the output from the Emitter referencing to ground and all works well. I still have some adjusting to do but the system works. I'm using it for a remote volume control. There is a specialized part used in audio mixers etc. it's an LED and an LDR encapsulated in a small plastic blob. As the LED gets brighter the resistance on the LDR diminishes. It's popular because there is no static from dirty potentiometers.
 

hippy

Technical Support
Staff member
I'm using it for a remote volume control. There is a specialized part used in audio mixers etc. it's an LED and an LDR encapsulated in a small plastic blob. As the LED gets brighter the resistance on the LDR diminishes.
You may be able to vary the LED by PWM into an RC circuit which could give you much finer control. Filtering the ripple out is the challenge there but should be possible.
 

inglewoodpete

Senior Member
You may be able to vary the LED by PWM into an RC circuit which could give you much finer control. Filtering the ripple out is the challenge there but should be possible.
LDRs are relatively slow to respond to changes in light levels. If you use a high PWM frequency, the output from the LDR will be quite smooth meaning that you don't need to worry too much about PWM induced ripple.
 

julianE

Senior Member
You may be able to vary the LED by PWM into an RC circuit which could give you much finer control. Filtering the ripple out is the challenge there but should be possible.
My concern with PWM is that it might distort the audio signal. Odds are it will not be audible. Ever since I made an AB switcher that I control from my chair thanks to the help from the forum, I'm doubting a lot of audiophile claims. The volume control that I'm working now is an addition to an audio source switcher I made, my oldest amplifier from 1981 does not have IR remote, i don't need anything too fine just a few steps will be ample.
 

AllyCat

Senior Member
Hi,

The Vbe offset won't be a problem for driving a LED which has a higher voltage drop (and similar temperature coefficient). However, I'm surprised that (less than) 32 "steps" will be sufficient for an audio application where a "logarithmic" transfer characteristic is usually required.

Ah yes, many, many, years ago I built an optically-controlled "Quadraphonic" (when it was fashionable) Remote Control for my Hi-Fi system. Before the days of (satisfactory) LEDs or PWM (let alone IR remote controls), it used low power (40 mA) torch bulbs, so low-pass filtering wasn't an issue. The four potentiometers (L/R , F/B , LR/RR and Master Volume) were mounted in a transparent Parker Pen presentation case (nice for labelling on the inside) with an "umbilical cable" to a "tobacco tin" (not sourced by me) in which I assembled the power supply, lamps and LDRs, etc., to mount near to the power amplifiers. The LDRs were left on their long leads and plugged in to transistor sockets, so the system could be balanced and calibrated by simply moving the LDRs about, relative to the bulbs. ;)

I used quite "special" (i.e. expensive) "audio" LDRs because it was said that the regular ORP12s of the day would "distort" the sound quality (no idea if they actually did). But as IWP says, LDRs are inherently slow, so filtering out any PWM ripple shouldn't be a problem. However, LEDs are principally constant voltage devices so you might need a "T" filter, with two similar resistors across the top (input to output) and a capacitor from their centre point to ground.

Cheers, Alan.
 

julianE

Senior Member
Hi Alan,
Love the Quadraphonic story. All I did yesterday is measure the resistance of the LDR as I varied the voltage coming out of the transistor. Today I will hook it up to the HiFi and see how much the volume varies. I just need enough to lower the volume if I'm interrupted by a phone call or something.

The other use is to fine tune the volume when I'm doing an A/B test of different components. Years back A/B tests were not very rigorous, you would switch out components and then return to your listening position and rely on audio memory. The amusing part, I always heard a difference. Now, with instant switching between components and just sitting in my chair, the difference is extremely subtle and when there is a difference I can't honestly judge which is the better sounding. For the ultimate test, I compared the difference between an all valve (tube) preamplifier and a solid state one and the difference was minuscule, all the talk about tube warmth and better pacing and on and on is hyperbole. Sadly, before I made the A/B test I had zero doubt tubes made for a better sound.

My next step in my audio adventures and I'm sure I'll be asking for help is to have a remote controlled switching of components in the loudspeaker crossover circuit. My home made speakers have very good Danish made drivers, it took me months to tune the crossover to my liking, crossover makes a big difference to the sound. The crossover is outside the speaker box and i have terminals for each driver, I made it that way so I can try active crossovers in the future and so as to be able to change crossover components. I'm thinking of using relays to switch out different components, for instance changing the inductor between two different values.

To my ears there is little to be gained by spending money on amplifiers and other electronic components in the end it's the speakers that vary the most. Of course, the best place to spend money for music enjoyment is on opera and symphony tickets, unfortunately due to the pandemic the season was cut short. At least I got to see Ms. Anne-Sophie Mutter before the lock down, I was so looking forward to Andras Schiff but it had to be canceled.
 

premelec

Senior Member
Many years ago I used some LDRs in a compression amp design... I recall they weren't linear with applied voltage and put them in a bridge with resistors to overcome distortion... some LDRs have very fast response and I used them with a neon tube for choppers - others very slow 'gooey drifty' response especially at low light levels. Good luck with it all...
 

julianE

Senior Member
Thanks Jim=TX, The ones I'm using look exactly like that. I think i'll order a few more. I bought mine at least 5 years ago and that's a good price
 

inglewoodpete

Senior Member
But as IWP says, LDRs are inherently slow, so filtering out any PWM ripple shouldn't be a problem. However, LEDs are principally constant voltage devices so you might need a "T" filter, with two similar resistors across the top (input to output) and a capacitor from their centre point to ground.

Cheers, Alan.
I'd be driving the LED with PWM running at >40 kbits/second. Even without filtering, aliasing should not be a problem, especially when using an LDR.

Note that you may need to use a lookup table or fancy algorithm to get a nice logarithmic response curve (you're probably aware that our ears have a logarithmic response to sound levels).
 

julianE

Senior Member
I'd be driving the LED with PWM running at >40 kbits/second. Even without filtering, aliasing should not be a problem, especially when using an LDR.

Note that you may need to use a lookup table or fancy algorithm to get a nice logarithmic response curve (you're probably aware that our ears have a logarithmic response to sound levels).
Well I ran some tests with the unit and everything mentioned is true, LDR does have a very slow response, the volume takes a very noticeable time to change from led fully on to fully off. I'm aware of the logarithmic curve, by a rough estimate I have about 4 levels with the present setup, I can probably get 2 more levels if I increase the current to the LED. As far as my application it's perfectly adequate but I no longer worry about using PWM, the LDR response time will smooth out any variance. I'll give PWM a try tomorrow and a close listening test.

The part number of the unit I'm using is:

NSL-7053 Vactrol / Optocoupler

It's available on Amazon and other suppliers.

Thanks again for all the help.
 

julianE

Senior Member
Hello, I need more help. I have decided to go the PWM route for volume control for a different project. What I'd like to have is a volume control for each channel in a stereo setup. Idea being that I can vary the balance from distance to fine tune the stereo image. My question, Can I run 2 PWMs independently on a same chip, probably use a 20M2. I know there are restrictions as far as frequency but you can vary the duty cycle, it's all a little hazy to me, I'd appreciate a couple examples of code if it's not too much trouble. The other option would be to use 2 08M2 chips.
 

hippy

Technical Support
Staff member
.
My question, Can I run 2 PWMs independently on a same chip, probably use a 20M2
Yes. All M2's except the 08M2 have two or more PWM channels which can have their duties independently controlled.

Use PWMOUT to configure a PWM channel's frequency and initial duty, then use PWMDUTY to adjust the duty -

http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/pwmout
http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/pwmduty

Also check out the PWMOUT Wizard included in PE6.
 

lbenson

Senior Member
In addition to hardware PWM, the 20M2 has 4 PWM pins which may have separate settings: B.1, C.2, C.3, and C.5. Check out the PWM wizard.

Here's some code I snipped from a program to ramp and fade an LED. Hope I didn't leave anything crucial out:
Code:
#picaxe 20m2

symbol pPwm=C.2

symbol pwmFlag=bit17
symbol fadeFlag=bit18
symbol rampDirectionFlag=bit19 ' 0 is down, 1 is up
' variables through w9 (b19:b18) used
symbol wPWMDuty=w10 ' b21:b20
symbol wPWMDutyNow=w11 ' b23:b22

  wPWMDuty=1023 ' maximum
  pwmFlag = 1: fadeFlag = 1
  pwmout pwmdiv16, pPWM, 249, wPWMDutyNow ' 500=50%, 1000=100% ' 1kHz    else

do

  if pwmFlag = 1 and fadeFlag = 1 then
    if rampDirectionFlag = 1 then ' ramp up
      wPWMDutyNow = wPWMDutyNow + 10
      if wPWMDutyNow > wPWMDuty then ' reverse direction
        wPWMDutyNow = wPWMDutyNow min 10 - 10
        rampDirectionFlag = 0
      else
        pwmduty pPWM, wPWMDutyNow  
      endif
    else ' fade
      wPWMDutyNow = wPWMDutyNow min 10 - 10
      pwmduty pPWM, wPWMDutyNow
      if wPWMDutyNow = 0 then: rampDirectionFlag = 1: endif
    endif
  endif
  pause 20
loop
 
Last edited:

julianE

Senior Member
In addition to hardware PWM, the 20M2 has 4 PWM pins which may have separate settings: B.1, C.2, C.3, and C.5. Check out the PWM wizard.

Here's some code I snipped from a program to ramp and fade an LED. Hope I didn't leave anything crucial out:
Thank you gentlemen, I have used PWM and the wizard before but never to vary two PWM pins. Will be testing code tonight.
 

julianE

Senior Member
An alternative solution is to use a low-pass filtered PWM signal, which gives 10 bit resolution (1024 levels). A simple R-C filter may be sufficient and the output impedance can be quite low. The output voltage is ratiometric (proportional) to the supply voltage (as is the default PICaxe DAC output) but I did write a code snippet to allow the program to compensate the level by using CALIBADC. Both DAC and PWM should work stably "in the background" regardless of how busy the chip is.

Cheers, Alan.
I have done some more tests with the optocoupler and want to build a PWM controlled LED. I had a bag of these optocouplers so I went and matched them in pairs of close matching resistances. I also ran tests with just a potentiometer and the optocoupler and here is the range I found depending on voltage applied at the LED:

Voltage at the LED= 1.28V Output Resistance of the LDR= 300 Kohms
Voltage at the LED= 1.58V Output Resistance of the LDR= 400 ohms

It's a pretty impressive range and I can probably go a little more in each direction but for a volume control this seems ample.

I'm not sure how to get the most resolution out of the PWM where the range of control is only .3V.

I have setup an 18M2 and have gave the PWM a try with this parameters

pwmout B.3, 24, 50

Derived by using the Wizard using 40KHz PWM Frequency at 50% duty cycle.

At this point I have the LED of the optocoupler going to 1K resistor in series with a potentiometer going to the PWM output pin so I can vary the operating point of the LED.

I'm sure there are ways to get the full range of the LED/LDR combination and I'm sure there are people on the board with very elegant solutions.
 

AllyCat

Senior Member
Hi,
I'm not sure how to get the most resolution out of the PWM where the range of control is only .3V.
As I said earlier, LEDs are substantially "constant voltage" devices so should "always" be used with a series resistor. Choose that resistor so that the 5 volts maximum DAC/PWM voltage (via the emitter follower) gives you the maximum volume that you require. I'm not sure if you even need (or are using) an emitter follower now, but if you are:

A conventional NPN emitter follower with the LED/isolator grounded will basically "lose" two forward diode drops, a Vbe and the (larger) LED voltage (depends on the emitted colour). That (0.6 + 1.5 = 2.1 volts) amounts to almost half of the available "analogue" swing. One possibility is to use a PNP emitter follower to "steal" some of the current which flows directly through a resistor from the supply rail to the LED. Then you lose only Vopto - Vbe (= < 1v), and their temperature coefficients balance each other.

Then there's the issue of whether to put any low-pass filtering either before or after the emitter follower (which will behave differently), but if you don't have any "noise" issues then probably KISS is the best approach. ;)

Cheers, Alan.
 

julianE

Senior Member
As I said earlier, LEDs are substantially "constant voltage" devices so should "always" be used with a series resistor. Choose that resistor so that the 5 volts maximum DAC/PWM voltage (via the emitter follower) gives you the maximum volume that you require. I'm not sure if you even need (or are using) an emitter follower now, but if you are:
Hi Alan,

Thanks for taking the time to help. I have abandoned the NPN follower and am driving the LED through 1K resistor directly from the PWM pin. I also am experimenting with a 2K precision potentiometer between the 1K and the LED to see if varying the dropping resistor helps with the range.
Also, I dropped the supply voltage to 3.3 Volts thinking that would suit the LED range better.

I've been experimenting with lowering the PWM frequency to 2 KHz and am getting a wider usable range not really sure why. PWM is over my head, you were a tremendous help when I made my turntable speed checking strobe LED but I never truly understood it, I must do some more reading.

I'll keep experimenting but I'm starting to doubt my decision to use an optoisolator as a volume control.
 
Top