Sei sulla pagina 1di 28

technology

workshop

living

food

play

outside

Arduino for Greenhouse, Garden or Growbox


by diy_bloke on May 11, 2012

Table of Contents
Arduino for Greenhouse, Garden or Growbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Intro: Arduino for Greenhouse, Garden or Growbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 1: Arduino for Garden, Greenhouse or Growbox: inputs and outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 2: Arduino for Greenhouse, Garden or Growbox: PSU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 3: Arduino for Garden, Greenhouse or Growbox: Humiditity and temperature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 4: Arduino for Greenhouse, Garden or Growbox: LDR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 5: Arduino for Garden, Greenhouse or Growbox: Soil humidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 6: Alternative Soil resistance measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 7: Arduino for Greenhouse, Garden or Growbox: the level/float switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 8: Arduino for Greenhouse, Garden or Growbox: an alternative level switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10


Step 9: The Waterreservoir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Step 10: Arduino for Greenhouse, Garden or Growbox: an alternative float switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Step 11: Arduino for Garden, Greenhouse or Growbox: Buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Step 12: Arduino for Greenhouse, Garden or Growbox: Solid State Relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Step 13: Arduino for Greenhouse, Garden or Growbox: The LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Step 14: Arduino for Greenhouse, Garden or Growbox: Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Step 15: Arduino for Greenhouse, Garden or Growbox: The Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Step 16: Arduino for Greenhouse, Garden or Growbox: The Software: code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Step 17: Arduino for Greenhouse, Garden or Growbox: The Software: special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Step 18: Arduino for Greenhouse, Garden or Growbox: possible expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Step 19: Updated software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Step 20: Using timers to read sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Author:diy_bloke
I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I like. Other than my hobbies that involves
grassroots medicine in S.E.&P Asia. I have built low income cow dung bio-reactors, solar lamps and family fish ponds as well as houses out of every kind of
thinkable material. Instructables is an endless source of inspiration for my projects.

Intro: Arduino for Greenhouse, Garden or Growbox


I have been using Attiny chips for irrigation tasks in my garden, but having plans to build a greenhouse, an Arduino seemed to be the way to go as it has more ports. I
know, there are many 'Garduino' type projects already, including ones that twitter the state of your plants, but I just wanted something more basic, so, what does an anal
retentive Asperger do in such a case: build something myself.
No use of course to spend 23-25 euro's for an Arduino Uno and then still have to add a shield for peripherals: seemed a bit overdone and as I have built various
'arduino's' alreday, I decided to just get the chip, a crystal, and 2 capacitors + some more stuff and build the entire thing on an Archer experiment board that I bought 20
years ago but has been idle all that time (radioshack partno 276-168). The Archer experiment board has islands with 3 holes and 2 seperate tracks (Vcc and Gnd)
snaking in between. Of course it can also be done on regular strip board, perfboard or one can make a PCB for it.
In the end I decided I needed to have the following functions
1.
2.
3.
4.
5.
6.
7.
8.
9.

measure temperature
measure humidity of the air
measure humidity of the soil
measure water level in a container
measure light, basically to determine if it is light or day
switch a fan
switch a pump
switch a heater
switch a lamp

List of materials
Processor
Atmega 328 here to burn bootloader yourself or buy one preprogrammed
1x 28 pins IC foot narrow (or 2x 14 pins ic foot)
16MHz Crystal
2x 22 nF (often carries '223' imprint)
1x 10 k (brown-black-orange)
1x 100nF (often carries '104' imprint)
1x PCB momentary make switch
perfboard This is what I used because I had that laying around idle for years already
NOTE:an attractive alternative is a cheap arduino clone such as the Pro mini or Pro Micro that can be had for a few dollars at e.g. Dealextreme.com
Display
16x2LCD CG046-3007 A00, but any other will do.
1602 I2C module
PSU
1x 7805, isolated
1x 1N4001
2x 100uF (16 V en 25 Volt)
1x 1 k resistor (brown-black-red)
1x LED
1x psu connector
Peripherals
1x LDR
1x DHT11 moisture sensor (e.g. from dealextreme)
1x make push button
1x throw switch
1x buzzer (I use the CMB-06, a triggerable buzzer) ($2.99) but use any other buzzer you have or are comfortable with e.g. this one
1x NPN transistor (e.g.BC547)
1x 330 Ohm (orange-orange-brown)
2x 10k (Brown-black-red)
Solidstate relays
4x330 Ohm (orange-orange-brown)
4xLED
4x8pin dil foot
4x39MF22 SSR
4x2pin screw connector (mains voltage)
1x 5pin female printheader
1x 5pin male header 90degrees angle
ALTERNATIVELY: buy a ready made 4 channel mechanical relay for just about the same price as loose parts for the SSR, such as this one, or an even cheaper one.
bits and bolts
a couple of different colour wires, some print headers, four 3mm bolts and nuts, some 2.5 mm bolts and nuts, 4 spacers
solder tin, solder iron.
I have used Cinch plugs for connection... but actually these are hard to solder.
casing
I use two casings:
one for the processor and most of the peripherals (I use a plastic box that contained screws: 9x12x4.5 cm)
one for the SSR's (I used a Tic-Tac peppermint dispenser The bigger one for 100 mints: 8.5x2x5 cm)

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Image Notes
1. indicating an empty reservoir

Step 1: Arduino for Garden, Greenhouse or Growbox: inputs and outputs


Before I describe the construction I will discuss the individual items:
Processor
An Atmel 328 with an Arduino bootloader is used. That makes for easy adaptation and uploading of the program. It will be a "bare bones" setup with no USB entrance,
but simply with the TTL Rx and Tx signals on a header. Consider using a cheap Arduino pro mini, pro micro or Arduino nano
PSU
Simply a 7805 circuit fed from a wallwart. No 3.3 Volts will be necessary
Humidity and temperature sensor
A DHT11 sensor will provide that. It is a 3 pin sensor of which the data can easily be read by the Arduino. There is a library available.
Switch
Simply read as an input to set various functions in the software
Light
An LDR read by an analogue port. It basically is there to determine if it is day or night. This allows for extra light when the days are too short or to perform certain
functions different at day or might(such as different day and night temperature, no irrigation at night etc)
Soil Moisture sensor
This sensor, read by an analogue port will give information about the humidity of the soil and signal the processor it is time to irrigate. There is an entire science on DIY
soil moisture sensors. I will just be using the classic '2 nails' approach. The software and hardware will allow for the current through the sensor to be switched off, thus
delaying corrosion.
Level sensor
This sensor checks if the water reservoir is still full. A simple float switch will suffice
Buzzer
used to give signals, e.g. if the water reservoir is empty. I use an old CMB-06 that has a trigger, meaning it can be triggered with a small current while it can get its main
current directly from the power supply lines.
Solid state relays
I use 4 solid state relays to switch a fan, a pump, a heater and the lights. The solid state relays are separated from the processor print out of safety precautions.

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 2: Arduino for Greenhouse, Garden or Growbox: PSU


