Pages: [1] 2 3   Go Down
Author Topic: Soil Moisture sensor  (Read 5265 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I have to build a small soil moisture monitor for 4 type of soil at the same time (the test was made on the same soil/same moisture). I build sensor and code a sketch (based on GardenBot) to read 4 of them from Arduino Uno. I've got some weird read value. In my test, i can see that the DigitalPin 5 and 10 cannot provide enough power for the sensor?? Someone know why? I also try to use AnalogPin 5 to 2 and only pin 2 and 3 give good reading.... I change the ATMEGA328p because i have spare and nothing change. I post the last sketch revision i made. In this sketch i can have good reading from sensor #2, #3 and #4 but the sensor #1 give me random number ranging from 400 to 800. I also post a picture to show how the sensor is made. For the 57-100K resistor i put 100K on all sensor. Help please!!

Code:
#define voltageFlipPin11 2
#define voltageFlipPin12 3
#define voltageFlipPin21 4
#define voltageFlipPin22 6
#define voltageFlipPin31 7
#define voltageFlipPin32 8
#define voltageFlipPin41 9
#define voltageFlipPin42 11
#define sensorPin1 5
#define sensorPin2 3
#define sensorPin3 1
#define sensorPin4 0

int flipTimer = 1000;
int currentsensor=1;
int val11;
int val12;
int avg1;
int val21;
int val22;
int avg2;
int val31;
int val32;
int avg3;
int val41;
int val42;
int avg4;

void setup(){
  Serial.begin(9600);
  pinMode(voltageFlipPin11, OUTPUT);
  pinMode(voltageFlipPin12, OUTPUT);
  pinMode(voltageFlipPin21, OUTPUT);
  pinMode(voltageFlipPin22, OUTPUT);
  pinMode(voltageFlipPin31, OUTPUT);
  pinMode(voltageFlipPin32, OUTPUT);
  pinMode(voltageFlipPin41, OUTPUT);
  pinMode(voltageFlipPin42, OUTPUT);
  digitalWrite(voltageFlipPin11, LOW);
  digitalWrite(voltageFlipPin12, LOW);
  digitalWrite(voltageFlipPin21, LOW);
  digitalWrite(voltageFlipPin22, LOW);
  digitalWrite(voltageFlipPin31, LOW);
  digitalWrite(voltageFlipPin32, LOW);
  digitalWrite(voltageFlipPin41, LOW);
  digitalWrite(voltageFlipPin42, LOW);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT);
  pinMode(sensorPin4, INPUT);
}

void loop(){
  switch (currentsensor){

  case 1:
    digitalWrite(voltageFlipPin11, HIGH);
    digitalWrite(voltageFlipPin12, LOW);
    delay(flipTimer);
    val11 = analogRead(sensorPin1);
    delay(flipTimer);
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, HIGH);
    delay(flipTimer);
    val12 = 1023 - analogRead(sensorPin1);
    avg1 = (val11 + val12) / 2;
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, LOW);
    ++currentsensor;
    break;

  case 2:
    digitalWrite(voltageFlipPin21, HIGH);
    digitalWrite(voltageFlipPin22, LOW);
    delay(flipTimer);
    val21 = analogRead(sensorPin2);
    delay(flipTimer);
    digitalWrite(voltageFlipPin21, LOW);
    digitalWrite(voltageFlipPin22, HIGH);
    delay(flipTimer);
    val22 = 1023 - analogRead(sensorPin2);
    avg2 = (val21 + val22) / 2;
    digitalWrite(voltageFlipPin21, LOW);
    digitalWrite(voltageFlipPin22, LOW);
    ++currentsensor;
    break;

  case 3:
    digitalWrite(voltageFlipPin31, HIGH);
    digitalWrite(voltageFlipPin32, LOW);
    delay(flipTimer);
    val31 = analogRead(sensorPin3);
    delay(flipTimer);
    digitalWrite(voltageFlipPin31, LOW);
    digitalWrite(voltageFlipPin32, HIGH);
    delay(flipTimer);
    val32 = 1023 - analogRead(sensorPin3);
    avg3 = (val31 + val32) / 2;
    digitalWrite(voltageFlipPin31, LOW);
    digitalWrite(voltageFlipPin32, LOW);
    ++currentsensor;
    break;

  case 4:
    digitalWrite(voltageFlipPin41, HIGH);
    digitalWrite(voltageFlipPin42, LOW);
    delay(flipTimer);
    val41 = analogRead(sensorPin4);
    delay(flipTimer);
    digitalWrite(voltageFlipPin41, LOW);
    digitalWrite(voltageFlipPin42, HIGH);
    delay(flipTimer);
    val42 = 1023 - analogRead(sensorPin4);
    avg4 = (val41 + val42) / 2;
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, LOW);
    ++currentsensor;
    break;

  case 5:
    Serial.print("Valeur sensor #1: ");
    Serial.println(avg1);
    Serial.println();
    Serial.print("Valeur sensor #2: ");
    Serial.println(avg2);
    Serial.println();
    Serial.print("Valeur sensor #3: ");
    Serial.println(avg3);
    Serial.println();
    Serial.print("Valeur sensor #4: ");
    Serial.println(avg4);
    Serial.println("-----------------------");
     Serial.print("Valeur moyenne de tout les sensors: ");
    Serial.println((avg1+avg2+avg3+avg4)/4);
    Serial.println();
    Serial.println();
    currentsensor=1;
    break;

  }

}



