Why do photo-transistors behave normally on their own, but not when multiplexed?

Grumpy_Mike:
What you are doing is not called multiplexing, is is known as a wired OR.

I did some research on the wired OR circuit. No, it's not a wired OR circuit. It is multiplexed. If it were a wired OR, the input would be shared. But it's switching so no two are sharing at one time. It's called ground multiplexing.

I still have no idea why it doesn't work the other way around. And now that I have it set up properly 3 of them are working, except the first of the 4 inexplicably get lower performance than the rest. It isn't the phototransistor itself, I've tried moving and replacing them. The results of some are changing to results of others still as well. And after some amount of time for no reason the second and third invert themselves. I have no idea how to explain this but it seems solutions are only temporary.

It's called ground multiplexing.

GROUND MULTIPLEXING FOR PHOTODIODE/PHOTOTRANSISTOR LINE SENSOR | wiguna149

There is no compassion with what you are doing and the circuits discussed in that link.

Can you post a proper schematic of what you have now. The other schematic only offered hints of what you are doing.

This might not be quite proper yet, but i spent a lot more time on this one and it's much more clear. Whatever you see, i mean exactly what is is in the link, it is exactly the same. The only difference is the resistor on the website is replaced by a potentiometer

The diagram on the website uses the symbol of an LED, but they are labeled as sensors

The more transistors you have in parallel the slower it will get due to the capacitance increasing.

What is the state of those Arduino pins when they are not selecting a transistor? Are they just an output with a zero? What they should be is to be set to an input, so they are in effect floating.

Oh, that capacitive thing explains it, i didnt think about that. I have those digital pins connected as outputs to write them high when im not reading a sensor, and low when i am, to provide a ground across the transistor while the voltage goes through the divider and into the analog input. If i set those to inputs, how do i tell it which transistor to be reading? wouldnt they just all be floating and the voltage would go over all the transistors at once?

Should i connect each phototransistor to an NPN transistor and multiplex those transistors instead? it would take a lot more components, but maybe it would work better because the phototransistors would be isolated from one another.

All the transistors BUT the one being addressed should have the pins set as inputs. The one being addressed should have that pin set as an output and set high.

ok, so i will be switching between input/outputHIGH rather than HIGH/LOW? I tried that out and at wasnt working, so i switched around the HIGH/LOW to be LOW when reading rather than high. It's working ok now. But it doesnt seem to be any different from before. Before i was getting values of about 200, 300, 400, 300 with all receiving the same amount of light. now im getting about 100, 250, 400, 250 with the same amount of light as before.

Is this what you meant by changing inputs and outputs around?

  pinMode(PT1, OUTPUT);
  digitalWrite(PT1, LOW);  
  val1 = 1023 - analogRead(0);    
  digitalWrite(PT1, HIGH);   
  delay(t);
  pinMode(PT1, INPUT);
  
  pinMode(PT2, OUTPUT);
  digitalWrite(PT2, LOW);  
  val2 = 1023 - analogRead(0);    
  digitalWrite(PT2, HIGH);   
  delay(t);   
  pinMode(PT2, INPUT);

  pinMode(PT3, OUTPUT);
  digitalWrite(PT3, LOW);
  val3 = 1023 - analogRead(0);
  digitalWrite(PT3, HIGH);
  delay(t);
  pinMode(PT3, INPUT);

  pinMode(PT4, OUTPUT); 
  digitalWrite(PT4, LOW);
  val4 = 1023 - analogRead(0);
  digitalWrite(PT4, HIGH); 
  delay(t);
  pinMode(PT4, INPUT);

No it is not what I mean.
Post the code and all the code of where you tried changing the high / low to high / input.

Well, if i write it HIGH, i dont have a path to ground through the phototransistor and it get ignored. I tried again writing it high and i get all values around 13 with no change from light change, so either it will be low or i need to change the circuit back to how i had it at first. I tried changing it from high/input, but since there is nothing changing it back to output, it only works the first time.

Heres the code that works, all others gave me nothing. I set the delay to 50ms and it seems to be giving the same numbers as if it were 1ms so thats good. But thats the same as before.

int val1, val2, val3, val4;

int PT1 = 2;
int PT2 = 3;
int PT3 = 4;
int PT4 = 5;
int t = 50;

void setup() 
{
  Serial.begin(9600);  
  pinMode(0, INPUT);
  pinMode(PT1, INPUT);
  pinMode(PT2, INPUT);
  pinMode(PT3, INPUT);
  pinMode(PT4, INPUT);
  
}

void loop() 
{
  pinMode(PT1, OUTPUT);  
  digitalWrite(PT1, LOW);  
  val1 = 1023 - analogRead(0);       
  delay(t);
  pinMode(PT1, INPUT);
  pinMode(PT2, OUTPUT);  
  digitalWrite(PT2, LOW);  
  val2 = 1023 - analogRead(0);      
  delay(t);   
  pinMode(PT2, INPUT);
  pinMode(PT3, OUTPUT);
  digitalWrite(PT3, LOW);
  val3 = 1023 - analogRead(0);
  delay(t);
  pinMode(PT3, INPUT);
  pinMode(PT4, OUTPUT);
  digitalWrite(PT4, LOW);
  val4 = 1023 - analogRead(0);
  delay(t);
  pinMode(PT4, INPUT);
   
  Serial.print(val1,DEC);
  Serial.print(",  ");  
  Serial.print(val2,DEC);
  Serial.print(",  ");  
  Serial.print(val3,DEC);
  Serial.print(",  ");  
  Serial.println(val4,DEC);
}

