An MCU-Based Irrigation Control SystemTom describes a PIC-based prototype design for a wireless water
control system for EarthBoxes, which are gardening boxes with water
reservoirs and fertilizer strips for cultivating plants and
vegetables. The scalable design can accommodate various
multiple-box configurations.
In this article, I will describe a prototype for a wireless water
control system for EarthBoxes (see Photo 1)。 The design is scalable
and can accommodate small to large quantities of EarthBoxes.
Photo 1-This shows all of the major components of the watering
system. There are two sites in the current design: Site A and Site
B. (This is indicated on the graphics display.)
I first described EarthBoxes in "Java-Based EarthBox Watering
System" (Circuit Cellar 201, 2007)。 EarthBoxes are advanced
gardening boxes with self-contained water reservoirs and fertilizer
strips that create a near-perfect environment for growing plants
and vegetables in a compact 2.5′ long by 15″ wide growing area (see
Figure 1)。 Because of its compact, self-contained configuration,
the EarthBox enables productive gardening in small spaces.
点击查看Figure 1My family and I are strong proponents of the EarthBox. We‘ve used
it successfully for several years now to grow vegetables. As a side
note, because of its productivity and low maintenance,the EarthBox
is beginning to make serious headway as an alternative in some
large commercial-scale farming activities.
When using EarthBoxes, you must make sure the internal water
reservoirs are replenished on a daily basis, especially during the
height of the growing season. A manual watering regimen could be my
"back to nature" mantra; however, I get a lot more satisfaction
using an automation process where microcontrollers (especially ones
that I have designed) do the work. My articles on this subject
document my attempts to automate that process. In one sense, the
design I cover in this article can be viewed as "second
generation"-that is,it is an updated version of the first design
that features new technologies.
IMPLEMENTATION OVERVIEW
A representation of this system is shown in Figure 1. This
configuration should not be conscribed as a design limitation,
rather it is configured for my gardening needs. The design covers
two sites that are on the south side of a large deck in my back
yard. Each site is separated by about 50′ to 70′ to allow for
gardening maneuverability and harvesting. The system, as
implemented, can water eight EarthBoxes. I found that six to eight
EarthBoxes can easily meet the fresh vegetable needs of my
immediate family (six people)。
Let‘s review the major components. The first is the portable
hand-held control unit (on the left-hand side)。 It is used to
remotely determine the level of the water reservoir within each
EarthBox and to initiate, under your control, a watering action for
that EarthBox, if necessary. Another component is the
remote-controlled monitor and water unit. There is one for every
four EarthBoxes. This component is controlled by the hand-held unit
and continuously monitors the water level for each of its
associated Earth- Boxes. It also controls the water valves, which
are used to supply water to the reservoirs in the EarthBoxes. The
final component is the valve box. The boxes are a useful way to
organize distributed valves using a manifold system to help
minimize water hose connections. Both the hand-held and remote
units are Microchip Technology PIC24F-based microcontroller
systems. Communication is accomplished with MaxStream (Digi
International) 802.15 ZigBee modules.
The hand-held control unit uses a Multilabs 160 × 80 monochrome
touchscreen LCD. This approach offers a flexible way of dealing
with the operator interface with virtual touch buttons and graphic
and text displays. The product is easy to configure (via a vendor
command set over a TTL serial port) and reasonably priced. Newer
versions have greater pixel capability and support controlled
backlighting. Photo 2 shows the user interface constructed for the
present design. One of my design goals was simple representation
and straightforward operation. I wanted a novice user,with some
reasonable notion of the overall system configuration,to be able to
effectively operate the system. In other words, I did not want the
watering responsibility all the time!
点击查看Photo 2
The main screen (defaults at power-up) enables you to select the
site of interest, determine the reservoir water status, and
initiate watering, if necessary. Once the site is selected, the
handheld presents the site‘s EarthBox water level and valve
activation status. The status can be refreshed at any time by
pressing the status button. You can then return to the main screen
to select another site. To provide audio feedback for button
depressions against the touchscreen, a simple 3.3-V buzzer is
incorporated. From the status panel, you can enter the control
panel by pressing the control button. At the control panel, you can
initiate a watering. Here, only one box valve can be activated at a
time by selecting the appropriate button or all of the box valves
can be turned off simultaneously. Once a box valve button is
selected, the control panel automatically exits to the status panel
where all status information for that site is updated
automatically.
MCU & TOOLS
The PIC24F is a 16-bit, general-purpose,3.3-V based
microcontroller. Microchip conveniently offers the chip in a 28-pin
skinny DIP package(PIC24F64GA002), which allows for easy assembly
in a solderless breadboard setup. The processor has ample program
flash memory (64 KB), 8 KB of RAM, and 16-MIPS operation with no
need for an external oscillator, low-power operation,and a host of
on-chip peripherals. In fact,it contains more internal peripherals
than there are pins available. Microchip has gotten around this
problem by introducing a peripheral pin select feature,which
enables you to map up to 16 (pins RP1 to RP16) of the 28 pins to
internal peripherals. The days when designers had to face up to
fixed-pin configuration for peripherals are gone. This was a handy
feature in this design, where in one case,two UARTs are required
for the handheld unit (one for the touch panel and the other for
the MaxStream radio)。While in the monitoring and control unit, a
single UART is still required for the radio, but all other pins are
needed for ADC inputs or relay control. The total available
peripheral capability of the PIC24F64GA002 is impressive: 10
channels,10 bits, 500-kbps ADC, dual UARTS, dual SPI, dual I2C,
dual analog comparators, a parallel master port, five 16-bit
timers, five capture modules, five compare/PWM modules, a real-time
clock calendar RTCC with an alarm function,as well as just 19
digital I/O. In this design, I hardly scratched the surface but
there is good use of the digital I/O, the UARTs, a SPI, ADC
channels, and RTCC.
The PIC24F64GA002 supports onboard debug and in-circuit programming
using Microchip in-circuit serial programming (ICSP)。 This
interface requires a simple six-pin interface that is easily
accommodated with simple six-pin 0.1″ headers on the solderless
breadboard. The ICSP appears as a common interface on all of the
prototype breadboards for obvious reasons. Microchip also supplies
the MPLAB integrated development environment(IDE) and C30 compiler
(the C compiler for this part) for free. (C was used in these
designs, but MPLAB also supports assembly.) To complete the picture
for debug and programming, Microchip recently introduced a low-cost
version of a real-time debugger/programmer that
works with PIC24F microcontrollers.
The PICkit2 Debug Express costs less than $50 for this design
implementation. The entire tool set was used. The PICkit2 plugs
nicely into the on-board ICSP, which enables quick insertion for
program updates and debug without any prototype disassembly. When I
assembled the ICSP on the breadboards,I ensured that the PICkit2
could be easily mechanically inserted and removed.
HAND-HELD UNIT
This design is a mixed 5- and 3.3-V design (see Figure 2)。 The
graphics display touchscreen requires 5 V. This voltage is derived
from a SparkFun Electronics regulator adapter board. The 3.3 V is
derived from a separate 3.3-V three-pin regulator connected to the
output of the SparkFun board. Fortunately for 5-V digital I/O, the
PIC24F, which is a 3.3-V part, can accommodate this need. Any
nonanalog peripheral pin of the PIC24F can be configured as
open-drain digital output that with a pull-up resistor to 5 V,can
drive 5-V logic without issue. UART2 TX, connected to an LCD, is
configured in this matter using C code:
__ODB5 =1; //set RB5 to be open drain

All input pins on the PIC24F64GA002 are 5-V tolerant. In this
matter, the Multilab serial output,a 5-V output, is simply
connected to the UART2 RX from the LCD. UART1 TX/RX is connected to
the MaxStream ZigBee radio. The radio module uses a 3.3-V serial
interface so no additional level shifting is needed. I directly
plugged the radio module into the solderless breadboard with a
carrier module that has 0.1″ spacing (from SparkFun Electronics)。
The PIC24F64GA002 real-time clock calendar requires external
circuitry,which is accomplished by a simple 32-kHz clock crystal
and dual 30-pF capacitors. The clock can be used to activate any
needed automatic timebased operations on part of the handheld
application (i.e., polled remote status updates or data logging, if
necessary)。There is also a 1-KB EEPROM interface supported by SPI1.
The EEPROM is needed to establish application-specific power-up
parameters. Finally, a RadioShack 3.3-V buzzer provides audio
feedback for the graphics touchscreen. The list of RP pins and C
code to modify them is in Listing 1. Note that a macro code is used
to unlock and lock the RP pin configuration using a special-purpose
register OSCCON.

Photo 2b shows the hand-held control unit in a makeshift case. The
main screen is active. It also shows the handheld unit disassembled
with major components highlighted. If it is not clear by now, I am
keen on minimal assembly efforts and rapid prototyping techniques.
In these early stages of design,it is prudent to use techniques
that can accommodate quick modification and changes. Once you are
through these early prototype validation stages, a more permanent
configuration can be employed based on market value and customer
needs.
MONITOR AND CONTROL UNITThis unit is exclusively 3.3 V with the exception of 12 V used in
relayswitched control of water valves (see Figure 3)。 UART1 TX/RX
is connected to the MaxStream ZigBee radio, identical to the
hand-held. I tried to employ as much commonality as I could across
the two designs to maximize hardware and software reuse. I also
used the PIC24F real-time clock calendar. Here the clock can
activate necessary automatic time-based operations on part of the
monitor application(i.e., unsolicited status updates every
predefined time)。 Finally, the 1-KB EEPROM is used, for the same
reasons as the hand-held, to establish application- specific
power-up parameters.
点击查看Figure 3The use of the ADC across four unique channels for individual
monitoring of each sensor input is new. This use of ADC is critical
to the"wet test." The Microchip MP608 opamp is used exclusively for
all sensor interfaces. Each MP608 is configured as a unity gain
follower. The part is low-power and has sufficient gainbandwidth
product to handle the sensor‘s 10-kHz input waveform. It also has
rail-to-rail operation for maximum output swing. Each MP608 is
capacitively coupled to the water medium. The input capacitor is
then connected to a resistor bias network to ensure that the
incoming level is properly DC-shifted to reduce clipping before ADC
conversion. A single MP608 is used to drive the water media from
the P24Fgenerated 10-kHz square wave. Again, a unity gain follower
configuration is used. The sensor probes are close to each other in
the water medium (less than 0.5″ separation), so no gain is
required.
The use of PIC24F digital I/O with interface electronics to switch
power to the water valves using relays is also new. The relays used
are DPDT capable of switching up to 2 A at 24 VDC. The coils can be
activated with just 3.3 V. The 12 V is connected to one side of the
DPDT wiper of each relay with the normally open side of the relay
connected to the power input to the water valve. To drive the
relay, each PIC24F digital I/O uses a TP120 Darlington pair
transistor with its collector connected to one side of the coil
while the other is connected to the 3.3-V supply.
Listing 2 includes the list of RP pins and the actual C code to
modify them with the monitor and control unit. Photo 3 shows the
disassembled monitor and control breadboard.
THE WET TEST
The key to ensuring successful operation is what I call the "wet
test." This test is conducted at every sensor in the monitor and
control unit to determine if water is present (see Figure 4)。