The PSU is a standard circuit build around a 7805. People that know their way around in electronics will probably have no problems with it. For the less experienced. I
made a somewhat more extensive drawing.
With regard to the polarity of the LED: normally LED's have 2 legs: a short one and a long one. The short one is usually the cathode that connects to ground. The long leg
is the anode that connects to the positive voltage.
With the 1k series resistor, the LED approximately receives 3.5 mA, whereas LED's can take some 15 mA. You'll see that most LED's are already pretty bright with a 1k
series resistor

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 3: Arduino for Garden, Greenhouse or Growbox: Humiditity and temperature


Fo rmeasuring of the humidity and temperature I used a cheap DHT11 module, It comes in various forms but though the bare module has 4 legs, it usually comes on a
small PCB with only 3 pins, or 4 pins in which one is not connected. There are various libraries for the DHT11 to be used by the arduino. A good library however is the
one from LadyAda.
Usually it is pretty well indicated what the supply lines and the signal lines are.
As some libraries presume the signal line is connected to arduino digital pin 2, that is what I have followed in this design
Note: after using it for a while, i found the DHT11 rather unreliable. The temperature can fluctuate 4 degrees between two subsequent reaadings and has a tendency to
be too high. The DHT22 is a more accurate Sensor (so I am told),but it is more expensive.
i do not know how accurate the DHT11 is with regard to the humidity... but you could opt to keep the DHT11 but add a BMP180 for more accurate temperature. That
gives you atmospheric pressure as well. It is an I2C sensor so it is easy to add.

Step 4: Arduino for Greenhouse, Garden or Growbox: LDR


I use an LDR to determine the light level. The LDR forms a voltage divider together with a 15 k resistor. When it gets dark, the resistance of the LDR goes up and thus the
voltage over the 15 k resistor goes down. When it is light, the resistance of the LDR is low and therefore the voltage over the 15 k resistor is high.
LDR's generally have a vary wide range. The one I have is around 300 k in the dark and 300 ohm in daylight. As I only am interested in making a crude light, twilight,
night reading, I determined the resistance of the LDR in what I called twilight. That was 15 k. Therefore, chosing a 15 k resistor in the voltage divider gives the broadest
range.
I inserted the LDR in a clear casing (a pill bottle) and connected it via a thin wire to the arduino Vcc and analog pin 1 (that is pin 21 on an Atmega 328). The 15 k resistor
is on the circuit board.
A good way to determine the value for the pull down resistor is the Axel Benz formula.
As we are only interested in a night and day difference, the pull up resistor is not that critical

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 5: Arduino for Garden, Greenhouse or Growbox: Soil humidity


Soil humidity is generally measured by reading the resistance between two pins in the soil. Generally, two types of sensors are used: two nails or pins stuck in the earth
or 2 rods encased in gypsum, put in the soil.
Both methods have their pro's and cons. The (galvanized) nails may be put in the soil at various distances, influencing the resistance, however, they are simple and
cheap and react quick to changes in the humidity. The gypsum method makes sure the rods have a fixed distance, but the reaction to changes in humidity are slower.
Both types are subject to corrosion in the soil, a process that is sped up by the electrolysis because of the current send through the sensor, however, the gypsum
encased rods are less prone to corrosion because they are not directly exposed to the soil.
Though bare rods probably will have to be replaced every year because of corrosion, the gypsum sensor will also need to be replaced because of dissolving of the
gypsum.
The corrosion of the rods can be delayed by limiting the electrolysis by only sending a current through the rods when measuring.
That is what I have chosen for in this design: The transistor is opened by a high level from a digital pin. The emmitter is then pulled high (about 4.7 Volts). This voltage is
then fed to the sensor (the two pins in the soil) and the voltage over the 10 k resistor is then read by an analogue pin. The transistor is basically any type of cheap NPN
signal transistor such as a BC107 or a BC547. I used a VN147 a 30 year old 'universal NPN' transistor, for no other reason that I had that laying around and I kinda like
the triangle shape it has
The sensor and the resistor in fact form a regular voltage divider in which the voltage over R2 gives an indication of the resistance of the sensor and thus the humidity of
the soil.
Resistance of the soil does not only vary with humidity but also with the type of soil. Generally, my soil in moist conditions renders a resistance of around 10 k between
the sensor pins, but even if the pins would be shorted, the maximum current will be about 0.5 mA. It is therefore possible to leave out the resistor and feed the sensor

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

directly from a digital pin (capable of delivering 40 mA), but I just feel happier having some protection of the arduino pins.
The 5V1 zenerdiode between the analogue pin and the earth is to protect the chip against high voltages that may build up if a long line to the probe is used. It is optional.
The software has a routine that will read the voltage over the 10k resistor 5 times and then calculate an average.
Some tech stuff about soil humidity
When using a measurement like the one here, you are measuring resistance, or conductivity, depending on how you want to look upon it.
Conductivity is in fact not the best way to measure humidity of the soil as water itself is not really a good conductor, what you are actually measuring is the amount of
dissolved ions in your plant-bed. A not so moist soil with lots of ions may give you a higher reading for conductivity than a wet soil with less ions.
Measuring your ions of course is a useful thing too as it gives information on whether your soil is depleted and if maybe it is time to add fertilizer.
If you want a more reliable measurement of the humidity of your soil, then a capacitive measurement is more reliable: water influences the dielectric of a capacitor much
more than ions do.
A capacitor in your soil sounds like having two metal plates in your soil in which the soil in between them is the dielectric. That is certainly possible, but it is also possible
to have the plates of the capacitor next to each other instead of facing each other. That opens the way to etch them on a piece of PCB. Obviously the plates are isolated
from the soil, otherwise you would just be measuring conductivity/resistance again. That has the added bonus that there will be no soil corrosion.
So once you have that set up, there are two ways of measuring: you could just measure the capacity of your diy soil capacitor, as the arduino is perfectly capable of doing
that. The problem though is if you have a cable going from that sensor to your arduino, that is in fact a stray capacity that you are measuring as well. The way your cable
is looped may influence your reading as well as someone approaching your cable. Using coax will help, but it still is best to do the measurement close to your diy
capacitor.
Another way is to use that capacitor in a circuit whose output signal is depending on the value of your capacitor, such as a capacitor controlled oscillator. This can be
done with a Schmitt trigger as is shown in the picture above.
The frequency of that oscillator is:
f=1.2/(RxC)
A changing amount of water in the soil will affect the dielectric of the capacitor and therefore the value of the capacitor and thus the frequency coming out of the oscillator.
This frequency can be measured by the arduino with the frequency measuring library of the Arduino.
The value of the resistor depends on your situation. choose one that gives a good range of your frequency. The Arduino library supports frequncies to 8MHz.
If your capacitor is 10nF and your resistor 100kOhm, the frequency would be 1200 Hz. You will find a calculator here.
The FreqCounter.cpp file is hardcoded for digital pin 5 as input and uses both timer interrupts T1 and T2
Should you choose to use capacative measuring of the humidity, instead of using the various frequency counter libraries, you can also use 'PulseIn'. An example function
of how to do that can be found in the next step.

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 6: Alternative Soil resistance measurement