but since there is nothing changing it back to output, it only works the first time.

So add something to change it back.

You are skipping about between code and circuits. You need to be consistent if we are going to work anything out.

Heres the code that works,

With what circuit?

But thats the same as before.

Then I would suggest that the problem is one of capacitance and the rather unusual technique you are trying to use simply does not work.

Grumpy_Mike:
So add something to change it back.

well that's what i did but you told me that's not what you meant.

Grumpy_Mike:
With what circuit?

yes, if i change the circuit i will post a schematic. otherwise i will always be using the most recently uploaded.

Im not sure what im doing differently from what I've found online. The circuit is exactly the same so I've been assuming it's the code. With the current code and circuit, speed isn't changing the results all the way down to 1ms, but if the problem is the capacitance, how would i solve it? The problem right now being that the results are not symmetric with equal amounts of light and equal phototransistors.

Try repeating the analogue read twice like this:-

 digitalWrite(PT3, LOW);
  val3 = 1023 - analogRead(0);
  val3 = 1023 - analogRead(0);
  delay(t);
  pinMode(PT3, INPUT);

To give the input time to stablise. Or even try:-

 digitalWrite(PT3, LOW);
  val3 = 1023 - analogRead(0);
  delay(t);
  val3 = 1023 - analogRead(0);
  pinMode(PT3, INPUT);

Sorry for disappearing for a few days, I got distracted by other projects. I've decided not to multiplex 4 phototransistors, but instead only two to make the learning process easier. I've done a bit more research online. I found a new website with much better information on it than the one about ground multiplexing.

http://trains4africa.co.za/?p=571

This website also suggests reading the sensor twice, but also mentions it's unnecessary. The time it takes to read the analog pin is 110uS and about 15uS for the phototransistor to change. so 1ms should be a lot more than enough time, but the problem is when voltage builds up between the resistor and the phototransistor. That takes a lot of time aparently. The speed is limited buy the current flowing through. A 10k is around the best resistance for the most sensitivity for my application so that limits current to only 0.5ma. Although, It was reading one just fine and much faster. Im still not sure exactly what my issue is. But from this article i can tell that it doesnt matter whether the resistor is to ground or the phototransistor. So ive made a few circuit changes. This circuit does not work. I decided to switch the led power on with the phototranistors because that will save power and allow them to share an anode later. The first thing stopping this circuit from working, is i cant figure out how to work these P channel mossfets right. They are logic level, the irf9540 is what they are. It should be very simple but the simplest solutions are always the ones that elude me. Attached is my new circuit diagram. again, sorry i had to draw it, but i did my best.

The FET IRF91078 is not a logic level FET.

Grumpy_Mike:
The FET IRF91078 is not a logic level FET.

Heres the datasheet:
http://www.irf.com/product-info/datasheets/data/irf9540n.pdf

i chose this one specifically because Great Scott from the youtube uses these for led stuff, and he said they were logic level. His circuit diagrams show them operating on 5v. Ebay claims they are logic level as well. But a second look at the data sheet tells me i've been lied to. And the fact that it drops 1.6v explains why i was able to use an led without a resistor with it. This changes my whole damn project. >:( for now ill substitute a general purpose transistor

I've removed the mosfets, powered the leds separately, and now im back to square one. I have made some progress though. It turns out the interference has nothing to do with my circuit! With two completely independent phototransistors with their own digital out pin and their on analog in pin, i STILL get the same results! I have no idea where this interference could be coming from. It happens when i have two measured separately, no multiplexing at all. The only time i dont have interference is when im only using one of the arduinos analog inputs. Does this mean i could have a damaged arduino? That would explain why im the only one on the internet to be having such a hard time with this. I have some MUXers which turn 5 digital pins into 16 analog. I think ill try using that for my inputs and maybe that could help

This website also suggests reading the sensor twice, but also mentions it's unnecessary.

That web site looks neat enough but the guy has little idea what he is talking about. Diagrams labeled "Photo Transistor Amplifier circuits" that contain no amplifiers at all. The stupid comment about "This allows charge to build up at the analog pin" clearly nonsense. As well has his rejection of the read twice technique that has solved so many problems on this forum. It is almost as if he doesn't understand what is going on.

With two completely independent phototransistors with their own digital out pin and their on analog in pin, i STILL get the same results!

That is not what you said before.

I have no idea where this interference could be coming from.

It is not interference. I have told you several times now. Try the two reads technique on your circuit with two separate circuits.

i never said i tried two separate phototransistors at they same time, i said they work one at a time. And i have tried several times to use the read twice. It makes the "interference" worse. they both have an obvious influence on each others values. Even when they are completely isolated. It appears that i can have either non interfering values but with different sensitivity or i can have symmetric values with interference.

If reading twice makes the cross talk worse ( the correct name for the effect ) then you have something else wrong. What is hard to say, I can't think of anything that would make it worse, it implies that something is changing quite rapidly.
Try the read, delay and read again to see what the delay has to be to stabilise the reading. Do this on the two sensors on two diffrent inputs first.