ESP32 has a 12 bit ADC. I've put a .1 microfarad capacitor in my circuit to reduce noise.
I've put ADC readings in an array and am smoothing the readings in an array as well.
I'm wondering if my delay time of 1 ms and number of 64 iterations is enough for smoothing to occur? I'm basing the delay time off of the arduino smoothing example on the the arduno website.
Also i'm going to use the ESP Now protocol to transfer these values to another ESP32.
The protocol only allows 250 bytes of data to be sent.
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/esp_now.html
esp_err_t esp_now_send(const uint8_t *peer_addr, const uint8_t *data, size_t len)
Send ESPNOW data.
I'm wondering if I mapped this properly since I declared my AzimuthVoltage global variable to be uint8_t and since the original Azimuth values have a range of 0-4096?
uint8_t AzimuthVoltage = 0;
// Apply the calibration to the sensor reading
AzimuthVoltage = map(Azimuth, 0, 4096, 0, 255);
//Sends analog values in this format: i.e. {255}
uint8_t data[1] = {AzimuthVoltage};
/*
Smoothing
Reads repeatedly from an analog input, calculating a running average and
printing it to the computer. Keeps 1 reading 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
uint8_t delay_numReadings = 1; // delay to process smoothing readings
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;
uint8_t AzimuthVoltage = 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
// Read the sensor
Azimuth = adc1_get_raw(ADC1_CHANNEL_6);
int inputPin[1] = {Azimuth};
//analog inputs in an array
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
}
while (millis() < time_now + delay_numReadings) {}
// Apply the calibration to the sensor reading
AzimuthVoltage = map(Azimuth, 0, 4096, 0, 255);
//Sends analog values in this format: i.e. {255}
uint8_t data[1] = {AzimuthVoltage};
uint8_t i;
for (i = 0; i < 1; i++) {
Serial.print(data[i]);
Serial.println(" ");
}
Serial.println(" ");
//wait approx. [period] ms}
while (millis() < time_now + delay_time) {}
}
Here is my sample serial output that I'm trying to reduce the variation for.
I kept my lighting condition the same (an led lamp post next to my desk)
6
6
5
6
7
7
5
2
2
2
2
2
2
3
4
5
6
6
5
2