Smoothing of multiple analog values in an array, mapping them and reducing noise

Hello, I'm unsure of whether of not my analog values are actually being smoothed out. I've uploaded the code and I am using .1 microfarad capacitors in between my analog out and gnd.

When I'm reading out the voltage with my multimeter there is still a big discrepancy.
2x There are two photoresistors in series and an analog out in between them.
I'm reading at analog out and ground after the 2nd photoresistor.
Seeing a .1 mV difference.

I've modified this code and am taking 64 readings.

Based on the thread below I am using long instead of int for the average and total variables.

I'm unsure if the values have been smoothed in this portion of the code since they come after the smoothing code:

  // Apply the calibration to the sensor reading
  AzimuthVoltage = map(Azimuth, 0, 4096, 0, 3300);
  ElevationVoltage = map(Elevation, 0, 4096, 0, 3300);

I'm uncertain if the smoothing code has been skipped or not. Any feedback is appreciated thanks!

/*
  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.

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

// 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 us use this
// value to determine the size of the readings array.
const int numReadings = 64;

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

unsigned long delay_time = 5000;  //Earth rotates .25 degrees/minute. In 4 minutes Earth rotates 1 degree.
unsigned long time_now = 0;

//Voltage divider analog in pins
const int PinAzimuth = 35;
const int PinElevation = 34;

int Azimuth = 0;
int Elevation = 0;

int AzimuthVoltage = 0;
int ElevationVoltage = 0;

void setup() {
  Serial.begin(115200);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}
void loop() {

  time_now = millis();

  // Read the sensor
  Azimuth = analogRead(PinAzimuth);
  Elevation = analogRead(PinElevation);

  int inputPin[2] = {Azimuth, Elevation};

  uint8_t ai;

  for (ai = 0; ai < 2; ai++) {
    // subtract the last reading:
    total = total - readings[readIndex];
    // read from the sensor:
    readings[readIndex] = inputPin[ai];
    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    // if we're at the end of the array...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }
    // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
 }
  
  // Apply the calibration to the sensor reading
  AzimuthVoltage = map(Azimuth, 0, 4096, 0, 3300);
  ElevationVoltage = map(Elevation, 0, 4096, 0, 3300);

  //Sends analog values in this format: i.e. {1800,2100}
  int data[2] = {AzimuthVoltage, ElevationVoltage};
  uint8_t i;

  for (i = 0; i < 2; i++) {
    Serial.print(data[i]);
    Serial.println(" ");
  }
  Serial.println(" ");

  //wait approx. [period] ms}
  while (millis() < time_now + delay_time) {}
}

If you are using analog read with an ESP32 that is a source for some issues. The Arduino code is not able to set the dB level of the A:D of the ESP32.

Include:

#include <driver/adc.h>

Setup looks like:

// https://dl.espressif.com/doc/esp-idf/latest/api-reference/peripherals/adc.html
  // set up A:D channels
  adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);

The link is a excellent link for the ESP32 A:D API

void TaskAnalogVoltRead_LIDAR( void *pvParameters )
{
  String localBuffer;
  localBuffer.reserve ( StringBufferSize300 );
  int iBit = 1;
  float ADbits = 4095;
  float offSET = 1.0f;
  float r1 = 100800.0f; // R1 in ohm, 100k = 100,800.0 //
  float r2 = 38780.0f; // R2 in ohm, 38k = 38000.0 //actual 38780K
  float uPvolts = 3.3f;
  // ADC1 channel 0 is GPIO36
  // ADC1 channel 1 is GPIO37
  // ADC1 channel 6 is GPIO34
  // https://dl.espressif.com/doc/esp-idf/latest/api-reference/peripherals/adc.html
  // to get resistor R2 go to:
  // http://www.ohmslawcalculator.com/voltage-divider-calculator
  //   used 12 volts for the input voltage to calculate R2, used 100K for R1
  for (;;)
  {
    // group handle, WaitForBits, ClearOnExitBit, WaitForAllBits, TicksToWait
    xEventGroupWaitBits( eg, evtAnalogVoltReadTask_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY );
    // read the input
    iBit = iBit << 1;
    if ( iBit == 1073741824 )
    {
      if ( xSemaphoreTake( sema_AnalogVoltRead_LIDAR, xSemaphoreTicksToWait ) == pdTRUE )
      {
        Vbatt_LIDAR += ( ((( uPvolts * adc1_get_raw(ADC1_CHANNEL_6)) / ADbits) / r2 * ( r1 + r2)) + offSET );
        Vbatt_LIDAR = Vbatt_LIDAR /2; // average readings
        xSemaphoreTake( sema_Time, xSemaphoreTicksToWait );
        fMakeTimeStamp ( localBuffer );
        xSemaphoreGive( sema_Time ); // give up semaphore
      } // if ( xSemaphoreTake( sema_AnalogVoltRead_LIDAR, xSemaphoreTicksToWait ) == pdTRUE )
      iBit = 1;
    } // if ( iBit == 1073741824 )
  }
  vTaskDelete( NULL );
}

Is a code example.

Thanks. I'm trying that, but am getting a compile error. :frowning:

This is how I updated the pin values.

adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34
const int PinElevation = adc1_get_raw(ADC1_CHANNEL_6);

adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35
const int PinAzimuth = adc1_get_raw(ADC1_CHANNEL_7);

https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/adc.html

I'm getting this error:

Compiling sketch...

ESP32_ANALOG_READ_AZIMUTH_AND_ELEVATION_R3:39:26: error: expected constructor, destructor, or type conversion before '(' token

adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34

^

ESP32_ANALOG_READ_AZIMUTH_AND_ELEVATION_R3:43:26: error: expected constructor, destructor, or type conversion before '(' token

adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35

I'm not sure if the Arduino IDE sees:

#include <driver/adc.h>

include adc esp32.PNG

Here is the updated 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.

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

#include <driver/adc.h>
[img]https://forum.arduino.cc/index.php?action=dlattach;topic=608472.0;attach=302790[/img]

// 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 us use this
// value to determine the size of the readings array.
const int numReadings = 64;

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

unsigned long delay_time = 5000;  //Earth rotates .25 degrees/minute. In 4 minutes Earth rotates 1 degree.
unsigned long time_now = 0;

//Voltage divider analog in pins
// https://dl.espressif.com/doc/esp-idf/latest/api-reference/peripherals/adc.html
  // set up A:D channels

adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34
const int PinElevation = adc1_get_raw(ADC1_CHANNEL_6);

adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35
const int PinAzimuth = adc1_get_raw(ADC1_CHANNEL_7);

int Azimuth = 0;
int Elevation = 0;

int AzimuthVoltage = 0;
int ElevationVoltage = 0;

void setup() {
  Serial.begin(115200);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}
void loop() {

  time_now = millis();

  // Read the sensor
  Azimuth = analogRead(PinAzimuth);
  Elevation = analogRead(PinElevation);

  int inputPin[2] = {Azimuth, Elevation};

  uint8_t ai;

  for (ai = 0; ai < 2; ai++) {
    // subtract the last reading:
    total = total - readings[readIndex];
    // read from the sensor:
    readings[readIndex] = inputPin[ai];
    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    // if we're at the end of the array...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }
    // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
 }
  
  // Apply the calibration to the sensor reading
  AzimuthVoltage = map(Azimuth, 0, 4096, 0, 3300);
  ElevationVoltage = map(Elevation, 0, 4096, 0, 3300);

  //Sends analog values in this format: i.e. {1800,2100}
  int data[2] = {AzimuthVoltage, ElevationVoltage};
  uint8_t i;

  for (i = 0; i < 2; i++) {
    Serial.print(data[i]);
    Serial.println(" ");
  }
  Serial.println(" ");

  //wait approx. [period] ms}
  while (millis() < time_now + delay_time) {}
}

include adc esp32.PNG

Okay. I realized I was still using analogRead in the void loop.

Here is corrected code.
But I’m still getting a .1 volt difference.
I’m wondering if it has to do with the breadboard. I hope not.
I just wanted to avoid soldering.

/*
  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.

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

#include <driver/adc.h>

// 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 us use this
// value to determine the size of the readings array.
const int numReadings = 64;

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

unsigned long delay_time = 5000;  //Earth rotates .25 degrees/minute. In 4 minutes Earth rotates 1 degree.
unsigned long time_now = 0;

int Azimuth = 0;
int Elevation = 0;

int AzimuthVoltage = 0;
int ElevationVoltage = 0;

void setup() {
  Serial.begin(115200);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}
void loop() {

  time_now = millis();

  //Voltage divider analog in pins
  // https://dl.espressif.com/doc/esp-idf/latest/api-reference/peripherals/adc.html
  // set up A:D channels

  adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34

  adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35

  // Read the sensor
  Azimuth =  adc1_get_raw(ADC1_CHANNEL_7);
  Elevation = adc1_get_raw(ADC1_CHANNEL_6);

  int inputPin[2] = {Azimuth, Elevation};

  uint8_t ai;

  for (ai = 0; ai < 2; ai++) {
    // subtract the last reading:
    total = total - readings[readIndex];
    // read from the sensor:
    readings[readIndex] = inputPin[ai];
    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    // if we're at the end of the array...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }
    // calculate the average:
    average = total / numReadings;
    // send it to the computer as ASCII digits
  }

  // Apply the calibration to the sensor reading
  AzimuthVoltage = map(Azimuth, 0, 4096, 0, 3300);
  ElevationVoltage = map(Elevation, 0, 4096, 0, 3300);

  //Sends analog values in this format: i.e. {1800,2100}
  int data[2] = {AzimuthVoltage, ElevationVoltage};
  uint8_t i;

  for (i = 0; i < 2; i++) {
    Serial.print(data[i]);
    Serial.println(" ");
  }
  Serial.println(" ");

  //wait approx. [period] ms}
  while (millis() < time_now + delay_time) {}
}