And the sensors. I coated the upper of the sensor with paraffin because water have tendency to accumulate on the little piece of board and cause bad reading.

« Last Edit: January 05, 2013, 12:58:06 pm by Lentrave » Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1018
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At "case 4:", you should use Pin41 and Pin42, but you also use Pin11 and Pin12. So that's an program error.

To avoid such errors you should write a function for just one probe. And you can call that function 4 times for the 4 probes.
Also setting the output mode could be done in that function.

The usage of pins can be done by calling that function, but they could also be declared in an array.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tanks .... i didn't notice that one. tanks for the cue, i will try to optimize the code like you said and post it! Tanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok ... i juste try the modification on the sketch but same problem ....

This is what i receive in the Serial monitor when all the sensor is in water.... It's not a sensor problem because if i switch it i have the same result:

Code:
Valeur sensor #1: 646

Valeur sensor #2: 15

Valeur sensor #3: 12

Valeur sensor #4: 15
-----------------------
Valeur moyenne de tout les sensors: 172
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok ... i change the analogPin definition and it seem to work in water. When i remove it and let the sensor in the air sensor #3 and #4 give me nearly 1024 that's ok but #1 and #2 give me random number between 600 and 800... ... i use pin 4, pin 2, pin 1 and pin 0. I really don't understand why analogPin works all fine but in this sketch some AnalogPin and DigitalPin, when they are used, give me error (DigitalPin= Not enough current, AnalogPin= Bad read value) ....

 Here the sketch(Translate from french to english) :

Code:
#define voltageFlipPin11 2
#define voltageFlipPin12 3
#define voltageFlipPin21 4
#define voltageFlipPin22 6
#define voltageFlipPin31 7
#define voltageFlipPin32 8
#define voltageFlipPin41 9
#define voltageFlipPin42 11
#define sensorPin1 4
#define sensorPin2 2
#define sensorPin3 1
#define sensorPin4 0

int flipTimer = 1000;
int currentsensor=1;
int val11;
int val12;
int avg1;
int val21;
int val22;
int avg2;
int val31;
int val32;
int avg3;
int val41;
int val42;
int avg4;

void setup(){
  Serial.begin(9600);
  pinMode(voltageFlipPin11, OUTPUT);
  pinMode(voltageFlipPin12, OUTPUT);
  pinMode(voltageFlipPin21, OUTPUT);
  pinMode(voltageFlipPin22, OUTPUT);
  pinMode(voltageFlipPin31, OUTPUT);
  pinMode(voltageFlipPin32, OUTPUT);
  pinMode(voltageFlipPin41, OUTPUT);
  pinMode(voltageFlipPin42, OUTPUT);
  digitalWrite(voltageFlipPin11, LOW);
  digitalWrite(voltageFlipPin12, LOW);
  digitalWrite(voltageFlipPin21, LOW);
  digitalWrite(voltageFlipPin22, LOW);
  digitalWrite(voltageFlipPin31, LOW);
  digitalWrite(voltageFlipPin32, LOW);
  digitalWrite(voltageFlipPin41, LOW);
  digitalWrite(voltageFlipPin42, LOW);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT);
  pinMode(sensorPin4, INPUT);
}

void loop(){
  switch (currentsensor){

  case 1:
    digitalWrite(voltageFlipPin11, HIGH);
    digitalWrite(voltageFlipPin12, LOW);
    delay(flipTimer);
    val11 = analogRead(sensorPin1);
    delay(flipTimer);
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, HIGH);
    delay(flipTimer);
    val12 = 1023 - analogRead(sensorPin1);
    avg1 = (val11 + val12) / 2;
    digitalWrite(voltageFlipPin11, LOW);
    digitalWrite(voltageFlipPin12, LOW);
    ++currentsensor;
    break;

  case 2:
    digitalWrite(voltageFlipPin21, HIGH);
    digitalWrite(voltageFlipPin22, LOW);
    delay(flipTimer);
    val21 = analogRead(sensorPin2);
    delay(flipTimer);
    digitalWrite(voltageFlipPin21, LOW);
    digitalWrite(voltageFlipPin22, HIGH);
    delay(flipTimer);
    val22 = 1023 - analogRead(sensorPin2);
    avg2 = (val21 + val22) / 2;
    digitalWrite(voltageFlipPin21, LOW);
    digitalWrite(voltageFlipPin22, LOW);
    ++currentsensor;
    break;

  case 3:
    digitalWrite(voltageFlipPin31, HIGH);
    digitalWrite(voltageFlipPin32, LOW);
    delay(flipTimer);
    val31 = analogRead(sensorPin3);
    delay(flipTimer);
    digitalWrite(voltageFlipPin31, LOW);
    digitalWrite(voltageFlipPin32, HIGH);
    delay(flipTimer);
    val32 = 1023 - analogRead(sensorPin3);
    avg3 = (val31 + val32) / 2;
    digitalWrite(voltageFlipPin31, LOW);
    digitalWrite(voltageFlipPin32, LOW);
    ++currentsensor;
    break;

  case 4:
    digitalWrite(voltageFlipPin41, HIGH);
    digitalWrite(voltageFlipPin42, LOW);
    delay(flipTimer);
    val41 = analogRead(sensorPin4);
    delay(flipTimer);
    digitalWrite(voltageFlipPin41, LOW);
    digitalWrite(voltageFlipPin42, HIGH);
    delay(flipTimer);
    val42 = 1023 - analogRead(sensorPin4);
    avg4 = (val41 + val42) / 2;
    digitalWrite(voltageFlipPin41, LOW);
    digitalWrite(voltageFlipPin42, LOW);
    ++currentsensor;
    break;

  case 5:
    Serial.print("Sensor Value #1: ");
    Serial.println(avg1);
    Serial.println();
    Serial.print("Sensor Value #2: ");
    Serial.println(avg2);
    Serial.println();
    Serial.print("Sensor Value #3: ");
    Serial.println(avg3);
    Serial.println();
    Serial.print("Sensor Value #4: ");
    Serial.println(avg4);
    Serial.println("-----------------------");
     Serial.print("Average Sensor Value: ");
    Serial.println((avg1+avg2+avg3+avg4)/4);
    Serial.println();
    Serial.println();
    currentsensor=1;
    break;

  }

}
« Last Edit: January 05, 2013, 01:29:23 pm by Lentrave » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you should read the section about arrays, that can make your code far more compact

