smart plant pot

Hi all.

First of all, I am new to coding, I know it's not tidy or very logic for that matter but I am trying. I am an old boy ish so can handle criticism. Please dont baffle me with advanced coding and tricks, I am looking for help getting the basic idea up and running then develope from there.

PLAN.

2 litre resevoir on the wall. plant pot has moisture sensor. Tank will fill up on demand based on moisture levels, using a solenoid for inlet and peristaltic pumps will add nutrients. Resevoir uses like a float switch to cut off when full. Once full, the feed will be delivered in short bursts using a submersible pump in the res over set time, taking further readings as it goes to avoid flooding or over feeding. Once i get 1 going I plan to scale it up to 8 pots but thats another day, one step at a time.

code is below. It all works at this simple level, what I am struggling to do, don't laugh.

I was trying to use my float switch in the resevoir 'tankfullsw' when LOW (empty), to switch 'insolenoid' on until full. Then off again when float swich 'tankfullsw' is HIGH.

I am basically struggling with command parameters and decoding the errors they throw up. Any help appreciated.

int sensor1Pin = A1; // sensor1
int sensor2Pin = A0; // sensor2
int sensor1Value = 0; // variable to store the value coming from the sensor
int sensor2Value = 0;
int insolenoid = 3; // control pin 3 for water inlet solenoid.
int tankfullsw = 4; //pin 4 for float switch in resevoir
int tankfullswstate=0; // flag to indicate if resevoir is full 0=empty 1=full

void setup() {

Serial.begin(9600);
pinMode(insolenoid, OUTPUT);
pinMode(tankfullsw, INPUT);
digitalWrite(insolenoid,LOW);
digitalWrite(tankfullsw,HIGH);
}

void loop() {
// read the value from the sensors
sensor1Value = analogRead(sensor1Pin);
sensor2Value = analogRead(sensor2Pin);
digitalRead(tankfullsw);
delay(3000);
if (tankfullswstate=1)
{
digitalWrite(insolenoid,HIGH);
}
else if (tankfullswstate=0)
{
digitalWrite(insolenoid,LOW);
}

Serial.print("sensor1 = " );
Serial.print(sensor1Value);
if (sensor1Value>601) //600 is threshold for feeding (
{
Serial.println(" needs feeding");
} else{
Serial.println( " all good");
}
Serial.print("sensor2 = " );
Serial.print(sensor2Value);
if (sensor2Value>601)
{
Serial.println(" needs feeding");
}
else
{
Serial.println(" all good");
}
delay(5000);
}

  digitalRead(tankfullsw);

This reads the state of the tankfullsw and throws the result away.

Presumably you meant to use the value it returns as in:

  bool tankfullswstate = digitalRead(tankfullsw);
if (tankfullswstate=1)

This sets tankfullswstate to 1. You meant to compare it to 1 with the == operator

if (tankfullswstate == 1)

or better:

if (tankfullswstate)

since HIGH or 1 or true are all evaluated as true in a conditional.

thanx for your help. Got this level working as I want, but I had to reset 'tankfullsw' to LOW at the end of the loop.... Why doesnt the switch 'tankfullsw' read LOW again when I switch it manually and it is re-read in next cycle.

Ammended code

int sensor1Pin = A1; // sensor1
int sensor2Pin = A0; // sensor2
int sensor1Value = 0; // variable to store the value coming from the sensor
int sensor2Value = 0;
int insolenoid = 3; // control pin 3 for water inlet solenoid.
int tankfullsw = 4; //pin 4 for float switch in resevoir
int tankfullswstate=0; // flag to indicate if resevoir is full 0=empty 1=full

void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
pinMode(insolenoid, OUTPUT);
pinMode(tankfullsw, INPUT);
digitalWrite(insolenoid,LOW);
digitalWrite(tankfullsw,LOW);
}

void loop() {
// read the value from the sensors
sensor1Value = analogRead(sensor1Pin);
sensor2Value = analogRead(sensor2Pin);
// int tankfullswstate=digitalRead(tankfullsw);
bool tankfullswstate = digitalRead(tankfullsw);
delay(3000);

Serial.println(tankfullswstate);
Serial.println(insolenoid);
Serial.println(tankfullsw);
if (tankfullswstate==1)
{
digitalWrite(insolenoid,HIGH);
}
else if (tankfullswstate==0)
{
digitalWrite(insolenoid,LOW);
}

Serial.print("sensor1 = " );
Serial.print(sensor1Value);
if (sensor1Value>601) //600 is threshold for feeding (
{
Serial.println(" needs feeding");
} else{
Serial.println( " all good");
}
Serial.print("sensor2 = " );
Serial.print(sensor2Value);
if (sensor2Value>601)
{
Serial.println(" needs feeding");
}
else
{
Serial.println(" all good");
Serial.println(tankfullswstate);
}
delay(5000);
digitalWrite(tankfullsw,LOW);
}

TonyTigro:
(...)
Why doesnt the switch 'tankfullsw' read LOW again when I switch it manually and it is re-read in next cycle.
(...)

Because you need a pulldown resistor. Take a look to this.

luisilva:
Because you need a pulldown resistor. Take a look to this.

Cheers mate, gutted though, had _PULLUP in my sights when i was fault finding but didn't cure the problem so moved on. Worked a treat, thanx again.

Next......

Another issue i have now is a clock. I have an RTC shield to gain some accuracy with regards peristaltic pumps and nutrient dosage. The pump delivers accurately depending on V and time. Would a software clock be any good here, i fugured not, actions within a loop must affect looping time and clock accuracy. Loads of examples out there but its hard to know whats best for me as am learning as I go. I am looking for something small and simple which is easily intergrated into my code. I am using 'Real Time Clock Module I2C RTC DS1307 AT24C32' D

Thanx

To be clearer and not appear lazy, as a relative noob I am seeing alot of stuff which if I knew earlier, I would have done things differently. I don't want to chose an example that will prove useless half way through my project. i.e I will be using timers for pumps and lights via the clock, are timers easily introduced? I will be using wireless comms between units, are there syncing issues. I am not lazy or thick, just looking for the right place to start.

TonyTigro:
Another issue i have now is a clock.

The biggest issue you have seems to be the sensor reading. From the code you posted, I suppose that you are using cheap 'resistive' moisture sensors?

// read the value from the sensors
   sensor1Value = analogRead(sensor1Pin);    
  sensor2Value = analogRead(sensor2Pin);

Is that correct? What type of sensors are you using?

Your sensors seem to be powered ON all the time, and you then read them in intervals.

If your sensors are cheap Chinese 'resistive' moisture sensors, you will ruin your moisture sensors within a couple of days by doing that. The metal on your sensors will corrode fast, if the voltage is "always on" at the sensors.

For resistive moisture sensors, you will need to provide voltage to the sensors only for a short time during measuring:

  • power on voltage to sensor
  • let the voltage settle for a few milliseconds
  • measure sensor value with analogRead()
  • power off voltage
  • do nothing for 4 hours or so, then repeat

Your plants will not die of thirst if you check every 4 hours for their watering needs.

But your sensors will be destroyed within one week of operating time in damp soil, while you provide voltage to cheap resistive moisture sensors all the time.

See: Galvanic corrosion - Wikipedia

jurs:
The biggest issue you have seems to be the sensor reading. From the code you posted, I suppose that you are using cheap 'resistive' moisture sensors?

// read the value from the sensors

sensor1Value = analogRead(sensor1Pin);   
  sensor2Value = analogRead(sensor2Pin);




Is that correct? What type of sensors are you using?

Your sensors seem to be powered ON all the time, and you then read them in intervals.

If your sensors are cheap Chinese 'resistive' moisture sensors, you will ruin your moisture sensors within a couple of days by doing that. The metal on your sensors will corrode fast, if the voltage is "always on" at the sensors.

For resistive moisture sensors, you will need to provide voltage to the sensors only for a short time during measuring:
- power on voltage to sensor
- let the voltage settle for a few milliseconds
- measure sensor value with analogRead()
- power off voltage
- do nothing for 4 hours or so, then repeat

Your plants will not die of thirst if you check every 4 hours for their watering needs.

But your sensors will be destroyed within one week of operating time in damp soil, while you provide voltage to cheap resistive moisture sensors all the time.

See: https://en.wikipedia.org/wiki/Galvanic_corrosion

Hello mate and cheers for jumping in. Yes they are cheap chinese sensors and yes they are on all the time. I knew that would be an issue but not so quick. They are cheap enough to replace regularly but not every few days. What seems to starts as a simple project quickly snowballs lol. Lookin at that was a future tweak but i think you have given me enough to work with now. cheers for that.

Do you mean to use a digital pin for 5v supply to sensors rather than hard wire

TonyTigro:
Do you mean to use a digital pin for 5v supply to sensors rather than hard wire

Yes, that should do the job.

Do you have a digital multimeter which use can be used to measure small milli ampere currents?

First I'd measure the maximum current the sensor draws under a worst case scenario from the voltage pin. Perhaps measure in dry air and in superwet fertilized soil and take the higher value.

If it is less than 20 mA, you can use a digital pin, set to OUTPUT and HIGH to power your sensor.

If the current consumption is much less than 20 mA, you can even use one digital pin to power multiple sensors. Let's say the maximum current is 4 mA, then you could connect up to 5 sensors to one digital "power pin".

I absolutely don't know how much current those sensors need.

jurs:
Yes, that should do the job.

Do you have a digital multimeter which use can be used to measure small milli ampere currents?

First I'd measure the maximum current the sensor draws under a worst case scenario from the voltage pin. Perhaps measure in dry air and in superwet fertilized soil and take the higher value.

If it is less than 20 mA, you can use a digital pin, set to OUTPUT and HIGH to power your sensor.

If the current consumption is much less than 20 mA, you can even use one digital pin to power multiple sensors. Let's say the maximum current is 4 mA, then you could connect up to 5 sensors to one digital "power pin".

I absolutely don't know how much current those sensors need.

I don't have a multimeter, i do but my son claimed the probes.

All set up on D5(sensorpowerstate) and works a treat manually. putting a time on it is way above my current level. software or RTC?

TonyTigro:
Would a software clock be any good here, i fugured not, actions within a loop must affect looping time and clock accuracy.

You figured wrong. There is an on chip timer that runs independently from your code. You can utilize it with the timer functions that include millis() and delay().

cheers for that fella. So the RTC was a waste of money? Why do people both using an RTC?
good guides / tutorials around clocks and timers? the parameters i use just spews top end technical shit which may as well be in chinese.

TonyTigro:
cheers for that fella. So the RTC was a waste of money? Why do people both using an RTC?

A RTC is needed, if you want to have everything done at the same time of the day each day.

So if you want to have the soil measured and the plants watered each day at 00:00, 04:00, 08:00, 12:00, 16:00, 20:00, then you need a RTC.

If you don't mind about the time, and you power on your Arduino at 15:43, and the measuring/watering then is 15:43, 19:44, 23:45, 03:46, 07:47, 11:48 initially, and one week later perhaps at 16:13, 20:14, 00:15, 04:16, 08:17, 12:18 and the time is always be a bit off, while it is "roughly 4 hours in between", then you need no RTC.

The Arduino board itself can measure "relative time" fairly well (but not as accurate as a RTC). And a RTC can measure "absolute time" quite accurately (accuracy depending on type of RTC).

peristaltic pumps, nutrient dosage... Can I ask you what is your project? I think I've tried to do something like that in the past but I never finish it.

TonyTigro:
So the RTC was a waste of money?

If it was a DS1307 which can't keep time any better than a dollar store watch, yes. :slight_smile:

jurs:
A RTC is needed, if you want to have everything done at the same time of the day each day.

So if you want to have the soil measured and the plants watered each day at 00:00, 04:00, 08:00, 12:00, 16:00, 20:00, then you need a RTC.

If you don't mind about the time, and you power on your Arduino at 15:43, and the measuring/watering then is 15:43, 19:44, 23:45, 03:46, 07:47, 11:48 initially, and one week later perhaps at 16:13, 20:14, 00:15, 04:16, 08:17, 12:18 and the time is always be a bit off, while it is "roughly 4 hours in between", then you need no RTC.

The Arduino board itself can measure "relative time" fairly well (but not as accurate as a RTC). And a RTC can measure "absolute time" quite accurately (accuracy depending on type of RTC).

I have 3 tears of power so to speak.

  1. permanent live for extractors and heaters. (24 hr climate control)
  2. mechanically timed, lights and extractors.
  3. arduino controlled. (sensors and feeding)

So I can use either power source, meaning any absolute time issues can be handled by other controllers ie mechanical timer. Monitoring and feeding is always during lights on, so i don't need a rolex when my sun dial is plenty. I will be putting my RTC away for another project.

Cheers for that mate, sometimes ya can't see the wood for the tree's.

luisilva:
peristaltic pumps, nutrient dosage... Can I ask you what is your project? I think I've tried to do something like that in the past but I never finish it.

If u haven't guessed already... hydroponics. Short term goal is automated feeding, longterm goal is a kind of self contained smart plant pot.

aarg:
If it was a DS1307 which can't keep time any better than a dollar store watch, yes. :slight_smile:

We live and learn my friend. That is what i love about arduino, u can buy a load of shit for nearly nothing that allows ya to play, tinker and learn with no big cost. my kids love it, show them a £2 laser unit and they are in like ferret up a rat hole. I digress, I apologise.

I decided on a change of plan. Automating everything from 1 arduino is asking for trouble with regards to catastrophic failure if that 1 arduino goes down. I am now going to allocate an ardiino to each aspect of the project then each a spect to communicate to a central arduino where i can corrolate and report status from. Hooefully this way I can fault find alot quicker and easier without the whole system going down.

New Plan.

Independent systems

Tank filling and neutrient addition
feed allocation and delivery (6 pots)
moisture monitoring and feed request.
Control centre

I have half a dozen NRF24L01 wireless units, I will overload if I try to get my head round setting up wireless comms, so would greatly appreciate a bare bones sketch that i can tweak and run the above.

Any ideas of a safer ,easier or more reliable way would also be appreciated.