During the wet test, the PIC24F64GA002 continuously generates a
10-kHz 50% duty cycle square wave doing nothing other than
bit-banging output pins. The output square wave is then applied to
an MCP608 3.3-V, singlesupply,full-rail op-amp configured as a
voltage follower. The op-amp then sources the signal to all of the
reservoirs within the site associated with the monitor unit. On the
other side of each reservoir is the sensor pick-up for that signal.
A replica of the signal should appear on this side of the reservoir
if water (as an electrical conductor) is present. Each pick-up
feeds its own MCP608 op-amp that is capacitively coupled to the
source and DC-biased to center the mid-voltage point of the
original square wave to avoid clipping. The recovered signal, a
copy of the original square wave, is somewhat "rounded" in its
edges (because of inherent low-pass filtering)and then applied to
an ADC channel of the PIC24F64GA002. Here‘s where the magic
happens. The PIC24F64GA002 is used to generate the output waveform
level. This level is first stabilized and then ADC sampled. This
synchronization with ADC sampling is done in lockstep with all
output waveform changes. Because you know the signal at origin and
you can synchronically sample it as it crosses the water media, you
should know the answer a priori. In essence, you perform a"digital
correlation." If there is a failure in comparison between input and
output at any time, this constitutes a lack of water between the
output source and the input sensor. The only other implementation
concern is to mechanically set the source and sensor probes within
the water medium at the right level in the reservoir to establish a
"full condition."
A couple of other points need to be made. The input samples through
the ADC channel are 10-bit unsigned magnitude values. They need to
be threshold set to determine the one or zero representation. The
PIC24F can accomplish all of these activities in its 16-MIPS
operation and sustain a 10-kHz waveform digital correlation
process. The 10 kHz, its 3.3-V swing, and the probe material
(steel), tend to minimize any potential for corrosion build-up at
the probes. Finally, the entire wet test runs over several hundred
cycles at 10 kHz for each sensor to ensure that a wet or unwet
condition is consistently stable before a determination is made and
reported.
DEPLOYED SYSTEMThe valve box is shown in Photo 4. The solenoid irrigation valve‘s
PVC manifold constructions are also in the box. For each manifold,
a garden hose must be connected for the water supply (see Photo 1)。
I mounted a 0.5″ garden hose adapter to the water entry for each
manifold. The solenoid relay I chose was a Lawn Genie 54048 0.75″
in-line valve, which is available at most hardware stores. I used
crushed white rock to weigh down the remote box.
OPERATING SOFTWARENo picture of a microcontroller-based design would be complete
without an overview and description of the software. As I mentioned
earlier, the design is written in C code using Microchip‘s C30
compiler. The design is multifile and the file structure is used
commonly across all of the designs. What is unique to the design is
constrained and captured in the WATER.C file. This is also the main
function file for C. Again,WATER.C is different for the hand-held
and the monitoring and control unit with all of the other files
being identical.
The file structure captures interfaces via their .H and the actual
functional code in their .C manifestations. There are seven file
sets all together (see Figure 5)。 The EZDISPLAY file set is a
driver set to communicate with a Multilab graphics display in its
predefined manufacturer‘s protocol. This file set is active only in
the hand-held unit; it is reliant on UART2 for its physical
connectivity. The support code needed for its operation is UART2.H
and CONU2.C. This set is a basic console with 9,600 bps, 8 bits, no
parity, 1 stop bit initialization,and input and output routines for
exercising UART2. This is required for EZDISPLAY.
The CONUART1 file set is a basic console with 9,600 bps,8 bits, no
parity, 1 stop bit, initializing, and input and output for
exercising UART1, which is connected to the MaxStream radio. This
code enables UART1 for interrupts when a character is received over
radio and made available to the PIC24F64GA002.
The ADC file set is the basic initializing and input driver for the
PIC24F ADC. It is active only in the monitor and control unit. The
EEPROM file set is the basic driver to read and write from the
25LC101A 1-KB EEPROM using SPI1. The RTCC file set is a basic
driver for initialization, reading, writing to the PIC24F64GA002's
clock, and setting the alarm. This code optionally enables the RTCC
alarm to interrupt.
The MENU file set is a low-level console-based menu system that can
be used to enter proper date/time arguments for clock, read/write
ASCII to EEPROM, bit control, and exercising relays. This functions
as a minitrapdoor to exercise the system using the MaxStream X-CTU
terminal emulation code over a radio link. The receive character
interrupt service is active in both units. The RTCC interrupt is
also active in both designs with a TBD function for hand-held and
an autostatus reporting capability for the monitor and control on
alarm.
Figure 6 is indicative of the WATER.C code in the handheld unit. An
executive routine drives the graphic display. During operator
direction within the constraint of the graphic menu presentation,
are the available buttons that the operator uses to operate the
system. Note that the status display is a template initially. It
requires incoming status messages to be received from the remote
monitor and control units to complete the presentation. Characters
are captured by interrupt from the radio, but they are processed
for operational context by software one character at a time.

