AXE to control volume level of LM386?

RogerTango

Senior Member
I was looking at the 386 to add to some of my projects, then the thought came to me... Is there a way to make an AXE control the volume level of the 386? Normally a 10k pot does the trick.

This isnt necessary, but curiosity has got the best of me on this one!

Andrew
 

BeanieBots

Moderator
There are several ways a PICAXE can control in a similar manner to a volume POT.
The most straight forward method would be to use a digitally controlled POT. There are several versions. Some are incremented/decremented by applying a pulse to inc/dec pins. Others can be set by I2C commands.

Another way is to use PWMout into an RC to give an analogue voltage. That voltage can then be used to control the gate of a FET. The on resistance of the FET is a function of its gate voltage, hence you have control of the resistance in the lower resistor of a potential divider.
 

boriz

Senior Member
Or PWM an LED that’s optically coupled to an LDR. So you can vary the LDR resistance using a PICAXE PWM output. The LDR could form part of the negative feedback loop for the OP-AMP, or be one leg of a divider. It’s completely electrically isolated and does not need either leg to be connected to ground or whatever.
 

westaust55

Moderator
You could take BeanieBots idea a stage further and even use a digital pot in conjucntion with a series cap 9say 10uF electro) across pins 1 and 8 for gain control.

open circuit gives a gain of ~20
1.2k Ohm gain = ~ 39
100 Ohms gain = ~124
0 ohms (cap only) gain = ~ 200


You would likely find that gain values above about 60 will result in distortion if input volume is reasonable.

I have modified my SPE03 Speech module by adding manual gain adjustment.
(mentioned in a past thread)


An alternative is an analogue switch 4066 IC and switch between some fixed settings.
 

manuka

Senior Member
The LED -> LDR approach mentioned is a breeze to rustle up,& one I use all the time- typically just shrouding the LED/LDR pair in dark heatshrink. It suits skinflint budgets & particularly appeals to lateral thinkers! Given how sensitive PICAXEs are to digital hash,the electrical isolation that comes with the technique is magnificent.

Note however that the LDR resistance range, although wide (~1k light - 1M dark) may not fall low enough for direct use in some applications. Stan
 

westaust55

Moderator
Be aware that you wil need to check around to find an LDR that presents only "about" 1K Ohm under light (bright) conditons.

My past findings and looking at specs for LDR's my local in electronics shops found that the light condition resistance can vary significantly within a particular make/model. For example from 1500 Ohms to 8500 Ohms. Another make is from 50,000 Ohms to 80,000 Ohms.
 

Jeremy Leach

Senior Member
A question I've got about the LED + LDR approach is: does the pulsing of the LED get translated to a pulsing of the LDR resistance? Or is the response of the LDR to changing light levels slow enough to filter this out? I always thought LDRs were relatively unresponsive ....I'm sure someone can put this in the right terminology !
 

BeanieBots

Moderator
Good point Jez. An LDR is sluggish when compared to something such as a photo transistor but they are still quite quick when compared to something "slow" such as the PWMout frequency of a PICAXE.
Only the datasheet will tell for sure. I have a commercial tacho which uses an LDR as the sensor. It works well at PICAXE PWMout frequencies so (without reading any datasheets) for my money, either the PWM drive or the LDR would need filtering and simple direct connection would not work.
 

boriz

Senior Member
It’s likely that your tacho has a large amount of AC amplification to offset the poor frequency response of the LDR. Though I would expect a tacho to use a phototransistor or photodiode, not an LDR for this very reason.

Using an LDR as an audio attenuator will mean that ripples on the resistance value will translate into small variations in volume. This can have a Vibrato effect at lower frequencies (<500Hz). But Anything higher than 4KHz (the lowest PWM frequency) is unlikely to cause any noticeable problem, though I have not tested this. Certainly if you use PWM frequencies beyond the audio range (>20KHz). The LED will take a little time to warm up / cool down / charge it&#8217;s junction capacitance, adding it&#8217;s own filtering to the system. I don&#8217;t have any LDR data to hand, but the ripple should be tiny at those frequencies.

Adding a small capacitor across the LDR would reduce the ripple significantly.
 
Last edited:

westaust55

Moderator
A question I've got about the LED + LDR approach is: does the pulsing of the LED get translated to a pulsing of the LDR resistance? Or is the response of the LDR to changing light levels slow enough to filter this out? I always thought LDRs were relatively unresponsive ....I'm sure someone can put this in the right terminology !
Funnily I quoted some data on LDR response time recently
http://www.picaxeforum.co.uk/showthread.php?t=10417&highlight=Decay+Time&page=15

But, here it is again:
Rise Time: (0~63%) 40mS- Decay Time: (100~37%) 10mS
 

hippy

Technical Support
Staff member
@ westaust55 : Thank's for that. I'd missed that previous information and had the same thoughts as others :)

It's one of those things which should be easy enough to test with a scope and the LDR wired as a potential divider. PWM pulses should be much faster than the attack and decay times so that should not be a problem.

The attack and decay times may even prove beneficial. Even though fast in themselves I've found it sounds much better to fade-in and fade-out audio than simply switch it on and off. Maybe the attack and decay times are too fast to be of real noticeable benefit but a C may allow that to be slugged as well as reduce ripple.
 

boriz

Senior Member
Roughly, and ignoring LED charge/discharge speeds…

20KHz has a 50uS period. So, worst case, if you have 2% duty, that’s a ‘low time’ of 49uS, approx 0.5% of the (100%-37%) 10mS decay time. So, if we take that decay slope as being straight (which it prolly won’t be, but it’s good enough for jazz), that’s 63%/200 = 0.3%.

That’s <0.3% resistance ripple at 20KHz with a duty of 2%, less for higher duty. I’m sure that will be completely unnoticeable, but there’s no reason why you can’t use higher frequencies. (0.03% @ 200KHz @ 2% duty)

Would someone check my figures. Maths isn’t my strong point.
 

hippy

Technical Support
Staff member
Checking figures ... assume 100% decay over 10mS and that's 0.01% per 1us, so 50uS is 0.5% so 0.3% would look to be right.

Here's where my brain gets mangled though ... it decays quicker than it attacks, so you turn the LED off, the LDR falls say X, you turn the LED back on and it rises less than X, turn it off and it falls again having not reached its previous level ... does it decay to zero ? What does it end up as ?
 
Last edited:

boriz

Senior Member
Perhaps it&#8217;s a percentage of final value?

IE: Light level increases 20%, it takes the LDR 40mS for it&#8217;s resistance to increase 63% of it&#8217;s final value. So it&#8217;s resistance_change_per_second gets slower and slower as it&#8217;s current resistance approaches it&#8217;s target resistance.

So that last 2% will take ages, but the first 2% will take no time. Hmmm. PWM might not work so well after all. This needs to be tested.
 
Last edited:

boriz

Senior Member
I’ve just scoped an LDR and a simple 22K divider resistor at 5v. Ripple quoted as peak-to-peak.

The mains frequency is 50Hz, so because the voltage crosses zero twice in one cycle, the bulbs brightness oscillates at 100Hz.

With a 20W 230v AC filament bulb at 1 foot, the resistance is 1.4K. The 100Hz ripple is about 100mV. When I shade the LDR from this direct light, it still gets the same frequency of ripple from ambient reflected light and the ripple increases to 500mV. Interesting.

With a LED driven at 50% 4KHz PWM I get about 125mV ripple. (Triangle wave)

With same led at 50% 20KHz I get about 20mV ripple.

I also had some high frequency noise caused by my mouse-mat. The mouse is wireless optical and batteryless. The mat contains coils and the mouse is powered inductively. This often effects my scope readings, but it can usually be ignored. I really like this mouse so it’s something I put up with.
 

BeanieBots

Moderator
With a 20W 230v AC filament bulb at 1 foot, the resistance is 1.4K. The 100Hz ripple is about 100mV. When I shade the LDR from this direct light, it still gets the same frequency of ripple from ambient reflected light and the ripple increases to 500mV. Interesting.
Not sure that test tells you very much. What is the thermal lag of the filament?
The ripple seen when shaded is more likely than not simple mains pickup. What precautions did you take?
The LDR resistance increases when shaded, hence, so does the ripple. ie, indicative of pickup as apposed to response to light ripple.
 
Last edited:

BeanieBots

Moderator
Can't say that I've ever heard of the term "Vactrol".
As the document clearly has a (R) next to the name, it would suggest that the term "Vactrol" is registered to the author (PerkinElmer) and is NOT an engineering word to describe an LDR+Resistor. A bit like saying a device that measures very high resistance by using high voltages is a "Megga". "Megga" is actually the name of the company that made the early ones and the name stuck. A bit like Hoover. No different to saying "I measured the voltage with a Fluke" or "I observed the waveform with a Techtronics". "I measured the light level with a Vactrol".
 

Jeremy Leach

Senior Member
Good point BB .... the word is used a bit loosely on various sites without any ref to the company. E.g http://narbotic.net/?p=130 as one example.

