Smoothing analog sensor values

Aus hyperdramatik
Zur Navigation springen Zur Suche springen

If the sensor value on your analog pin has some unwanted amplitudes, you can smooth it with some tricks.

trick#1) smoothing analog sensor values

Tremors can come from fluctuating power supply, interference or a circuit inside the Arduino chip.

In this example, we simply take a portion of the new measurement and combine it with a portion of the value measured last time. The part of the old value is always a bit bigger than the new value and so the new value influences the measurement but we overwrite the tremor.


int sensorValue;

void setup(){

void loop(){
sensorValue = analogRead(A0);
smoothValue = 0.6 * smoothValue + 0.4 * sensorValue; //you can tray with the values you multiply. It is just always under 1
Serial.print("\t"); //TAB

trick#2) smoothing analog sensor values #2: running average

To smooth your sensor value, a simple line of code you can add to calculate the running average is:

averageSensorValue = (averageSensorValue * (average-1) + currentSensorValue) / average;


trick#3) smoothing library