Figure 7 is indicative of the WATER.C code within the monitor and
unit. An executive routine continuously performs the wet test on
all the sensors while checking the radio communication for incoming
characters between tests. After receiving a command from the
hand-held, the monitor and control checks for its address
condition. The address condition is valid only for a single
command. The unit must be addressed for each new command.
MESH OR PEER-TO-PEER?The radios are based on MaxStream OEM RF embedded modules that are
compliant with the 802.15.4 standard and operate within the 2.4-GHz
band. The MaxStream XBee type is used in this design. They have an
outdoor range of up to 300′ and data rates of 250 kbps. The modems
interface to a microcontroller through a 3.3-V logiclevel
asynchronous serial port. By default, these modules operate in
a"transparent mode." When operating in this mode, the modules act
as a serial line replacement. All UART data received through the DI
pin is queued for transmission. When RF data is received, the data
is sent out of the DO pin. To modify or read RF module parameters,
the module must first enter into Command mode-a state in which
incoming characters are interpreted as commands. This is achieved
by issuing a Command mode sequence, which looks like a Hayes modem
command sequence. The manufacturer supplies PC-based software
designated X-CTU that enables you to test and configure modules
that are connected to an adapter board that uses a serial port or
USB. Out of the box it is trivial to set up and test a
point-to-point wireless network. I have expanded on this to quickly
configure a peer-to-peer network. For my purposes, each module at
the transparent level uses a unique address one character. In this
case, it is a simple designator ASCII X for site A and ASCII Z for
site B. I was able to do this because there is never an X or Z
ASCII character used in any other communications within the system.
In fact, most communication is restricted to messages with ASCII
numbers, a slash, or a colon. The hand-held uses this address to
designate which module a command is intended for. The addressed
module responds with an echo of its address and results of the
command operation. I tried to keep things simple because most
commands are simply S (status),R (relay), and M (menu)。 The Menu
command is not used during normal operation; it‘s used only for
setups using the X-CTU in terminal mode(thereby allowing me full PC
keyboard and screen) to set the clock, configure the alarm, and
modify the EEPROM.
The peer-to-peer configuration works well for my
small-to-mediumsized system; however, for a larger system, a mesh
will be required. I tried MaxStream XBEE_B modules that support
mesh. The latencies of communication are longer than the peer to
peer, but the range and extensibility of mesh shouldn't be
underestimated because you may have several EarthBox sites spread
over a wide area. In the mesh configuration, one of the units must
function as a coordinator,while the others are router/end devices.
The coordinator must be on first to be able to coordinate all of
the other routers or end devices within the mesh.
START ASSEMBLINGI assembled a comprehensive watering system using a PIC24F64GA002
and commercial radio electronics. The system, as configured,
represents an initial prototype that I can easily modify for
greater range and capability. In small configurations, peer-to-peer
radio setups are sufficient; however, as the system expands, the
need for MESH becomes necessary.
Tom Kibalo (tkibalo@gmail.com) holds an M.S. in electrical
engineering. He has been a practicing engineer for over 36 years in
the Washington, D.C., area. In addition to engineering, Tom is an
adjunct faculty member and technical advisor at a local community
college,where he teaches courses on microcontroller technology. He
is also a Microchip academic partner. Tom enjoys reading,biking,
gardening, and bonsai.
PROJECT FILESTo download code, go to
ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2009/223.
SOURCES
Darlington drivers, diodes, LEDs, relays, and solder
All Electronics Corp.
www.allelectroincs.com
XBEE Radio modules
Digi International, Inc. (distributor)
www.digi.com
EarthBox Gardening system
EarthBox
www.earthbox.com
Capacitors, clock crystal, resistors, and solderless breadboard
Jameco Electronics
www.jameco.com
54048 In-line valve
Lawn Genie
www.lawngeniestore.com
MCP608 Op-amp, PIC24F64GA002 microcontroller, and PICkit2 Debug
Express
Microchip Technology, Inc.
www.microchip.com
Graphic LCD
Multilabs
www.multilabs.net
Regulators board and XBEE carrier adapter
SparkFun Electronics
www.sparkfun.com