LEDs distort Analog read

REALLY simple app.
A TMP 36 sensor, pretty much right out of the Arduino starter book.
Works fine - middle pin to A0 and it reads about 150, which /1023 x 5 V = 0.7 v which is right at room temperature (20C, 68 F).
PROBLEM: I hook up an LED to Pin 2,3, or 4 with a 220 ohm resistor and

  • When the LED is powered, the A0 suddenly increases to about 215 (/1024 * 5....) is about 55C, 131 F) ! WRONG.
    When the LED goes LOW, the A0 Read goes back to the correct ~ 150sh.
    I hooked up my DVM (Digital Vole meter) and the center TMP pin that goes into A0 is holding at a steady 0.70, certainly nowhere near the ~ 1.05 v that would warrant 55C.

I tried a 2nd board (eelego), with the same results.
Suggestions?

Thanks
Henry

Unfortunately all those TMP36 starter-book sketches are wrong.

If you compare sensor output to the potentially unstable 5volt supply,
then a slight drop in that supply will increase sensor value.

Compare sensor voltage to the internal reference voltage of the Uno (1.1volt Aref).
That will not only give you a stable temp, but also a higher resolution.

Basic example sketch attached. Read the comments.
Leo..

const byte tempPin = A0; // connect TPM36 to 3.3volt A0 and (not-shared) ground
float calibration = 0.1039; // calibrate temp by changing the last digit(s) of "0.1039"
float tempC;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use the stable internal 1.1volt Aref
}

void loop() {
  tempC = (analogRead(tempPin) * calibration) - 50.0;
  
  Serial.print("Temperature:  ");
  Serial.print(tempC, 1); // one meaningful decimal place is all you get
  Serial.println(" C");
  
  delay(1000); // use a non-blocking delay when combined with other code
}

@henrygf, what is the actual voltage output by the TMP36's middle pin, and its input voltage (left pin) with the led off and on, measured with your multimeter?

According to this page:

Using the TMP36 is easy, simply connect the left pin to power (2.7-5.5V) and the right pin to ground. Then the middle pin will have an analog voltage that is directly proportional (linear) to the temperature. The analog voltage is independent of the power supply.

temperature_tmp36graph.gif

That will not only give you a stable temp, but also a higher resolution.

But it would limit the max temp you could measure to around 60C. Fine for measuring room temperature!

temperature_tmp36graph.gif

Unforutnately, it is NOT a Voltage variance per se.
As noted

I hooked up my DVM (Digital Vole meter) and the center TMP pin that goes into A0 is holding at a steady 0.70, certainly nowhere near the ~ 1.05 v that would warrant 55C.
The Center pin

  1. Is holding VERY STEADY at 0.70 volgs
  2. 0.70 IS just about right, mapping to about 68-70F

henrygf:
…my DVM (Digital Vole meter)…

henrygf:
…my DVM (Digital Vole meter) …

At first I thought that was just a mistake, but now it is confirmed! Does it measure any type of semi-aquatic rodents?

But what about the input voltage, the left pin?

henrygf:
Unforutnately, it is NOT a Voltage variance per se.

But it is.
The sensor might output a steady/correct voltage, but when the Arduino converts this to temperature it uses another voltage reference to do so.
By default that reference is the 5volt supply.
If that supply voltage dips 1% when the LED turns on, then temp conversion returns a 1% higher number.

Other things could also be wrong, like no current limiting resistor on the LED (excessive current draw),
or sharing sensor ground with breadboard ground, where other current users can lift sensor ground voltage.

1.1volt Aref indeed reduces max temp to ~60C.
If that is a problem, then 3.3volt (with drop resistor) can be used as Aref.

Sensor output should be independent of sensor supply, but I think it's wise to power it from the 'cleaner' 3.3volt pin.
Leo..

OH, A volT meter!
That might me the problem, I got rid of my volE meter, but wait, I have one here somewhere, oh here it is, a volT meter.
Let’s see:
Actually, *I think I have the answer * (wow, this is gonna be a real pain going forward :frowning:

A) Separate from all of the above, I connected the TMP36 GND to the ‘other’ GND on the Arduino board,
and all is perfect!
(primarily: Constant reading on A0 whether the LEDs are all on or all off ),
AND reads a steady 144 (/1023) on A0 which is about 20C 70F which is correct !!!
My ‘take away moral / lesson-learned’ is :
__ ALWAYS Use a separate GND for ICs, separate from the GND that you use for ‘powering’ things (LEDs, Relays, etc).__
Would you all agree with this as a general rule?

