Energia

Examples > Analog

### Smoothing

This sketch reads repeatedly from an analog input, calculating a running average and printing it to the computer. This example is useful for smoothing out the values from jumpy or erratic sensors, and also demonstrates the use of arrays to store data.

### Hardware Required

- MSP-EXP430G2 LaunchPad
- a variable resistor, like a potentiometer

### Relevant Groundwork

The code below sequentially stores 10 readings from your analog sensor into an array, one by one. With each new value, the sum of all the numbers is generated and divided, producing an average value which can then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather than waiting for 10 new values, for instance) there is no lag time in calculating this running average. Altering the size of the array used, by changing `numReadings` to a larger value will smooth the data collected even further.

Connect the three wires from the potentiometer to your LaunchPad board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 3 volts to the other outer pin of the potentiometer. The third goes from analog input A3 to the middle pin of the potentiometer.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the wiper which is connected to the center pin of the potentiometer. This changes the voltage at the center pin. When the resistance between the center and the side connected to 3 volts is close to zero (and the resistance on the other side is close to 10 kilohms), the voltage at the center pin nears 3 volts. When the resistances are reversed, the voltage at the center pin nears 0 volts, or ground. This voltage is the analog voltage that you're reading as an input.

The LaunchPad has a circuit inside called an analog-to-digital converter that reads this changing voltage and converts it to a number between 0 and 1023. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and the input value is 0. When the shaft is turned all the way in the opposite direction, there are 3 volts going to the pin and the input value is 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

### Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

### Schematic

### Code Explanation

None.

### Code

    /*
      Smoothing

      Reads repeatedly from an analog input, calculating a running average
      and printing it to the computer. Keeps ten readings in an array and
      continually averages them.

      The circuit:
      * Analog sensor (potentiometer will do) attached to analog input 3

      Created 22 April 2007
      By David A. Mellis
      modified 9 Apr 2012
      by Tom Igoe
      modified 9 May 2013
      by Sean Alvarado

      This example code is in the public domain.

    */

    // Define the number of samples to keep track of. The higher the number,
    // the more the readings will be smoothed, but the slower the output will
    // respond to the input. Using a constant rather than a normal variable lets
    // use this value to determine the size of the readings array.

    const int numReadings = 10;

    int readings[numReadings]; // the readings from the analog input
    int index = 0; // the index of the current reading
    int total = 0; // the running total
    int average = 0; // the average

    int inputPin = A3;

    void setup()
    {
      // initialize serial communication with computer:
      Serial.begin(9600);
      // initialize all the readings to 0:
      for (int thisReading = 0; thisReading < numReadings; thisReading++)
        readings[thisReading] = 0;
    }

    void loop() {
      // subtract the last reading:
      total= total - readings[index];
      // read from the sensor:
      readings[index] = analogRead(inputPin);
      // add the reading to the total:
      total= total + readings[index];
      // advance to the next position in the array:
      index = index + 1;

      // if we're at the end of the array...
      if (index >= numReadings)
        // ...wrap around to the beginning:
        index = 0;

      // calculate the average:
      average = total / numReadings;
      // send it to the computer as ASCII digits
      Serial.println(average);
      delay(1); // delay in between reads for stability
    }

### Working Video

(Insert Video Here)

### Try it out:

- add an LED to the program that reacts to the potentiometer

### See Also:

- setup()
- loop()
- analogRead()
- int
- serial
- float
- BareMinimum: The bare minimum of code needed to start an Energia sketch.
- Blink: Turn an LED on and off.
- DigitalReadSerial: Read a switch, print the state out to the Energia Serial Monitor.
- AnalogReadSerial: Read a potentiometer, print it's state out to the Energia Serial Monitor.
- Fade: Demonstrates the use of analog output to fade an LED.
- ReadAnalogVoltage : Reads an analog input and prints the voltage to the serial monitor

Getting Started Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Energia reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.

©Energia | Edit Page | Page History | Printable View | All Recent Site Changes