I have a soil moisture reading system which I made using capacitive soil moisture sensor connected to an ESP8266. My readings are too noisy. I tried using stable power supplies, tried changing the sensor, etc. But there is no improvement.
I am looking for a way to average like 5 readings expecting the reading to be more stable. Could anyone help me on how to add this in my below code?
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
int sensorVal = analogRead(A0);
int percentageHumididy = map(sensorVal, 1023, 800, 0, 100);
Serial.print(percentageHumididy);
Serial.println("%");
Serial.print("Raw Sensor value = ");
Serial.println(sensorVal);
delay(2000);
}
Read the sensor value several times.
Pause briefly after each reading.
Find the sum of all read values.
Divide by the number of values.
Use a for loop to simplify your program for statement
Thanks a lot.. I tried the below after some googling and from your guidance..
Could you please advice if this is good? Also, I can I know it is acually averaging?
#define SensorPin A0
float sensorValue = 0;
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
for (int i = 1; i <= 100; i++)
{
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue / 100.0;
Serial.println(sensorValue);
delay(30);
int percentageHumididy = map(sensorValue, 1023, 800, 0, 100);
Serial.print(percentageHumididy);
Serial.println("%");
Serial.print("Raw Sensor value = ");
Serial.println(sensorValue);
delay(500);
}
#include <runningAvg.h> // The include of the .h file.
runningAvg smoother(5); // Create a running average of 5 value smoother. (global variable)
ave = smoother.addData(aValue); // Pop this number into our smoother. (Running average object.) Out pops the average.]
You'll need to grab LC_baseTools from the IDE library manager to get these goodies.
Here is an example for typing numbers into one of these running average objects. Then it prints out the average and a bunch of other stuff about the list of numbers it has.
#include <runningAvg.h>
/*
Running average.
This class was originally developed as a super simple to use data smoother. You decide, when creating
it, how many data points you would like to smooth your data over. Then each time you add a data point
it returns the running average of the last n points that were added. Simple to use, easy to understand.
Then, of course, this opened up new horizons for things like; What was the max and min data points we
saw in this set? So that was added.
Then it was.. Well, what about sound? Sound is all about the delta between max and min. Delta was added.
Then.. Yes, I also added standard deviation. What the heck? Everything else is in the pot. Might as
well go for it all.
Still, true to its roots, it only calculates the average when a datapoint is entered. All the rest
are only calculated if/when the user actually asks for them. But they are there, ready to leap into
action, if desired.
So this example lets you add data points using the serial monitor and it'll print out all the stuff
it can calculate with this data.
Enjoy!
-jim lee
*/
runningAvg smoother(5); // Our smoother. You can change the number of datapoints it will act on.
char inBuff[80]; // A char buffer (c string) to hold your typings.
int index; // An index to be used in storing charactors into our char buffer.
// Standard setup stuff..
void setup(void) {
Serial.begin(9600); // Fire up the serial stuff.
inBuff[0] = '\0'; // Clear the c string.
index = 0; // The next char we read in goes here.
Serial.println(F("Enter numbers")); // Tell Mrs user to start inputting numbers.
}
// Standard loop stuff..
void loop(void) {
char aChar; // A char to catch your typings in.
float aValue; // The float version of the number you typed.
float ave; // The Average that the smoother will return to us.
if (Serial.available()) { // If there is a char in the waiting to be read..
aChar = Serial.read(); // Grab and save the char.
if (aChar=='\n') { // If its a newline char.. (Make sure the serial monitor is set to newline.)
aValue = atof(inBuff); // Decode what we have read in so far as a float value. (Decimal number)
ave = smoother.addData(aValue); // Pop this number into our smoother. (Running average object.) Out pops the average.
Serial.println(); // From here down its just grabing info from the
Serial.print(F("Entered : ")); // SMoother and displaying it on the serial monitor.
Serial.println(inBuff);
Serial.print(F("Data : "));
for(int i=0;i<smoother.getNumValues();i++) {
Serial.print(smoother.getDataItem(i));
Serial.print(" ");
}
Serial.println();
Serial.print(F("Average : "));
Serial.println(ave,4);
Serial.print(F("Min : "));
Serial.println(smoother.getMin(),4);
Serial.print(F("Max : "));
Serial.println(smoother.getMax(),4);
Serial.print(F("Delta : "));
Serial.println(smoother.getDelta());
Serial.print(F("Deviation : "));
Serial.println(smoother.getStdDev());
Serial.println(F("--------------"));
inBuff[0] = '\0'; // Clear the inBuff string.
index = 0; // Reset the index to start a new number string.
} else { // Else, it wasn't a newline char..
inBuff[index++] = aChar; // So we save the char we got into the c string.
inBuff[index] = '\0'; // And pop an end of string after it.
}
}
}
I managed to do it with this Smoothing example.. thanks a lot for your time and support..
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
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[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// 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
Serial.println(average);
int percentageHumididy = map(average, 1023, 800, 0, 100);
Serial.print(percentageHumididy);
Serial.println("%");
delay(1000); // delay in between reads for stability
}