B) Paul was onto something! : the left TMP pin (which is tied directly to the [ + ] power rail which is tied directly to the Arduino 5v pin) fluctuated from 4.70 v when LEDs where ON up to 5.01 v when the LEDs were OFF
Which also ties directly into Wawa’s insight that
"All voltages are RELATIVE - "A0 (0.7) is relative to Vcc (sometimes 5.0, sometimes 4.7! )
NOTE: These voltages (4.7 -5.0) were observed even when the USB was disconnected and only the 9v wall charger was powering the circuit.

Ok, Wawa's latest post has the 'a ha!' insight.
But first:
**THANK YOU , THANK YOU THANK YOU ** both.
As an Arduino newbie (3 weeks) this was very daunting and "Oh, hell - there's too many pitfalls. I'm just gonna give up! "
But you guys have given me renewed vigor and confidence - Thank you!

So, lessons learned: ?
Vcc is not always 5.00 v, whether powered from a USB (notoriously dirty) or a 9v wall charger or a 9v battery.
Vcc can be brought down by as much as 0.30 volts by a simple LED with a 220 ohm resistor (22ma), which changes the voltage reference for many ICs, throwing off their ADC reading (even if the voltage output of the IC remains steady, it is the RELATIVE voltage to Vcc that counts, so the change in Vcc can throw off ALL analog readings )

Solution: (aka, design principle going forward for this and all future projects)
A) Use a separate GND for ICs vs the GND used for non-ICs (like LEDs, motors, relays, etc)

Question: There are 3 grounds on my Uno board: 2 on the Power Header (next to my 5v source) and also one on the ARef pin on the other side of the board. My solution used the '2nd Gnd' next to the 5v pin on the Power header. Any reason why I would want to utilize the 'Gnd next to ARef' pin in the future?

B) Use the 3.3V pin as an alternate voltage/power supply line for ICs that will operate on 3.3v , to help isolate them from the 5v that most other things (LEDs, etc) might want?

C) Wawa alluded to 1.1 ARef, and I am drawing a complete blank on that.
I don't see '1.1v' printed anywhere, and I have no idea what ARef is for :frowning:
Forgive my ignorance (hey, I was using a vole meter instead of a volT meter until corrected!)
Is this '1.1 ARef' thingy also a potential 'best practices' avenue, going forward????? What is it??

Much appreciated!
Henry

C) Wawa alluded to 1.1 ARef, and I am drawing a complete blank on that.

I don't see '1.1v' printed anywhere, and I have no idea what ARef is for :(
Forgive my ignorance (hey, I was using a vole meter instead of a volT meter until corrected!)
Is this '1.1 ARef' thingy also a potential 'best practices' avenue, going forward????? What is it??

analogReference()

As you probably know, by default the ADC reference is Vcc (~5V) so 5V reads ~1023 on the ADC. Most Arduinos have 2 other options. "INTERNAL" is the 1.1V reference and "EXTERNAL" where you supply the reference voltage from an external circuit.

With the 1.1V reference, 1.1V reads the maximum 1023 value on the ADC and each count/step is about 1mV instead of 5mV which gives you more resolution. It will also "amplify" any noise. (If you apply more than 1.1V it will still read 1023.)

The 1.1V reference is very stable, but it has a tolerance of about 0.1V so you may have to calibrate your readings (in software).

Thank you DVDDoug.
For my minimalist purposes, just a reasonably stable Vcc will do, which in my case(s) can be achieved via either
a) Using a separate GND pin (as originally suggested above), or
b) Using the 3.3V pin as my Vcc for my 'more delicate' analog IC chip readings.
Nothing this hobbyist will do needs increased resolution from 3.3 down to 1.1 as full scale :slight_smile:
Would you agree?

On the subject of separate grounds, only analogue sensor are sensitive to that, not digital ICs.

Don't use a stable reference for ratiometric sensors, like pots, hall (current) sensors, (most) pressure sensors, etc.
They need the same Aref as their supply, to cancel out sensor output variation against Aref variation.
Leo..