In the previous page i have spoken about the pro's and cons of measuring soil humidity, either as a resistance measurement or as a capacitive measurement.
With regard to resistive measurement, using DC with the soil probes as a voltage divider feeding into an analog port is the simplest solution, however, pushing a dc
current through the probes will lead to rapid detoriation as a result of electrolysis.
One can counter that by only putting power on the probes at the moment of measuring and limit the mesuring to say a few times per day.
Another option however is to use AC.
The presented circuit provides that. The output signal is a frequency that depends on the resistance of the sensor.
The + power input can be as low as 3.5 volts and as high as 15 volts, so the 5 Volt of the Arduino is perfect. The CMOS LMC555 timer operates in its direct feedback
mode, with a square wave output from pin 3 charging or discharging the 0.1 f film capacitor through the fixed resistors in series/parallel with the conductive sensing
probes.
The 2.2 uf non-polar capacitors in series with the probes assure that even the low level leakage currents from the CMOS chip do not flow through the probes. The
capacitors, one being on each side of the probes, also assure that this circuit is galvanically isolated from other sensors that might be in the same aqueous environment.
R1 and R2 limit the range in case of a short circuit or extremely dry soil. R2 also has another function, as it discharges any slight DC build up over the capacitors in series
with the probe that could make the oscillator stop.
The output frequency is transmitted from the open collector DIS output pin on the '555. This pin is in sync with the Output and will produce a square wave whose
frequency/pulsewidth can be measured by the Arduino (e.g. through a timer interrupt or through 'PulseIn'). R3 is necessary since the DIS (discharge) pin of the 555 is an
open collector.
As the current drawn by the 555 timer circuit varies linearly in proportion to the frequency of oscillation due to the charge and discharge cycles of the timing capacitor, the
supply current can be used as an analog signal, to send to an analog to digital converter. Therefore a 1k resistor can be put in one of the supply lines (typically the Grnd)
The current develops a voltage across the 1kohm resistor, to give a voltage signal if desired.
The picture above shows a function to measure the frequency (in fact the pulsewidth). The program below does the same.
I use value of 500 000 uS instead of 1 000 000 uS (= 1 second) for the division, because I want to get signal's period (T), not pulse length(tpulse). With duty cycle of 50%,
T = 2*tpulse. However, unless you are interested in the true frequency, it doesnt really matter for determining the soil moisture
int pin = 7;
unsigned long duration;
void setup()
{
pinMode(pin, INPUT);
Serial.begin(115200);
}
void loop()
{
duration = pulseIn(pin, HIGH);
Serial.print("Time ");
Serial.print(duration);
Serial.print(" usec ");
Serial.print(500/duration);
Serial.print(" kHz ");
Serial.print(500000/duration);
Serial.println(" Hz");
delay(500);
}

Of course the above program is only a proof of concept/test program: we are not interested in specific frequencies but in differences in frequencies/periods. You need to
determine for your soil what the right timing/frequency is for it to need water or not and incorporate that in your program the same way as previously you would have done
so fr the voltage divider probe on an anolog port. It is still possible to use the potmeter on A1 to set a level, e.g. by mapping it to your frequency range, or with some If or
Case statements.
To give an impression, when i tested it in my system (probes with about 6 cm distance, in so called 'Mel's Mix') a somewhat moist reading was 3kHz, while a wet soil
gave 10kHz

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Image Notes
1. Reading of the probes in wet soil. This static pic suggests a stability that isnt
there, other readings might have been 9985

Step 7: Arduino for Greenhouse, Garden or Growbox: the level/float switch


I have added a level switch to check if the water reservoir is still full. This is not only necessary for the well being of the plants but also for the well being of the pump as
submersible water-pumps should not be left to run dry.
There are many ways in which one can construct a level switch: a magnet on a float that activates a reed switch, a tlt switch attached to a lever that goes up and down
with the water level, 'feelers' in the water etc.
I have chosen a very simple approach. I have a rubber-duck floating in the water. That duck has a string attached that operates a micro switch that hangs over the top of
the container. If the water goes down, so goes the ducky intil it pulls the switch. That switch is read by the Arduino that then switches off the pump and sends a signal to
the buzzer.
The switch is connected to an ordinary digital I/O pin. perhaps it would be more elegant to attach it to an interrupt pin, but as the Arduino is not really held up in all sorts of
routines, reading a regular I/O pin should not be much delayed.
In place of a rubber duck one can also take a piece of wood. Anything that floats, but is heavy enough to operate the switch. For the string I advise nylon fishing string

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Image Notes
1. the leverswitch has a nylon string going through the hole in the wood and is
attached to a floating device
2. Cover for the switch

Step 8: Arduino for Greenhouse, Garden or Growbox: an alternative level switch


If you do not like a mechanical switch to signal the water-reservoir to be full, thee is a simple electronic solution:
Hang two electrodes at the level of the water that you want to signal at. They may come up higher but not lower. Instead of two loose electrodes one could also use a
piece of PCB with two tracks etched on it.
Feed those two electrodes to the circuit as depicted here.
As long as the water is in contact with the electrodes, the transistor will receive a base current and open up. The collector will be pulled low and the arduino pin can detect
that.
When the water-level falls below the electrodes, the transistor will not receive a base current anymore and close. The collector will therefore be high.
Mind you the Atmega 328 I/O pins have an internal pull up resistor and therefore a pull up transistor is not necessary in this circuit. To be sure though a 15k or 22k pull up
resistor can be added between the collector and the +5 Volt line.
If you are short on I/O pins you could save a pin by using T1 not just to signal a low level to the processor, but also to directly steer the buzzer, or even to operate a Relay
to switch off the pump, saving another I/O pin. But then again, most of the functions could probably be build with discrete components and the Atmega 328 has enough
I/Opins for this project

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 9: The Waterreservoir


I use an 86 liter cement bucket as a waterreservoir.
I have that dug into the soil to make sure it is lower than the plantbed. If it is higher than the plantbed, there is achance that yr pump, after it has been switched off, will
start siphoning, emptying the entire bucket in your plantbed.
If for whatever reason your waterreservoir has to be higher than your plantbed, you need to install a vacuum release.

Step 10: Arduino for Greenhouse, Garden or Growbox: an alternative float switch
Determining whether the tank is empty is also possible with a reed switch as the figure shows: When the level drops to a specific amount, the magnet on the plunger will
close the switch and that will be signalling the Arduino that the pump should not be operated any more.
The disadvantage of this circuit is that eventually the reed switch will magnetize and go to a permanent closed position.Replacing the relatively cheap reed switch of
course will remedy that.

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 11: Arduino for Garden, Greenhouse or Growbox: Buzzer


I added a buzzer for signalling functions (mainly to signal that the water reservoir would be empty). I have used the CMB-06 buzzer from Star. My main consideration was
that I had that buzzer laying around for years already. It is a very handy buzzer though because it is triggerable. The advantage of that is that I do not need an arduino pin
to deliver the entire current needed for the audio signal, just some current for the trigger. Therefore I do not have to add a transistor to drive the Buzzer.
The + and - are conveniently connected to the power supply and the 'C' (=control) pin is connected via a 1k resistor to an arduino pin (in fact pin 9, that has PWM)
The maximum average (!) current through the buzzer is stated as 23 mA, which could be delivered by an Arduino pin, but the data sheet requires a current source of
about 70 mA for peaks. That is above what an arduino can safely deliver. Feeding it directly from the power line and only the trigger current being delivered from an
arduino pin solves that problem.

Step 12: Arduino for Greenhouse, Garden or Growbox: Solid State Relay
The 4 channel solid state relay is used to switch things like grow lights, a water pump a fan and a heater if necessary. It is almost a separate (albeit simple) project.
The circuit needs to be build 4 times and I have a print design available.
The circuit uses a 39MF22 solid state relay that simply needs a series resistor to limit the current. An LED shows whether the relay is active.
Because it is connected to 220 Volts, I have constructed it on a separate PCB and it is in a different case.
The 4 channels are controlled by digital pins 5,6,7 and 8 (Those are physicals pins 11-14 on the Atmega 328)
Inductive loads like the fan and the pump, may need a snubber network (100 Ohm resistor and 100nF capacitor in series over the connection). As these inductive loads
are only low power I have left them out and have not experienced any problems.
This part is fully described in an other instructable.
.
Warning
This device connects to a mains voltage. That can kill you. If you are not acquainted with working with high voltage circuits then do not build this

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 13: Arduino for Greenhouse, Garden or Growbox: The LCD


Considering there are only so much I/O pins on the Atmega 328, I decided to add an LCD through an I2C interface
The library to use is the new LCD-library from Malpertida
The pin-out of the i2C interface is such that it will nicely fit most LCD's.
It is possible to build your own I2C interface but i would advise against it. The one at Dealextreme is so cheap that building it yourself would be more expensive. Just the
chip alone would probably already be more than the ca. 1.80 Euro, the entire ready built part costs at dealextreme.
I had considered to use Nokia 5110 display, but that needed some 4 or 5 data-pins. Surely I could use I2C either for that display or for the in and outputs, but by then I
was already too far ahead to rewire everything for I2C for outputs and using I2C for the Nokia LCD is something I hadnt figured out yet, but it is possible. The available
library uses SPI -which is the fastest mode of communication- but as it is hard coded you would need to do some rewriting. therefore I opted for the regular LCD.
Using a 5110 display over I2C may seriously slow down the screen build up to maybe a second. You will find some initial reading here

Image Notes
1. this jumper connects the backlight. ca be replaced by a female header attached
to a switch

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 14: Arduino for Greenhouse, Garden or Growbox: Construction


It is hard to give a description of the construction as a 'this is how you do it' as the way i constructed it depended on the material I had on hand: a slightly opaque plastic
box as a container and a piece of Archer PCB that after laying around for 10 years i finally wanted to put to some good use.
Therefore I can only give some general guidance.
I started with building the power supply followed by the Atmel 328 processor. The latter somewhat in the middle of the PCB so there would be space on both sides for
various components.
It is a good idea to check the workings of every part after you finish it, That way you avoid building the entire circuit and have no clue why it isnt working (if worse comes
to worst). So after i was done with the power supply, I checked it for 5 Volts. Then after I built the processor, I inserted a 328 chip that i had preprogrammed with a test
program, Added the interface for the relay/ssr and that worked . Wired up the FTDI tested it etc.
In the mean time I had prepared the the LCD by soldering the I2C piggyback to it and tested that on an Arduino UNO so I knew it worked. Then I put it in the box as well.
Initially I had thought to make the connection to the ssri/relay by running a cable through the box, but i decided I wanted a connector. As this needed to be a 5 prong
connector and I already had made a female sil connector on the relay, i just opted for making a male sil connector on the box, using a lot of hot glue.
Don't make the mistake I made by mounting the DHT11 sensor on top of the casing.... especially if the LCD backlight is on, the temperature in and around the casing can
rise, easily leading to a 2-5 degrees difference

Image Notes
1. to ssr/relay

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 15: Arduino for Greenhouse, Garden or Growbox: The Software


The Software is what is tailored to my situation and you may well decide you need to make changes. I tried commenting as much as possible. I am sure the code could
be simplified but this worked for me.
The most important elements are the following:
Libraries
Adafruit DHT library
Malpertida new LCD library
RTC library (optional, for adding a DS1307 RTC)
Declarations
The usual: pins, constants, variables etc
Definition of characters for the LCD
Setup
Setting up the PinModes and their statuses
Setting up the LCD, upload the user defined characters
Print a welcome text
Flash the LCD twice
Give a quick buzz
set the RTC (optional)
Loop
First check if the water reservoir is empty or not. This is to avoid that the pump runs dry
If it is empty, give a buzz
Then read the level for dry vs wet as set by the variable resistor
Then set a voltage on the soil humidity sensor
and read he soil humidity
//--------------------------//
Read the DHT11 sensor
Read the LDR
//------------------------//
Display data depending on the status of the Pushbutton
//------------ACTIONS-----------//
if the temperature is below 20 degrees turn on heating
if the Humidity is over 60 % turn on the fan
If below the level set by the variable resistor-> start the pump
One could alter the code to only irrigate when it is light
Functions
sample
Will read a sensor 5 times and map the average to an 8 bit value
flash
can flash the LCD backlight a number of times. can be used as an alarm function
buzz
can sound the buzzer for a number of times

Step 16: Arduino for Greenhouse, Garden or Growbox: The Software: code

/* measure soil moisture, if less than the set value switch pump
measure temperature, if less than set value, switch on heating
measure humidity, if higher than set value, switch on fan
<a href="http://www.quickgrow.com/gardening_articles/fans_air_movement.html"> <a rel="nofollow"> <a href="http://www.quickgrow.com/gardening_articles/fans_..
LCD commands:
lcd.setBacklight(LED_OFF);
lcd.noBacklight();
lcd.clear();
lcd.home();
*/
/*-----(
#include
#include
#include
#include

Import needed libraries )-----*/


<Wire.h>
<LiquidCrystal_I2C.h> //F. Malpertida
"RTClib.h"
//Adafruit
<dht.h> // Rob Tillaart <a href="http://arduino.cc/playground/Main/DHTLib"> http://www.quickgrow.com/gardening_articles/fans_...</a>

/*-----( Declare objects )-----*/


dht DHT;
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//
addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
/*-----(Declare RTC object )------*/
RTC_DS1307 RTC; //declare onject

// Set the LCD I2C address

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

/*-----( Declare pins )------*/


byte moisturePin=0; //read soil mositure
byte levelPin= 1; //analogue Pin1 set level for irrigation
byte humidPin=2; //Physical pin4 Humidity sensor
byte lightPin=5; //Switches on light
byte fanPin=6; //Switches fan
byte pumpPin =7; //Switches pump
byte hotPin= 8; // Switches heater
byte buzPin=9; // Switches buzzer
byte emptyPin=10; //digital Pin10 guards waterlevel
byte spikePin=12; //Digital Pin12
Extra for intermittent switching of spike
byte LDRPin=3;// analog pin for LDR
byte PushButton=4;
// PushButton
byte SwitchButton=3;// throw Switch
byte push=0;
byte sButton=0;
#define DHT11_PIN humidPin
/* Variable setting
*/
unsigned int moist=0;
//will contain the soil moisture level
unsigned int irrigate=0;
//sets level for irrigation in case no potmeter
byte level=0;
int c;// contains the temperature reading
// these constants won't change:
const int sensorMin = 40;
// LDR minimum, discovered through experiment
const int sensorMax = 1012;
// LDR maximum, discovered through experiment
byte light; // value for LDR reading
// Create a set of new characters
const uint8_t charBitmap[][8] = {
{
0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0

,
{
0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0

,
{
0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0

,
{
0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0

,
{
0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0

,
{
0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0

,
{
0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0

,
{
0x4,0x4,0x4, 0x4, 0x15, 0xE,0x4,0x0
}
};
/// -------- end creation-------------//The following function, "setup", must always be present
void setup()
{
Serial.begin(115200);
//upload defined characters to LCD
//---------------end upload---------------pinMode(levelPin,INPUT); // set level
pinMode(humidPin,INPUT); // measures humidity
pinMode(emptyPin,INPUT); // measures reservoir
pinMode(SwitchButton, INPUT);
// make Switch
pinMode(PushButton, INPUT);
// PushButton
pinMode(spikePin,OUTPUT); // for alternative supply to spikes
pinMode(pumpPin,OUTPUT); // Output for Relay
pinMode(fanPin, OUTPUT); // Output for fan
pinMode(hotPin, OUTPUT); // Output for heater
pinMode(lightPin, OUTPUT);// Output for light
pinMode(buzPin, OUTPUT); // Output for buzzer
digitalWrite(pumpPin, LOW);// Pump off
digitalWrite(spikePin, LOW);// moisture sensor off
digitalWrite(fanPin,LOW); // fan Off
digitalWrite(hotPin,LOW);
// heater off
digitalWrite(lightPin, LOW); // light Off
digitalWrite(buzPin, LOW); // buzzer off
/* Now LCD */
lcd.begin(16,2);
// initialize the lcd for 20 chars 4 lines, turn on backlight
int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
for ( int i = 0; i < charBitmapSize; i++ )
{
lcd.createChar ( i, (uint8_t *)charBitmap[i] );
}
lcd.backlight();
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Greenhouse");
// ------- Quick 2 blinks of backlight -------------

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

flash(2);
// ------- Quick buzz-------------------------------buzz(1);
Wire.begin(); //needed for RTC, not for LCD
RTC.begin();
/* Set the date / time to the time this program was compiled.
Comment this OUT, AND upload, to let the clock just run. */
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
/*----------------------------(end setup )---------------------*/
void loop()
{
DateTime now = RTC.now(); //Get the current data
// Serial.print("The year is ");
// Serial.print(now.year(), DEC);
// Serial.print(" Month = ");
// Serial.print(now.month(), DEC);
// Serial.print(" Day = ");
// Serial.print(now.day(), DEC);
// Serial.print(" Time = ");
// Serial.print(now.hour(), DEC);
// Serial.print(':');
// Serial.print(now.minute(), DEC);
// Serial.print(':');
// Serial.print(now.second(), DEC);
// Serial.println();
//------------end of RTC
// ---------- 1. Check if there is enough water ------------// check if there is water
level= digitalRead(emptyPin);
if (level==0) {
buzz(3);
delay(500);
}
//------------2. Read the soil moisture content/switch pump---------------/*
First read the level set with P1 on the levelPin and store that in 'irrigate'
*/
irrigate=sample(levelPin); //average of 5 readings
/*

Then we read the soil humidity sensor.


We'll first have to set the spikePin to HIGH, in case that is used to feed the sensor.
After the reading we set it back)
If the value read ('moist') is smaller (high resistance) than what is considered dry <br>
(='irrigate') then the pump should be switched on for a specifi
Flutter is prevented by indicating a slightly higher treshhold for switching the pump off
If however the waterreservoir is empty ('level == LOW'), the pump should not be on.<br>
To avoid a nested if loop, we write the value of 'level' to the p
but the waterreservoir empty, the pump will remain Off ->dead plants but pump still alive
*/
digitalWrite(spikePin, HIGH);// put voltage on the humidity sensor
delay(100); //wait a short while
moist=sample(moisturePin); //read soil humiditySensor, average of 5 readings
digitalWrite(spikePin, LOW);
if (moist <= irrigate) digitalWrite(pumpPin, level);// if empty a 'LOW' is written
if (moist >= irrigate+5) digitalWrite(pumpPin, LOW); // prevents Jitter
//-------------3. Read the DHT11 humidity/temp sensor----------------// now we measure temperature and air humidity
// READ DATA
// Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
// Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
//-------------4. Read
light=Map(LDRPin);

LDR ----------------------------------------

/* ------------------Actions -------------------*/
//-------------5. DISPLAY DATA -----------------------------------/*
Display data on LCD */
push=digitalRead(PushButton);
if (push==1) // pushbutton not pressed
{
lcd.clear();
lcd.setCursor(0, 0); //set cursor on first line (line 0)
lcd.print("Temp.
: ");
lcd.print((float)DHT.temperature, 0);
lcd.print (char(1)); // prints degree sign
lcd.print("C");
lcd.print(" ");

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

lcd.print (char(7));
lcd.setCursor(0,1); //set cursor on 2nd line
lcd.print("Humidity: ");
lcd.print((float)DHT.humidity, 0);
lcd.print("%");
lcd.print(" ");
delay(1000); // wait for one second and then print the
lcd.setCursor(0,1);
lcd.print ("Irr. Level: ");
lcd.print(irrigate);
lcd.setCursor(0,0);
lcd.print("Moisture: ");
lcd.print(moist);
lcd.print("
");

soilmoisture

}
if (push==0)
// pushbutton pressed
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("licht: ");
lcd.print(light);
lcd.print("
");
lcd.setCursor(0,1);
lcd.print("licht niv.: ");
lcd.print(analogRead(LDRPin));
//buzz(1);
}

// ---------------5. Action on temperature -----//if ((DHT.temperature,0) =<20)


c=(DHT.temperature);
if (c<=20)
//Serial.println(c);
{// switch on heating
digitalWrite(hotPin,HIGH);
}
else
{
digitalWrite(hotPin,LOW);
}
//--------------6. Action on Humidity ----------if (DHT.humidity >=50)
{
// zet ventilator aan
digitalWrite(fanPin, HIGH);
}
else
{
digitalWrite(fanPin,LOW);
}
delay(1000);
//end dht
//end loop
}
//------- End of Main program----------//
//-------- Start of functions-----------int sample(int z)
/* This function will read the Pin 'z' 5 times and take an average.
Afterwards it will be mapped to 8 bits by dividing by 4
Could ofcourse immediately divided by 20 but this way it is easier to follow the program
*/
{
byte i;
int sval = 0;
for (i = 0; i < 5; i++){
sval = sval + analogRead(z);// sensor on analog pin 'z'
}
//sval = sval / 5;
// average
//sval = sval / 4;
// scale to 8 bits (0 - 255)
sval=sval / 20;
return sval;
}
//------------- Flash the backlight a number of times-------void flash(byte y)
{
byte j;
for (j=0; j<y;j++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
return;
}
// This function will sound the buzzer "u" times
void buzz(byte u)
{
byte k;
for (k=0; k<u;k++)

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

{
digitalWrite(buzPin, HIGH);
delay(250);
digitalWrite(buzPin, LOW);
delay(250);
}
return;
}
// This function will blink an LED a number of times for a specific duration
void ledblink(int times, int lengthms, int pinnum){
for (int x=0; x<times;x++){
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}
// This function maps the LDR reading into nrs 0-3
int Map(byte sens) {
// read the sensor:
int sensorReading = analogRead(sens);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// do something different depending on the
// range value:
switch (range) {
case 0:
//
// Serial.println("dark");
lcd.backlight();
break;
case 1:
//
// Serial.println("dim");
lcd.backlight();
break;
case 2:
//
// Serial.println("medium");
lcd.noBacklight();
break;
case 3:
//
// Serial.println("bright");
lcd.noBacklight();
break;
}
return range;
}

You will find a slightly updated version of the sketch here

Step 17: Arduino for Greenhouse, Garden or Growbox: The Software: special characters
As I wanted a proper 'degree' character to use for Celsius (check yr LCD for character 123, perhaps that is suitable for you), I was in luck as the LCD allows for definition
of 8 user characters. Decided to make an arrow as well as visual indication of an empty tank. With regard to the definition of characters for the LCD, that is simple to do
with Malpartida's library On page http://custom-character.appspot.com/ you will find a 5x8 template that you can use to design your own characters. For the Arrow I
designed, the pattern as follows (white segment is '0', black segment is '1'). With the help of a binary - hex converter, one can then find the hex equivalent of the binary
number, but one can also directly send the binary number.
00100 -> 4
00100 -> 4
00100 -> 4
00100 -> 4
10101 -> 15
01110 -> E
00100 -> 4
00000 -> 0
Those binary numbers then can be send to the LCD with the help of the lcd.createChar function of the library. Of course one can also directly send the binary numbers

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 18: Arduino for Greenhouse, Garden or Growbox: possible expansions


-Adding an RTC (DS1307) module will make the circuit more flexible and will simplify the software for e.g. switching on a lamp for a specified time as times can be set
and determined much easier. The circuit shows an addition for an RTC and includes a led that can be set to flash every second. ofcourse that is only optional. Though it
is quite simple to put together an RTC, I would advise against it as ready made RTC modules are very very cheap and look neat. probably cheaper than self built.
(currently 2.83 USD/2.10 euro. the cost of the DS1307 is alread 2.95 euro, a crystal is 0.70 ct, a battery 50 cts, a battery holder 60cts, while the RTC I linked to also
contains a 24C32 EEPROM that offers 32kbit=4kByte of space)
an even cheaper RTC module ($1.99) you find here. but even High precision DS3231 RTC boards are available at very low price (currently Euro 1.94)
Should you still want to build the RTC yourself, I enclosed a circuit, a picture and a stripboard layout. You dont need the EEPROM, just leave it out (and the 6 wire
bridges connecting it)
In order to set the LED attached to the SQW to 1Hz, use the code below. the DS1307_ADDRESS=0x68 (usually)
void sqw1() // set to 1Hz
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary)
Wire.endTransmission();
}

-Setting the threshold level for irrigation in the software with a button rather than with a variable resistor
-Adding various automated growth cycles to the program to chose from. Possibly stored in the EEPROM that comes with an RTC module).
-There is still an analogue port available. One could add an ntc to measure soil temperature, or a rain detector.
-Consider replacing the 4 solidstate relays by a ClickOn-ClickOff system that usually works on 433MHz, by adding a 433 MHz transmitter and sending the right codes.
The 433 MHz transmitter only needs 1 pin, freeing up 3. Ofcourse this is only useful if you feed the heater, fan, pump, lights with 110 or 220 Volt
After building this, i decided that having the LCD on, even if only at low light levels, wasnt the right choice, so i added a switch to just switch it on and off. The I2C module
i used for the LCD has a jumper to disconnect the backlight. A 2 pin female header attached to a switch will do that.
A better idea perhaps would be to add a proximity detector that switches on the LCD whenever someone comes close

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Image Notes
1. DS1307
2. Pull-up resistors
3. Clock Crystal

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Step 19: Updated software


I have updated the software slightly. It now uses symbols for temperature, humidity and pump use.
I have left the original software in place
NOTE: publishing a program somehow always leads to strange effects on this site and substantial manual editing was necessary. It is possible that that resulted in a
mistake and he code wont run. If so, drop me a line and i will send the code
/* =======================================================
Measures soil moisture. If less than a set value it switches the pump
measures temperature. If less than a specific value (20 degrees) switch heating
measures humidity. if above a certain humidity switch fan
measure light... at yr disposal
<a href="http://www.quickgrow.com/gardening_articles/fans_air_movement.html" rel="nofollow"> http://www.quickgrow.com/gardening_articles/fans_...</a>
LCD commands:
lcd.setBacklight(LED_OFF);
lcd.noBacklight();
lcd.clear();
lcd.home();
========================================================= */
//--------------------------------------/*-----( Import needed libraries )-----*/
//--------------------------------------#include <wire.h>
#include <liquidcrystal_i2c.h> //Malpertida
#include "RTClib.h"
//Adafruit
RTC_DS1307 rtc;
#include <dht.h>
//------------------------------/*-----( Declare objects )-----*/
//------------------------------// first the humidity sensor
dht DHT;
// ------ ( declare LCD data)------------------------// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//
addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//
/*-----(Declare RTC objects )------*/
RTC_DS1307 RTC; //declare object

// Set the LCD I2C address

// ------------------------// >> pin definitions


// ------------------------// this is where you define what pins you'll be using</p><p>/*-----( Declare pins )------*/
//analogue pins
byte moisturePin=0; //read soil mositure
byte LDRPin=3;// analog pin for LDR</p><p>//digital pins
byte levelPin= 1; //set level for irrigation
byte humidPin=2; //Humidity sensor
byte lightPin=5; //Switches on light
byte fanPin=6;
//Switches fan
byte pumpPin =7; //Switches pump
byte hotPin= 8;
//Switches heater
byte buzPin=9;
//Switches buzzer
byte emptyPin=10; //digital Pin10 guards waterlevel
byte spikePin=12; //Digital Pin12 ->for intermittent switching of spike
byte PushButton=4;
// PushButton
byte SwitchButton=3;// Make Switch
#define DHT11_PIN humidPin</p><p>/* Variable setting
*/
// variables -placeholders for various readings
byte push=0;
// contains the state of the pushbutton
byte sButton=0;
// contains the state of the throw switch
byte light;
// contains the value for LDR reading
byte pump=0;
// contains the pump state
unsigned int moist=0;
// contains the soil moisture level
unsigned int irrigate=0;
// contains the set level for irrigation in case no potmeter
byte level=0;
// contains level of the waterlevel (HIGH or LOW)
int c;
// contains the temperature reading
// Constants
// these constants won't change:
const int sensorMin = 40;
// LDR minimum, discovered through experiment
const int sensorMax = 1012;
// LDR maximum, discovered through experiment</p><p>//---------------- Define Special Characters----------------// Create a set of new characters
// 0=
// 1=degree sign
// 2=termometer
// 3=waterdrupp
// 4=spikes
// 5=pump
// 6=up arrow
// 7=down arrow
const uint8_t charBitmap[][8] = {
{
0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0
}
,
{
0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0
}
,
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

B11111,
B01110

,
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110

,
{
B10001,
B10001,
B11111,
B10001,
B10001,
B10001,
B10001,
B10001

}
,
{
B01010,
B00100,
B00100,
B00100,
B11111,
B11111,
B11111
}
,
{
0x0, 0x4, 0xE, 0x15, 0x4, 0x4, 0x4, 0x0
}
,
{
0x4,0x4,0x4, 0x4, 0x15, 0xE,0x4,0x0
}
};
/// -------- end creation-------------//The following function, "setup", must always be present
void setup()
{
Serial.begin(115200);
//----RTC----#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//----end rtc------//----Set sqw to 1 Hz--Wire.beginTransmission(0x68);
Wire.write(0x07); //move pointer to SQW address
Wire.write(0x10); // sends 010 (hex) 00010000 (binary)
Wire.endTransmission();
//---end sqw----pinMode(levelPin,INPUT); // set level
pinMode(humidPin,INPUT); // measures humidity
pinMode(emptyPin,INPUT); // measures reservoir
//digitalWrite(emptyPin, HIGH);
// turn on pullup resistors
pinMode(SwitchButton, INPUT);
// make Switch
pinMode(PushButton, INPUT);
// PushButton
pinMode(spikePin,OUTPUT); // for alternative supply to spikes
pinMode(pumpPin,OUTPUT); // Output for Relay
pinMode(fanPin, OUTPUT); // Output for fan
pinMode(hotPin, OUTPUT); // Output for heater
pinMode(lightPin, OUTPUT);// Output for light
pinMode(buzPin, OUTPUT); // Output for buzzer
digitalWrite(pumpPin, LOW);// Pump off
digitalWrite(spikePin, LOW);// moisture sensor off
digitalWrite(fanPin,LOW); // fan Off
digitalWrite(hotPin,LOW);
// heater off
digitalWrite(lightPin, LOW); // light Off
digitalWrite(buzPin, LOW); // buzzer off
/* Now LCD */
//---------------Set up LCD-----------------lcd.begin(16,2);
// initialize the lcd for 20 chars 4 lines, turn on backlight
//upload defined characters to LCD
int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
for ( int i = 0; i < charBitmapSize; i++ )
{
lcd.createChar ( i, (uint8_t *)charBitmap[i] );
}
//---------------end upload---------------lcd.backlight();
// Print a message to the LCD.
lcd.setCursor(0, 0);

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

lcd.print("Greenhouse");
// ------- Quick 2 blinks of backlight ------------flash(2);
// ------- Quick buzz-------------------------------buzz(1);
Wire.begin(); //needed for RTC, not for LCD
RTC.begin();
/* Set the date / time to the time this program was compiled.
Comment this OUT, AND upload, to let the clock just run. */
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
/*----------------------------(end setup )---------------------*/
void loop()
{
DateTime now = rtc.now(); //Get the current data
Serial.print("Het jaar is ");
Serial.print(now.year(), DEC);
Serial.print(" Maand = ");
Serial.print(now.month(), DEC);
Serial.print(" Dag = ");
Serial.print(now.day(), DEC);
Serial.print(" Tijd = ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//------------end of RTC
// ---------- 1. Check if there is enough water ------------// check if there is water
// if not sound the buzzer
// later in the program the pump will be disabled
level= digitalRead(emptyPin);//Switch closed = empty
if (level==0) {
digitalWrite(buzPin, HIGH);
delay (50);
digitalWrite(buzPin, LOW);
delay(500);
}
//------------2. Read the soil moisture content/switch pump---------------/*
First read the level set with P1 on the levelPin and store that in 'irrigate'
*/
irrigate=sample(levelPin);
/*
Then we read the soil humidity sensor.
We'll first have to set the spikePin to HIGH, in case that is used to feed the sensor. After the reading we set it back)
If the value read ('moist') is smaller than what is considered dry ('irrigate') then the pump should be switched on for a specific time.
This is done by indicating a higher treshhold for switching the pump off
*/
digitalWrite(spikePin, HIGH);// put voltage on the humidity sensor
delay(100); //wait a short while
moist=sample(moisturePin); //read soil humiditySensor
//
digitalWrite(spikePin, LOW);
if (moist <= irrigate){
pump=1;
digitalWrite(pumpPin, level); // if the reservoir is empty a 0 will be written
}
if (moist >= irrigate+5) {
pump=0;
digitalWrite(pumpPin, LOW); // prevents Jitter
}
//-------------3. Read the DHT11 humidity/temp sensor----------------// now we measure temperature and air humidity
// READ DATA
// Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
//
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
//-------------4. Read LDR ---------------------------------------light=Map(LDRPin);
/* ------------------Actions -------------------*/
//-------------5. DISPLAY DATA -----------------------------------/*
Display data on LCD */
push=digitalRead(PushButton);
if (push==1) // pushbutton not pressed
{
lcd.clear();
lcd.setCursor(0, 0); //set cursor on first line (line 0)
lcd.print(char(2)); //prints thermometer
lcd.print(" ");
lcd.print((float)DHT.temperature, 0);

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

lcd.print (char(1)); // prints degree sign


lcd.print("C");
lcd.print(" ");
lcd.print(char(3));// droplet
lcd.print(" ");
lcd.print((float)DHT.humidity, 0);
lcd.print("%");
lcd.print(" ");
lcd.print (char(7-level));//prints up or down arrow (char 7 or 6)
if(level+pump==2){ lcd.print(" ");
lcd.print(char(5));
} else { lcd.print(" ");}
lcd.setCursor(0,1);
lcd.print(char(4)); // spikes
lcd.print(" ");
lcd.print(moist);
lcd.print("/");
lcd.print(irrigate);
}
if (push==0)
// pushbutton pressed
{
lcd.clear();
lcd.setCursor(0,0);
//lcd.print("licht: ");
//lcd.print(light);
//-------------DateTime now = rtc.now();
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
//----------lcd.print("
");
lcd.setCursor(0,1);
lcd.print("licht niv.: ");
lcd.print(analogRead(LDRPin));
//buzz(1);
}
// ---------------5. Action on temperature -----c=(DHT.temperature);
if (c<=19)
{
// switch on heating
digitalWrite(hotPin,HIGH);
}
else
{
digitalWrite(hotPin,LOW);
}
//--------------6. Action on Humidity ----------if (DHT.humidity >=50)
{
// zet ventilator aan
digitalWrite(fanPin, HIGH);
}
else
{
digitalWrite(fanPin,LOW);
} delay(1000); //end dht
//end loop
}
//------- End of Main program----------//
//-------- Start of functions-----------int sample(int z)
/* This function will read the Pin 'z' 5 times and take an average.
Afterwards it will be mapped to 8 bits by dividing by 4
Could ofcourse immediately divided by 20 but this way it is easier to follow the program
*/
{
byte i;
int sval = 0;
for (i = 0; i < 5; i++){
sval = sval + analogRead(z);// sensor on analog pin 'z'
}
//sval = sval / 5;
// average
//sval = sval / 4;
// scale to 8 bits (0 - 255)
sval=sval / 20;
return sval;
}
//------------- Flash the backlight a number of times-------void flash(byte y)
{
byte j;
for (j=0; j<y;j++)
{
lcd.backlight();
delay(250);
lcd.nobacklight();
}
//finish with backlight on=
return;
//this function will sound the buzzer "u" times
void buzz(byte u)

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

byte k;
for=(k=0; k<u;k++)
digitalwrite(buzpin, high);
delay(200);
digitalWrite(buzPin, LOW);
delay(200);
}
return;
}
// This function will blink an LED a number of times for a specific duration
void ledblink(int times, int lengthms, int pinnum){
for (int x=0; x<times;x++){
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}
// This function maps the LDR reading into nrs 0-3
int Map(byte sens) {
// read the sensor:
int sensorReading = analogRead(sens);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// do something different depending on the
// range value:
switch (range) {
case 0:
//
// Serial.println("dark");
// lcd.backlight();
break;
case 1:
//
// Serial.println("dim");
// lcd.backlight();
break;
case 2:
//
// Serial.println("medium");
// lcd.noBacklight();
break;
case 3:
//
// Serial.println("bright");
// lcd.noBacklight();
break;
}
return range;
}
void printDigits(int digits) { //this adds a 0 before single digit numbers
//if (digits >= 0 & digits < 10) {
if (digits <10) {
lcd.write('0');
}
lcd.print(digits);
}float tempConvert(float celsius)
{
float fahrenheit = 0;
fahrenheit = (1.8 * celsius) + 32;
return fahrenheit;
}

Step 20: Using timers to read sensors


It is not necessary to continuously read the sensor(s). Especially if it is the soil humidity sensor, it is a good idea to only read that one at intervals, because that will limit
the amount of DC flowing through the pins and thus slow down corrosion.
Rather than using delays, it is possible to use a timer interrupt.
In the example below the prescaler for timer 1 is set to overflow every second. The timer compare interrupt will count the seconds till 10 and then read the sensor. Read
up on use of timers here.
// avr-libc library includes
#include <avr/io.h>
#include <avr/interrupt.h>
int seconds=0;
void setup()
{
pinMode(LEDPIN, OUTPUT);
// initialize Timer1
cli();
// disable global interrupts
TCCR1A = 0;
// set entire TCCR1A register to 0
TCCR1B = 0;
// same for TCCR1B
// set compare match register to desired timer count:
OCR1A = 15624;
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
sei();
// enable global interrupts
}
ISR(TIMER1_COMPA_vect)
{
seconds++;
if(seconds == 10)
{
seconds = 0;
readSensor;
}

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

}
void loop(){
//other stuff
}

Keep in mind though that if you would set a timer such that your soilhumidity would be read every 10 minutes, that if there is need to switch the pump on, you need to
read the soil humidity constantly, otherwise your pump will flow for at least 10 minutes, before another reading is taken. That may be too much water.

Related Instructables

Clock with
thermometer
using Arduino,
i2c 16x2 lcd,
DS1307 RTC
and DHT11
sensor. (Photos)
by Timofte Andrei

Arduino Real
Time Clock
(DS1307) by
Naren_Murali

Weekend
project clock
date
thermometer
and humidity
with Arduino
mega by arick

Mostrar
Temperatura en
Display con
Sensor DHT11 y
Arduino by
ElectroCrea

Build yourself a
clock and
thermometer by
arick

Clock ,RTC
DS1307 SET
AND SHOW THE
TIME ON YOUR
LCD. by citin

Advertisements

Comments
11 comments Add Comment

Akin Yildiz says:

Mar 21, 2015. 7:05 PM REPLY

this is just perfect, wow.!! thank you for sharing.

diy_bloke says:

Mar 22, 2015. 1:01 AM REPLY

my pleasure Akin

caixinhadeleite says:

Nov 10, 2014. 11:19 AM REPLY


you read my mind and thank god you made it right. you just showed 85% of my automation grade last project. the only thing diferent from what i need is the
data readings, full control, and even especies choice from an android app. based on fancy pepper cultivars such "ghost pepper" "Carolina Reaper" an so
on... once im done ill share!

diy_bloke says:

Nov 11, 2014. 6:01 PM REPLY

looking forward to that.


I currently am testing a wifi module on this project

acheide says:

Oct 4, 2014. 3:21 PM REPLY

One of the more comprehensive garden monitor/controllers.


Thanks

diy_bloke says:

Oct 21, 2014. 1:15 AM REPLY


thanks Acheide. Sorry for my late reply, but i discovered i seem to have to click 'Make Comment' twice, otherwise it won't store

Timofte Andrei says:

Oct 20, 2014. 10:36 PM REPLY

That's a nice project! I would use your project to create my own watering system :D thanks for sharing

diy_bloke says:

Oct 21, 2014. 1:14 AM REPLY


thanks Timofte. As a matter of fact I quite enjoyed your clock project and have 'stolen' some of your symbol definitions that I am now using rather than
text for temperature and humidity :-)

henriveil says:

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Sep 24, 2014. 7:08 AM REPLY

BLR_RAVI says:

Jul 30, 2014. 4:41 PM REPLY

very well done..nicely explained..cheers

diy_bloke says:
Thanks BLR_Ravi :-)

http://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/

Jul 30, 2014. 11:03 PM REPLY

Potrebbero piacerti anche