Code:
int flipPins[] = { 2, 3, 4, 6, 7, 8, 9, 11 };
int sensorPins[] = {4, 2, 1, 0};
int avg[] = {0,0,0,0};

setup:
for (int i=0; i< 8; i++)
{
  pinMode(flipPins[i], OUTPUT);
  digitalWrite(flipPins[i], LOW);
}

for (int i=0; i< 4; i++)
{
  pinMode(sensorPins[i], INPUT);
  digitalWrite(flipPins[i], LOW);
}

furthermore then you can introduce functions for repeating pieces of code:
Code:

void function(int number)
{
    digitalWrite(flipPins[number], HIGH);
    digitalWrite(flipPins[number+1], LOW);
    delay(flipTimer);

    int val1 = analogRead(sensorPin[number]);
    delay(flipTimer);

    digitalWrite(flipPins[number], HIGH);
    digitalWrite(flipPins[number+1], LOW);
    delay(flipTimer);

    int val2 = 1023 - analogRead(sensorPin[number]);
    avg[number] = (val1 + val2) / 2;

    digitalWrite(flipPins[number], HIGH);
    digitalWrite(flipPins[number+1], LOW);
}

The switch will become simpler
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tanks for the script, yes it's simpler but i have the same problem .... i cannot use 4 sensor at same time, i can read 3 but when i add one it give me bad read ....
Logged

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you shouldn't use digital pins 0 and 1.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I try nearly all analog pin combinations and i dont even see a patern... Random number on random pins... Maybe limitation with the A/D ...???
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#define voltageFlipPin11 2
#define voltageFlipPin21 4

conflicts with these pins ?

#define sensorPin1 4
#define sensorPin2 2

IIRC Analog pins should be addressed as A0 A1 A2 A4 etc.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#define voltageFlipPin11 2
#define voltageFlipPin21 4

conflicts with these pins ?

#define sensorPin1 4
#define sensorPin2 2

IIRC Analog pins should be addressed as A0 A1 A2 A4 etc.

Tanks a lot ... That's the problem... All works well now!!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you're welcome!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm confused, I thought if you were using hardware serial, it was not recommended to use digital pins 0 and 1 as GPIO.

Code:
#define sensorPin3 1
#define sensorPin4 0
...
pinMode(sensorPin3, INPUT);
pinMode(sensorPin4, INPUT);

What happens to sensorPin3 when the first Serial.print() is executed?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


depends if the
pinMode(sensorPin3, INPUT);
is done before or after the serial.begin()

Note that the OP nowhere uses the Serial in its sketch so his code is OK in that sense.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


depends if the
pinMode(sensorPin3, INPUT);
is done before or after the serial.begin()

Note that the OP nowhere uses the Serial in its sketch so his code is OK in that sense.


So you're saying that if this case
Code:
case 5:
    Serial.print("Valeur sensor #1: ");
    Serial.println(avg1);
    Serial.println();
    Serial.print("Valeur sensor #2: ");
    Serial.println(avg2);
    Serial.println();
    Serial.print("Valeur sensor #3: ");
    Serial.println(avg3);
    Serial.println();
    Serial.print("Valeur sensor #4: ");
    Serial.println(avg4);
    Serial.println("-----------------------");
     Serial.print("Valeur moyenne de tout les sensors: ");
    Serial.println((avg1+avg2+avg3+avg4)/4);
    Serial.println();
    Serial.println();
    currentsensor=1;
    break;
is never executed, it's all good? Seems kind of iffy to me.
Logged

Pages: [1] 2 3   Go Up
Jump to: