# Linear resistance through optocoupler

I'm trying to control a 9v audio signal through a self made optocoupler(200k dark LDR and random blue LED, temporarily). They're all taped up together and dont seem to be getting any interference. The LDR is in parallel with a 10kohm resistor as I need a linear 0-10k resistance.

But I'm having a really hard time getting a nice linear fade in the resistance in the values I need. I tried to use logarithm and it helps a tad but I don't think I'm doing it right, and I seem to still be having other problems.

This is my equation 255-(log(256-i)*46)) I literally just added to it until I got about what I was going for. I assume it's not done correctly. I'm just looking for my 0-255 value to have a curve. It's still not curved enough according to my multimeter. But even more troubling is that it reports from 10k to IMMEDIATELY 5K when the LED PWM goes from 0 to 1 or 2. So I literally cannot get any more resolution out of that, and I need the 5k-10k to have at least close to as much resolution as 5k-0.

``````int j = -1;
void setup(){
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
Serial.begin(9600);
Serial.println("reset");
}

void loop(){
analogWrite(3,255-(log(256-i)*46));//optocoupler LED
analogWrite(5,255-(log(256-i)*46));//test LED
if(i!=j){//Only print if value changes
Serial.println(255-(log(256-i)*46));//print value
j=i;
}
delay(10);
}
``````

So basically I'm having nothing but trouble with this so I could really just any advice.

Thanks

But even more troubling is that it reports from 10k to IMMEDIATELY 5K when the LED PWM goes from 0 to 1 or 2.

That suggests to me that the LED is too bright. Try cutting down on the LED current by increasing the series resistor value.

I did say it was very non linear.

have you tried using PWM modulation, might give you more control, especially if the LED is enthusiatic!

Well he did say:-

But even more troubling is that it reports from 10k to IMMEDIATELY 5K when the LED PWM goes from 0 to 1 or 2.

oops missed that saw analog_write and assumed ...

will stand in corner for a while :blush:

I've gone all the way up to 10k for the LED limiter and I still get an LDR jump from 9.8k to 8.9k when I go from off to PWM 1. I would keep going up, but when I get to 255 the LDR only gets down to 1.7k. I can live with 300ohms at the lowest, but not 1.7k.

Maybe put a 20k in parallel with the LDR so that I'm not actually aiming for the lowest end of the PWM spectrum? I sure don't like lowering my resolution like that, but do I have any other choice?

The other thing you can try is an optical filter between the LED and the sensor. A few cellophane wrappers from candy might do.

Would it help much to just get a real optocoupler? Would this work?

It would work but not as well as some other designed. If you want to do an analogue transfer through an opto then look at the sort with one LED and two transistors. The second transistor is used for feedback to keep the thing more linear.

http://www.ssousa.com/appnote060.asp

oops missed that saw analog_write and assumed ...

And assumed what? The analogWrite() function IS used for PWM.

I'm already going to have so much going on with this project(5 or 6 rotary encoders, a bunch of 10-seg bar graphs, hopfully an LCD text screen) I would rather calibrate the optocoupler ahead of time than constantly check it. In that case would my link do or is it still substandard?

In that case would my link do

Hard to say but it will still be very non linear. The approach I suggested uses no CPU time so I don't see why you would need to constantly check it.

I figured the second LDR is for feeding back into the arduino so that I can adjust until I get the correct value. Is that not correct?

Also it's \$5 and I need at least 5 so I sure like the 25 cent one I linked better. I know I'm being pretty picky here, but are there any other choices? Or could I make due with the one I linked? If I logged the values and the curve and adjusted in programming would I at least get fairly consistently repeatable resulted out of it? That's really all that's important as long as I don't get those huge jumps like with my rigged up scheme.

the second LDR is for feeding back into the arduino so that I can adjust until I get the correct value. Is that not correct?

No it is for feedback to the op-amp driving the LED. Without it you tend to get the same sort of large steps you did with your home made stuff.

Is that the only model that does that or is there a way to find more because I can't hardly find anywhere that carries those and the places that do are expensive.

There are lots and lots of optos with two transistors and all the major suppliers have them, Mouser, Digikey, Farnell.

Ok, of course I looked over that page too quickly and didn't realize it was focuses more on the circuit than the IC. I get it know... thanks