Unstable analogue input

I have 4 x MCP9700 temperature sensors connected A2 to A5 on an Uno. I also have 6 LEDs pretending to be relays on digital channels 3 to 8. When I run the code below the result from the sensors goes up in proportion to the number of LEDs switched on. Even just switching one on effects the results.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(500);
analogReference(INTERNAL);
Serial.println("Hello");
for (int i = 0; i < 6; i++) pinMode(i + 3, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int input, volts;
float reading;

input = analogRead(A2); //(5000/1024)/10;
volts = analogRead(A3); //
(5000/1024)/10;
Serial.print("0, ");
Serial.print(input);
Serial.print(", ");
Serial.println(volts);

for (int i = 0; i < 6; i++) digitalWrite(i + 3, true);
delay(2000);

input = analogRead(A2); //(5000/1024)/10;
volts = analogRead(A3); //
(5000/1024)/10;
Serial.print("1, ");
Serial.print(input);
Serial.print(", ");
Serial.println(volts);

for (int i = 0; i < 6; i++) digitalWrite(i + 3, false);
delay(2000);
}

This is an image of the output with A2 sampling the sensor A3 connected to 0V .

I have tried using the 1.1V internal ref but the problem is not the digitisation. Even small changes in the voltage caused by turning even a single relay on seems to have a significant effect on the sensor output.

Any help would be appreciated.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Code with tags this time.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
  analogReference(INTERNAL);
  Serial.println("Hello");
  for (int i = 0; i < 6; i++) pinMode(i + 3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int input, volts;
  float reading;

  input = analogRead(A2);  //*(5000/1024)/10;
  volts = analogRead(A3);  //*(5000/1024)/10;
  Serial.print("0, ");
  Serial.print(input);
  Serial.print(", ");
  Serial.println(volts);

  for (int i = 0; i < 6; i++) digitalWrite(i + 3, true);
  delay(2000);

  input = analogRead(A2);  //*(5000/1024)/10;
  volts = analogRead(A3);  //*(5000/1024)/10;
  Serial.print("1, ");
  Serial.print(input);
  Serial.print(", ");
  Serial.println(volts);

  for (int i = 0; i < 6; i++) digitalWrite(i + 3, false);
  delay(2000);
}

Thank you

It's probably power supply noise, although I wouldn't expect "regular little" LEDs to make a difference.

You may need a better power supply, or a separate power supply or separate voltage regulator for the temperature sensors.

Or you can use some smoothing.

In temperature control it's common to use hysteresis... i.e. If you have a heater with a target of 70 degrees you turn it on at 69 (or 69.9) degrees and off at 71 (or 70.1) degrees. Or any time you have a relay you might want to do that, or add delay so the relay doesn't "chatter" on & off several times per second.

Good, but we meant you'd edit your first post and enclose that code, not making another post while keeping the first bad/unformatted code.
So far, please, to make it cleaner I think you better edit post #1 and remove the whole code from there, we'll get it from post #3.

It does not matter much, but I prefer the code with code tags in a new reply. That way the topic retains its chronology and comments about the original post make sense

Thanks for this.

I have tried to a separate lab type power supply and it has no effect. Its currently running off the 9V jack. I have also tried driving the seniors with higher voltages (up to 9V from the lab supply) but it make no difference.

Its not a ripple or noise effect. I get a stepped response. I have turn on up to 6 LEDs, and the reported temperature from the sensors (all of them) will go up in (roughly) even steps for each on I turn on, and then come down again as they are turned off. It is repeatable.

This is actually a test program to separate out the issue with minimum code, which it does. The main program is much larger and does have hysteresis is also range limiting etc.

I think a photo of your wire up is necessary. For some reason, your LED currents are crossfeeding into your Analog system, which should not be.

Did you remember to include current limiting resistors in series with EACH LED?

Breadboard EPCS 4 x Analogue MCP.pdf (346.5 KB)

This is the wiring as it stands. Yes, it does have resisters on the LEDs. I agree there must be a some kind of cross feed. I have seen some non-zero voltages on the 0V lines which does not make any sense at all to me.

Might make more sense to just answer: yes or no!

I would use a star ground configuration...
Your rather large ground current from the leds, combined with some small resistance in the ground line may cause a non zero ground voltage at the sensors. So give the leds a separate gnd wire and see if that helps.

Sounds like a plan, i will give it a go.

Or run the LEDs to 5V instead of GND, and invert your 'thinking' - an off LED is an ON relay. It's only a test.

Hi, thanks for this. I changed to a star format for the ground and made use of all three GRD points on the UNO board. This does make a very big difference. So much so that I can now convert to temperatures and add in the other sensors. I also made use of the "double read" trick that I read about in a different feed which also seems to help.

I am now getting this:

So some sensors are more effected than others. If I move the ground connections around the most effected sensors also changes.

The new test skit now looks like the below, but I am pretty sure I still have work to do on the electrical side. Do I need to add some caps? If so were do they go?

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
  analogReference(INTERNAL);
  Serial.println("Hello");
  for (int i = 0; i < 6; i++) pinMode(i + 3, OUTPUT);
}


float readit(int ch){
  int input;
  float reading;

  input = analogRead(ch); //dummy read
  input = analogRead(ch); //actual read
  input *=(1100/1024);    //convert to mV
  input -= 450;           //correct for zero offset
  reading = input/10.0;
  return reading;
}


void loop() {
  // put your main code here, to run repeatedly:
  
  Serial.print("0, ");
  Serial.print(readit(A2));
  Serial.print(", ");
  Serial.print(readit(A3));
  Serial.print(", ");
  Serial.print(readit(A4));
  Serial.print(", ");
  Serial.println(readit(A5));

  for (int i = 0; i < 6; i++) digitalWrite(i + 3, true);
  delay(2000);

  Serial.print("1, ");
  Serial.print(readit(A2));
  Serial.print(", ");
  Serial.print(readit(A3));
  Serial.print(", ");
  Serial.print(readit(A4));
  Serial.print(", ");
  Serial.println(readit(A5));

  for (int i = 0; i < 6; i++) digitalWrite(i + 3, false);
  delay(2000);
}

Thanks for this idea. I did have a look at it but it just inverts the problem. I am reading too high when the LEDs are off rather than on and since both states are controlled by the sensors its not the answer.

What unit is the timescale of your graph?
A 100nF capacitor from 5V to ground near each sensor might help.
Also: keep all wires to the srnsors (5V GND and signal) as short as possible and close together.
Are you in an industrial environment?

Please make a picture of your setup. The drops in value 5 are pretty big. The wobble in the others might just be the size of 1 step of the analog input (you can do some calcs. To check that ir you can print the raw values instead of the calculated values...

You are still getting two distinct voltages that depend on whether the LEDs are on or off, on three out of the four traces.

The 'value 2' trace looks good. Did you do something different on that channel?
That might be a clue as to what you need to do for the other three channels.

I'd connect the 4 sensors to one Arduino GND pin, and then connect the switches and LEDs to a differant arduino pin.