# Arduino sensor data erratic

I'm a noob just getting my feet wet in EE, so go easy on me, please. I bought an Arduino Uno and have been learning tons in the last couple weeks. I took an LM35 sensor, a generic thermistor (using a 10kohm resistor), a light-dependent resistor (photoresistor?) (using a 10k resistor), a button (using internal Arduino pullup resistor), and a couple LEDs (with 220ohm resistors), and wired them all to my Uno. I can provide a diagram, but I don't suspect that it matters much. Basically, some are sharing the same ground, and some are sharing the same 5v.

I'm noticing that the temperature data from the LM35 and the thermistor is very erratic. It varies all by itself, plus when I press the button. I've read many forum posts saying that these types of sensors need a voltage regulator to be accurate, so here's my question: Is it probable that the cause of my erratic sensor data is because the supply from my Arduino is not constant enough, and that it's sagging or spiking?

If so (or if not), what's the solution to this problem? Should I buy one of those little 5v bread-board power supplies?

Update: Ok, here's a diagram. It's a proud day for me, this is my first circuit schematic ever!

And here's a graph of the LM35 readings. It's only read once every 9 seconds. It's just sitting on my office desk, no furnace vent nearby or anything. Now, the graph is not raw analog readings, but rather F temperature. But the raw analog readings are all over the map, including 116, 107, 90, 145, 129, etc. Don't get distracted by the analog->temperature math, I don't care about the accuracy, I just want it to be stable!

Update 2: The photoresistor and the thermistor are reading very consistent, only the LM35 is all over the map.

• You should provide a diagram -- it does matter. Include ALL connections to power and ground, and ALL connections to the Arduino. Your question isn't very answerable without this (and thus not a candidate for migration!!). You might even provide your code. – Scott Seidman Apr 14 '18 at 20:07
• My recommendation would be to test each of your sensors alone in it's own circuit – Scott Seidman Apr 14 '18 at 20:08
• @ScottSeidman Ok, whipped one up in Visio. Updated. I don't think the code is relevant. It basically just reads each sensor once every 9 seconds. – user3629081 Apr 14 '18 at 21:09

IMO much better than the average is a very simple digital low pass filter.

LPF_DEPTH_SHIFT as large as slower reaction to the changes (lower filter "frequency"). Remember that LPF_data has to accommodate (max value) << LPF_DEPTH_SHIFT. You need to choose the proper type.

#define LPF_DEPTH_SHIFT   3

unsigned int LPF_data = UINT_MAX;

unsigned int lowpassfilter(unsigned int value)
{
unsigned int v = LPF_data >> LPF_DEPTH_SHIFT;
if(LPF_data == UINT_MAX)
LPF_data = value << LPF_DEPTH_SHIFT;
LPF_data -= v;
LPF_data += value;
return LPF_data >> LPF_DEPTH_SHIFT;
}

• This isn't what most would call a low-pass filter. – Scott Seidman Apr 14 '18 at 20:30
• @ScottSeidman But it is. A very simple of course. It is called moving (or running) average low pass filter. Google it if you have not heard about it – 0___________ Apr 14 '18 at 20:35
• @ScottSeidman Trust me this code does not as well. Read the code carefully and try to understand it. It is only 5 lines - not the rocket science – 0___________ Apr 14 '18 at 20:47
• @ScottSeidman - is it the low pass filter or not? Have you google it? – 0___________ Apr 15 '18 at 8:22
• It is a LPF, but certainly doesn't explain the LM35 problem - just covers it up. – Scott Seidman Apr 15 '18 at 13:12

You can RC filter the device as an alternative to buying a regulator. The datasheet shows that the IC can operate at 4V and you are supplying 5V. It also states that max current consumption is around 100uA assuming you aren't loading the output which your arduino likely isn't unless you are sampling as fast as possible. For safety margin let's assume the IC may consume up to 1000uA.

5V-4V=1V 1V/1000uA = 1k ohm

Apply the largest capacitor you reasonably can after the resistor and directly across the power supply inputs of the IC and you will have a much cleaner supply. Simulation link below.

• That's cool, thanks for the simulation, and for the idea. I'll have to buy a cap and try that out. – user3629081 Apr 14 '18 at 21:15
• Note you can also apply this same type of filter directly to the input of the ADC after the output of the LM35. This may prove more effective. – lucky bot Apr 14 '18 at 21:28

To be honest, I cannot remember how precise the LM35 is at best.

But a very known algorithm to be used (especially with 'erratic' sensors) is to use the 'moving average' of the last x measurements. This means you take the average of the last x values. If you get a new sensor value, you take again the average of the last x values (that's the 'moving' part).

This will make the temperature over time you get much more smooth. What x is you can define yourself. For a large x, you get a bit of 'delay' because you need first x measurements. But my suspicion if you take for x between 5 and 10 you will see a lot of improvement.

Another addition is to remove values which you know cannot be true (very low or high spikes).

If you don't take the spike values into account, and take the average of the last x measurements, you will get a much better value.

There seems to be even an Arduino library for it (it's calling running average), see here.

• Thanks for your suggestion. My data is so crazy that I'm not sure averaging out the data is going to be beneficial because the data is so scattered. I updated my post with a little more detail and a graph. – user3629081 Apr 14 '18 at 21:23
• Well consider the average of the last 10 values each time, than the first 2/3 will have a reasonable smooth flow, the last 1/3 have have lower values but maybe the temperature changed in real time as well slightly. – Michel Keijzers Apr 14 '18 at 21:29

Determine if the noise is correlated or random.

It will help if you capture a bunch of data and plot it. The frequency and type of noise can be very important. If you have "random" noise, it is easy to filter out in software as long as you are sampling your ADC fast enough. If your have correlated noise, which basically means the noise changes with the signal you are trying to measure, it can be more difficult.

So, to answer your original question, to determine if the noise is due to power supply fluctuations, I would connect another ADC pin on the Arduino to the power supply (through a resistor divider) and sample it at the same time as the sensor. If you plot data for both, it will be very clear if the noise is correlated.

If they are correlated, one option would be to keep the supply ADC in your design and subtract it from the sensor reading, giving a quasi differential reading.

If they are not correlated, the noise is probably coming from something else. In that case,

• How long are the wires connected to the sensor?
• Do you have any filtering caps on the remote sensor?

Also, This is an excellent tool I often use to quickly design software filters: http://t-filter.engineerjs.com/ It will generate source code for you. (For Arduino, make sure to set the number format to integer!)

• Definitelly FFT is the best for arduino and this more than simple task. Arduino is known for its computation power. – 0___________ Apr 15 '18 at 8:49