I haven't been able to find anything specifically talking about using PWM driving the LED in a LED + LDR combo. It's sensible (and indicated by Boriz's experiments) that the higher the freq the lower the resitance ripple amplitude. But the higher freq, the less resolution you've got on the PWM duty. So it seems to me it's a trade-off.

But I was also thinking how a cap across the LED would work: To my thinking it would then work in a different way ....the PWM duty would then set the av voltage across the LED, and because the LED light output is prop to voltage then this would set the av intensity. And with a big enough cap you should be able to virtually eliminate ripple (but it might not be very responsive to change in duty).

Yet another thought ... if the LDR was in a simple res divider setup, then could send the voltage output of the divider back into an ADC input, to allow the 'precise' LDR resistance to be established and set by code in a feedback loop. This might be useful.
 

boriz

Senior Member
“thinking how a cap across the LED would work”

I considered that. I think once the voltage across the cap has reached the turn-on voltage of the LED, only a small increase in duty will drive the LED to full brightness, significantly limiting the useful ‘duty range’. But then again, it’s not hard to test…
 

BeanieBots

Moderator
You're after an RC to 'smooth' the PWM.
The resistance of an LED is in the order of 10R, so C would need to be very large to have much effect. To be more effective, you would need:-
PWM --- R1 --- C(to Gnd) --- R2 --- LED

@Flooby, read post 2.
 
Last edited:

Jeremy Leach

Senior Member
Ok, be gentle with me BB ... but could this work ;) ? ..

My reasoning here is :
You choose R1 so that R1 and the LED in isolation would bring the LED just turning on. You then choose R2 so that R2 (and D1) in parallel with R1 will make the LED turn on to it's max but no more. C is just to smooth.

The Duty would then operate across the full range of LED just on to full on.

Would C still have to be pretty big?? I've not got into sums.

(P.S Code /code doesn't seem to work with ASCII art !)
 

Attachments

Last edited:

boriz

Senior Member
Using PWM is a good, efficient, digital solution to power control problems. I think smoothing it before it does any work is a step in the wrong direction. Better just to use higher frequencies I think.
 

boriz

Senior Member
At 50KHz, you still have 80 discrete duty steps, and only a tiny ripple. Same for 100KHz if you use the AXE at 8MHz.
 

Jeremy Leach

Senior Member
I agree Boriz, now I've tested my idea :D . Definitely need big cap! So not good idea.

But, my circuit (without cap) does allow the LED to be pre-biased, so the duty range can control a range of, say, HalfOn to FullOn, rather than Off to FullOn. Which might be useful.
 

Attachments

boriz

Senior Member
Ripple analysis of an LDR optically coupled to an LED driven by PWM.
This post is split into three because of the 4-images-per-post limit.

The parts:


The finished coupler:


LED has Cathode to 0v, Anode to resistor to PICAXE output. Scoped at LED Anode.
LDR is top half of divider with 22K lower half. Scoped across 22K.
The PICAXE 14M and the divider both supplied from same 4.92v supply.
Conducted at 3 different frequencies, each at two duty cycles (80% and 5%).
Ripple values are approximate peak to peak.
Calculated resistance of LDR is about 7K5 @ 80% and 88K5 @ 5%. The LED is an old and weak standard red LED. For lower resistances, use brighter LED.

Scan: 50uS / Div.
Top trace: DC 1V / Div. (LED)
Bottom trace: AC 20mV / Div. (Divider)

4KHz @ 80% Duty (35mV ripple):


4KHz @ 5% Duty (65mV ripple):
 
Last edited:

boriz

Senior Member
10KHz @ 80% Duty (15mV ripple):


10KHz @ 5% Duty (25mV ripple):


50KHz @ 80% Duty (5mV ripple):


50KHz @ 5% Duty (5mV ripple):
 

boriz

Senior Member
The last two again, but with faster scan for better detail. 5uS / Div:

50KHz @ 80% Duty:


50KHz @ 5% Duty:


Interestingly, the ripple is much larger for 5% than for 80% duty except for the 50KHz test. At that frequency, the duty seems to make little difference to the ripple amplitude.
 

manuka

Senior Member
Great pix! Like many light sensitive materilas,LDRs usually show responses relating to illumination colour of course, with red LEDs perhaps being the least energetic. Try UV LEDs ?

HINT: Perhaps use a white LED if "optical smoothing" is needed, as their phosphor has a slight afterglow. They're of course an energetic blue LED under this phosphor anyway. Stan
 
Last edited:
Top