Pages: [1] 2   Go Down
Author Topic: Linear resistance through optocoupler  (Read 1729 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


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

void loop(){
  //analog input 0 connected to pot just for testing
  int i = map(analogRead(0),0,1023,5,255);
  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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

there are only 10 types of people
them that understands binary
and them that doesn't

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well he did say:-
Quote
But even more troubling is that it reports from 10k to IMMEDIATELY 5K when the LED PWM goes from 0 to 1 or 2.
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oops missed that
saw analog_write and assumed ...

will stand in corner for a while smiley-red
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
oops missed that
saw analog_write and assumed ...
And assumed what? The analogWrite() function IS used for PWM.
Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: