249

For the remote control of a garden watering system, the use of a commercially available bi-stable water valve, operating at low voltage, was explored. Previous user experience with this type of device was disappointing.

Summary
For the remote control of a garden watering system, the use of a commercially available bi-stable water valve, operating at low voltage, was explored. Previous user experience with this type of device was disappointing. The driver circuitry is embedded in resin and it appeared difficult to bypass this controller. The valve is opened by a 50 ms pulse and is closed by a 10 ms pulse with reversed polarity. These pulses can be generated by two monostable multivibrators which are respectively triggered by the raising and falling edge of a common input signal. Alternatively, a small PIC microcontroller can be employed for timing of both pulses. These pulses are used to drive an integrated H-bridge power stage.  Hereby, straightforward on and off control is obtained with a single I/O line, making it easy to combine it with a favorite microcontroller or webserver project as a replacement for the proprietary timer.

Introduction
After re-design of our garden, it appeared that watering all those plants was quite time-consuming. Especially in holiday periods we considered it not done to ask the neighbours for this task. By coincidence, we had a Gardena T1030 water valve at our disposal (Photo 1). This valve operates on a 9V battery and can be programmed for up to 3 watering intervals daily at a preset time. Unfortunately, the experience of the previous owner was rather disappointing. The advantage of availability and electrical safety was larger than the urge to purchase a valve of a washing machine, operating at 220Vac. This marked the start of this project, aiming at an easily controlled water supply to the garden.

From mathematics to hardware
The internals of the Gardena T1030 are entirely embedded in black resin. Thus, the device is protected from water (not to be under-estimated) and ‘reverse engineering’. There is no way of bypassing the manual on/off switch position. Nevertheless, the user must open the housing in order to insert the 9V battery. In this condition, the male cinch connector to the valve output is also exposed. Other investigators [1] had already found that the valve could be opened with a negative going pulse of 250 ms and closed with a positive going pulse of 50 ms, measured as core relative to shielding on the cinch connector. However, a final satisfying solution was not found. From the forum discussion it becomes clear that these pulses are far too long since these colleagues considered the entire decay of a discharging capacitor. The question is: too long, but how much? Since a storage scope was not available, a good old pen recorder was used to check the output of the controller (Photo 2). This trace revealed a negative going pulse for opening and a shorter and smaller positive impulse for closing the valve again.

Pulse durations, measured halfway the peak were 300 ms and 150 ms respectively.  With little effort, a comparison between a rectangular pulse and an exponential decay curve, resulting from a discharging capacitor as shown in photo 2, can be made through the area under the curve (Photo 3). The area under the normalized decay curve between 0 and 5 seconds is about 0.99. This same area is found for a rectangular pulse of 1.0 second. This same ratio is obtained when choosing a characteristic RC time of 50 seconds and integrating over 250 seconds. Hence, the pulse duration has to be reduced to one fifth of the original values when applying rectangular pulses. Finally, the amplitude of the closing pulse is about one third of the opening pulse,  ending up with approximately a 50 ms pulse for opening and 10 ms for closing the valve.

Now a circuit had to be designed, capable of generating output pulses of different duration, starting by a raising and falling edge of a digital input signal. Starting point was a standard CMOS 4098 dual, retriggerable monostable multivibrator (MMV) for which the pulse duration is given by 0.5RC. According to the data sheet [2], in the non-retriggerable mode, the outputs have to be fed back to the unused trigger input. Short trigger pulses are generated from the digital input signal by the RC sections at the inputs. The resistors also keep the trigger inputs in a non-active state. The dimensioning is not critical but the RC time must be shorter than the output pulse duration. The circuit can conveniently be tested by making use of a bi-color LED with series resistor. Finally, the MMV outputs are connected to 2 sections of a L293D driver stage to form an H-bridge amplifier [3]. This part takes care of the reversal of the signal polarity without the need for a relay or a double power supply (Photo 4). After all, the original device also makes use of a single battery (first schematic).  

Alternatively, one may opt for a microcontroller solution. Initially, a PIC 16C84 was chosen, to be replaced by a 12F629 having a smaller pin count. This family of microcontrollers is known for its low power consumption (second schematic). The software is quite simple and waits for an incoming input signal on pin 6 (RB0). This triggers the opening pulse of 50 ms on output pin 8 (RB2). After the input changes state, the closing pulse of 10 ms on pin 7 (RB1) is generated. The same L293D bridge driver combines both logic inputs to the bipolar valve output. By choosing an RC clock oscillator, the programming of delay loops is simplified although one have to take the component tolerances into account. In a first working prototype an RC section of 10 kOhm and 10 nF was used, yielding a clock frequency of 3.39 kHz. In the revised version, an RC section of 13 (12+1) kOhm and 470 pF-5% were choosen, resulting in a clock frequency of 32.6 kHz.

Variations on a theme
One of the strong points of the original Gardena controller is the exceptionally low power consumption. During standby, it draws about 10 micro Ampere and opening the valve takes 200 milli Ampere only during a short interval. This makes the use of a single 9 V battery feasible. The discrete MMV solution draws some 40 milli Ampere on average. The PIC microcontroller performes better, consuming on average 26 milli Ampere. This could be improved by disabling the H-bridge driver during standby. Hereto, the enable-inputs on pin 1 and pin 9 were pulled low.  The 3-4EN input is not used and can be hardwired to ground but the 1-2EN input was connected to an OR gate, consisting of R5 and D2-D3,  such that any positive going output pulse from the PIC will enable the bridge amplifier when needed (second schematic). This approach results in an average power consumption of about 16 milli Ampere, approximating the quiescent current of the L293D.

Another interesting option is to replace the output stage by a dual power opamp L272M. Accourding to the specifications, the quiescent current is only about 7 milli amperes [4]. In this setup, the digital input levels are compared to half the digital supply voltage on the inverting inputs, delivered by a voltage divider. When one of the output pulses goes high, the corresponding comparator changes its output state, resulting in activation of the valve. Implementation of this idea, in combination with a PIC 16F629 processor and a logic supply, stabilized by a 78L05 resulted in a power consumption of about 11 milli Ampere (third schematic).

Finally, further reduction can be obtained by employing a small relay for reversal of the output pulse polarity. When not activated, this component draws no power at all. However, one has to take the switching speed of the contacts into account. A small double throw relay with 5 V solenoid voltage, such as the Omron type given in the schematic, has a contact settling time of about 7 milli seconds. This means that prior to the closing pulse, a contact stabilization delay of 10 milli seconds was introduced in the software. during the course of experimentation, it appeared that  using a crystal clock oscillator gave more robust timing of the output pulses. Hence, in the final design a small watch crystal with a resonant frequency of 32.7 kHz was employed. this circuit only draws 2 milli Ampere on average (fourth schematic). Further improvement by choosing a low drop voltage regulator or implementation of the wake up from sleep upon input change feature of the PIC processor family might be considered as a further chalenge but this was not further explored.

Programming the PICs
The software was compiled using MPASM version 3.0 which also supports the 12F6xx type controllers. The hex-files were transferred to the PIC, using the versatile Willem EPROM Programmer with parallel port interface (PCB35 hardware) and the PCB45 0.98D5 software [5].  Configuration fuses are also listed in the software ( __config keyword). In general, for the 16F84: no watchdog timer, no code protect, use power-on reset timer and RC oscillator.

for the 12F629: no watchdog timer, no program or data code protection, MCLR internally connected to +V and pin 4 functions as GP3, BOD disabled, use power-on reset timer and LP (32.7 kHz) crystal oscillator with CLK OUT. The 16F629 is also equipped with an analog comparator, which is not used in this project. This part is configured at the start of the program.

Use Raspberry Pi's cron for timing
Every Unix and Linux distribution, including the increasingly popular Raspberry Pi, has a flexible and versatile mechanism for timed execution of other processes: the cron daemon. This program runs in the background and reads a table, listed in the file /etc/crontab, which holds the timing requirements and the names of the programs which have to be executed. Because of the flexibility of this system, this platform was chosen for automation of the watering of the garden. Although compromising the security of the system, a form on a web-page was designed as a front-end for passing timing information to the crontab: the file containing the information for starting other programs according to a given time schedule. External hardware such as the water valve can be connected to the GPIO pins, provided on the pin header of the Raspberry Pi.  There is already much documentation available how to use the hardware of the Pi and how to install additional software packages on this platform (or on Linux systems in general) so for this project it is assumed that a webserver, such as apache2, is already up and running and that the Wiring Pi software is available for addressing the individual GPIO pins [10]. The gpio program, provided with this software package, has the advantage that it does not need to be run with elevated (root) permissions. The following commands are neccessary for our application:

     gpio mode 1 out
defines GPIO1 as output.
     gpio write 1 1
sets GPIO1 to a logic high level and
     gpio write 1 0
sets GPIO1 to a logic low level.
    gpio read 1
reads back the actual status of GPIO1.

Here, I wish to focus on passing the timing information to the system, thereby creating a more flexible replacement for the original hardware (Photo 1). 
The basic system file /etc/crontab is used for automating system management tasks e.g. creating a backup or inspecting or cleaning up of log-files. A normal user without administrative access rights should not touch this file, since programs in this file are executed with elevated (root) permissions. However, individual users can create their own crontab file in another directory. This is a first security measure to prevent execution of unwanted programs. User crontabs are named with the user name and are placed in the /var/spool/cron/crontabs directory. In this example the webserver is run by the account www-data so the file /var/spool/cron/crontabs/www-data must be created. Furthermore, the user account must be member of the group crontab and must be listed in the file /etc/cron.allow in order to be granted the use of the cron program. Crontab files are not meant to be edited by hand. Hereto the command line program: crontab -e (with the -e flag for edit) is available. The format of the timing fields in a crontab is as follows:
         
   minute hour day_of_the_month month day_of_the_week

with: minute ranging from 0-59, hour from 0-23, day_of_the_month 0-31, month 1-12 and day_of_the_week 0-6 in which 0 is interpreted as sunday. Ranges can be given by connecting values with a minus-sign and discrete values can be combined, using a comma. An asterisk signifies e.g. every month or every day. More complex repetitive tasks can be constructed by the */5 syntax, meaning every 5 minutes or every 5 hours (interesting for other water features such as a fountain).

example 1: 30 18 * * 1,3,5 means every monday, wednesday and friday at 18:30 hours
example 2: 30 12,18 * * 1-5 means every monday till friday at 12:30 hours and 18:30 hours
example 3: 05 */2 * * 1-5 means every monday till friday at 5 minutes past every second hour

From these examples it is already clear that the possibilities are almost endless.
Each crontab line ends with the full pathname to the command to be executed at the given time. In our case: set a GPIO pin to high, wait a pre-defined time and set the GPIO pin to low again. The waiting time is not a part of the crontab syntax but can be provided as an extra input argument. A full crontab line looks like:

example 4: 30 18 * * 1,4 /usr/lib/cgi-bin/water -r 20

which means: turn on the watering system in the rear garden during 20 minutes at 18:30 hours on every monday and thursday. The water program is a shell script which is provided in the software section of this project.

For the ease of configuration, some web forms were created, which upon submission to the webserver, activate a server-based CGI (Common gateway Interface) program. Unix and Linux distributions have several excellent and powerful programmable filters available so that the CGI programs could be written entirely as shell scripts. For transferring data from the html form to the program, the CGI specifies two methods: GET and POST.  The GET method is meant for querying the server or for retrieval of data and transfer of the request proceeds through the QUERY_STRING environment variable. The POST method is used in cases where submitted data have to be stored in a file or database, located on the server. In this case the submitted form data are put in the standard input, which can be used by the CGI program. Technically, both methods can be used but since we have to modify and store a file on the server, POST is the method of choice. The CGI scripts all have the same structure. After submission of the form data, standard input is read with 'cat  -' and passed to the stream editor sed. In a few steps this editor replaces separator characters by spaces en translates hexadecimal codes %2C and %2F to comma and forward slash respectively. If these codes are not present, the data are left unchanged. From the resulting string, the awk program takes selected fields, containing the timing information and combines it with the program, controlling the outputs, yielding a temporarily crontab file. A correctly formatted crontab file is installed by the crontab editor itself by executing 'cat /tmp/crontab | crontab - ' .  Sending an empty or incomplete form results in clearing of the crontab file.

Finally, some thoughts have to be given to were to place the complete system i.e. webserver, since one doesn't want other people to take control over what happens in your garden. Placing the computer within a private subnet, separated from the Internet, is an easy solution but prohibits remote access. Other options such as logging in remotely with ssh and modify crontab manually (using the crontab editor) or making a login/password protected webpage, combined with a PIN code are outside the scope of this project.

Conclusion
Based on analysis of the timing pulses of the original hardware, a few circuits with discrete logical ICs and PIC processors were developed for controlling a low voltage commercial water valve. Alternatively, a water valve from a washing machine, operating at high voltage (220 Vac) might be considered. These parts are really cheap and do not need much 'electronic overhead' to be switched on and off but electrical safety must be taken seriously into account, especially in wet environments. The on/off timing was provided by the Linux crontab facility as present on a Raspberry Pi computer. For editing the crontab, a novel approach by using a web form was presented.

References
  1. http://www.circuitsonline.net/forum/view/46582. The Cat Buster. A water canon as cat deterrer
  2. http://www.datasheetcatalog.org/datasheets/400/82723_DS.pdf Datasheet of the 4098 dual MMV. This part is pin compatible with the 4528 and 4538
  3. http://www.datasheetcatalog.org/datasheet/texasinstruments/l293d.pdf Datasheet of the L293 H-bridge driver
  4. http://www.st.com/web/en/resource/technical/document/datasheet/CD00000054.pdf Datasheet of the L272 series dual power opamp
  5. http://www.sivava.com/ Vendor of the versatile Willem EPROM programmer.
  6. http://www.redcircuits.com/Page18.htm The Plant Watering Watcher II. Soil humidity sensor
  7. Mini-webserver. Elektor (2001) 7-8, 12-15.
  8. Jürgen Wickenhäuser, Micro-webserver. Elektor (2004) 7-8, 12-23.
  9. http://www.raspberrypi.org/ home of the increasingly popular Rasberry Pi platform
  10. http://wiringpi.com/ Software and documentation for controlling the GPIO pins