Determine the average of several sensors

Hello all

I need help or your advice for my project
I want to build an automatic irrigation system for my greenhouse. I want to determine the moisture of the soil with 5 soil moisture sensors and combine these values to an average value.

Can someone explain to me how I can now get these values from the 5 sensors to an average value?

Thanks in advance
Greetings Jonny

Given 5 values how would you calculate their average using pencil and paper ?

no i mean how i write the code

Start with how you would do it manually then you can convert it to code

Please post your code that gets the 5 values

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

#define ventil 2 

#define SENSOR_PIN0 A0
#define SENSOR_PIN1 A1
#define SENSOR_PIN2 A2
#define SENSOR_PIN3 A3
#define SENSOR_PIN4 A4

int sensorValue0 = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;

void setup() {
  pinMode(ventil, OUTPUT);  
void loop() {
   int sensorValue0 = analogRead(A0);
   int sensorValue1 = analogRead(A1);
   int sensorValue2 = analogRead(A2);
   int sensorValue3 = analogRead(A3);
   int sensorValue4 = analogRead(A4);


If I told you that

sensorValue0 = 20
sensorValue1 = 25
sensorValue2 = 30
sensorValue3 = 10
sensorValue4 = 18

Show how you would work out the avarage on paper

divide and then multiply by 5

Try again

uint32_t add;
add = sensorValue0 + sensorValue1 + sensorValue2 + sensorValue3 + sensorValue4;
uint16_t divide;
divide = add / 5;

like so

Why don’t you try it ?

There is a neater way of doing it but get it working the simple way first

1 Like