Get reliable results from multiple devices read by analogRead

Hi, I'm working on an IR phototransistor and some other devices that need to be read by analogRead from an Arduino Nano.
I started a thread previously for some problem with the phototransistor that you can see here , anyway the phototransistor is now connected to ground with a 680k ohm resistance to get some decent results.
If I read it alone, when nothing else is being read, the readings are correct (from 0 to 900~), but if I read some other device connected to the Arduino on the same cycle, it reads wrong values.
I searched a bit and even in this forum I find some threads that address this behavior. I read that the ADC in the Arduino is one multiplexed for all analog inputs and that the previous reading can affect the next one.
The only solutions were
a) do multiple readings and keep the last one (I need 3 readings to get back to the correct one)
b) wait some time before reading

Is there a solution beside these? I read something about a capacitor but no solution were given in the threads

EDIT: Here are some more explanations, the code and some attachments.

//Pin Names
const int fireSelectorPin = A0;

int fireMode = 0; //Safe = 0, Semi = 1, Auto = 2, Burst = 3
int readFireMode = 0;
String fireModesNames[] = {"Safe", "Semi", "Auto", "Burst"};
int anRead = 0;

void setup() {
  Serial.begin(9600);
  //Pin Modes
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(fireSelectorPin, INPUT_PULLUP);
}

void fireSelection() {
  readFireMode = analogRead(fireSelectorPin); //reads the voltage
}

void loop() {
  fireSelection();

  anRead = analogRead(A3);
  anRead = analogRead(A3);
  anRead = analogRead(A3);
  Serial.println(anRead);

 /* if (anRead < 20) {
    Serial.print("<<<<<<");
    Serial.println(anRead);
  } else {
    //Serial.print(">>>>>>");
  }
  //Serial.println(anRead);*/
}

The problem can be replicated like so.

  1. If only one analog reading is done per cycle, it all works well (1st image, anRead reading goes from 0 to ~70).
  2. If I read something else prior A3, the reading is wrong (2nd image, anRead goes from ~240 to ~280).
  3. If I do multiple reading of A3 after something else got read, it came back to the correct values (3rd image).

Start by posting a schematic of your circuit and the code that you are using

You're right, I'll edit the post with more explanations

ariesbreath:
You're right, I'll edit the post with more explanations

But a schematic is essential for us to see what is going on.

Posting code as screen dumps is useless because we can run the code ourselves.

With a 680k pull-up, you are going to require some settling time for the ADC given the recommended input impedance is around 10k.

A capacitor - try a 100 nF - is one approach but you realise it will slow the response of the phototransistor. Just realise that you will not be able to respond to fast changes however you do it. The other option is to use an op-amp in a current-to-voltage buffering mode.

Here is a schematic. I hope it is readable.
The phototransistor's Collector and the IR LED anode are connected to 5v.
The phototransistor's Emitter is directly connected to an analog pin and to Ground through the 680K ohm resistance. Also the IR LED catode is connected to Ground.

Why have you drawn a photo resistor when you say it is a photo transistor or is it a photo diode.

Anyway the resistor is way too high to be read directly, you need either a delay between selecting the channel with the multiplexer and reading it or have an emitter follower or Darlington arrangement before the analogue input.

IR LEDs can normally take a lot more continuous current than a 220R will allow, so you could probably drop that to 90R to get more light.

Grumpy_Mike:
Why have you drawn a photo resistor when you say it is a photo transistor or is it a photo diode.

Anyway the resistor is way too high to be read directly, you need either a delay between selecting the channel with the multiplexer and reading it or have an emitter follower or Darlington arrangement before the analogue input.

IR LEDs can normally take a lot more continuous current than a 220R will allow, so you could probably drop that to 90R to get more light.

I drawn a photoresistor because the Fritzing software didn't have (or I didn't find) a phototransistor.
Some days ago I actually forgot to put a resistor at all on the IR LED and it was working. I soldered a 220 ohm as I noticed, but I think I'll go lower to 90 then

I have connected one analog pin to ground, say A0. I then put the analog signal to be read on A1. I do a read of A0 to bleed off the past 'charge' then do a read on A1.

It may be worth your while to do a read on the grounded pin. store what value was read on the grounded pin. Read the analog signal and subtract the grounded pin reading from the read signal. Worked well for me.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.