Professional Documents
Culture Documents
An MCU-Based Irrigation Control System
An MCU-Based Irrigation Control System
F EATURE ARTICLE Circuit Cellar, the Magazine for Computer Applications. Reprinted by permission. For subscription information, call
(860) 875-2199, or www.circuitcellar.com. Entire contents copyright ©2009 Circuit Cellar Inc. All rights reserved.
by Tom Kibalo
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.)
Site A
LCD
Touch
Staking system panel
Water tube
Fertilizer band
Radio-based EarthBox
Mulch cover hand-held control unit Water
Seepage port
Site B
automate that process. In one sense, the design I cover in supply water to the reser voirs in the EarthBoxes. The final
this article can be viewed as “second generation”—that is, component is the valve box. The boxes are a useful way to
it is an updated version of the first design that features new organize distributed valves using a manifold system to help
technologies. minimize water hose connections. Both the hand-held and
remote units are Microchip Technology PIC24F-based micro-
IMPLEMENTATION OVERVIEW controller systems. Communication is accomplished with
A representation of this system is shown in Figure 1. MaxStream (Digi International) 802.15 ZigBee modules.
This configuration should not be conscribed as a design The hand-held control unit uses a Multilabs 160 × 80
limitation, rather it is configured for my gardening needs. monochrome touchscreen LCD. This approach offers a flex-
The design covers two sites that are on the south side of a ible way of dealing with the operator inter face with virtual
large deck in my back yard. Each site is separated by about touch buttons and graphic and text displays. The product is
50′ to 70′ to allow for gardening maneuverability and har- easy to configure (via a vendor command set over a TTL
vesting. The system, as implemented, can water eight serial port) and reasonably priced. Newer versions have
EarthBoxes. I found that six to eight Ear thBoxes can easily greater pixel capability and suppor t controlled backlighting.
meet the fresh vegetable needs of my immediate family (six Photo 2 shows the user interface constructed for the pres-
people). ent design. One of my design goals was simple representa-
Let’s review the major components. The first is the tion and straightforward operation. I wanted a novice user ,
portable hand-held control unit (on the left-hand side). It is with some reasonable notion of the overall system configu-
used to remotely deter mine the level of the water reser voir ration, to be able to effectively operate the system. In other
within each EarthBox and to initiate, under your control, a words, I did not want the watering responsibility all the
watering action for that Ear thBox, if necessary. Another time!
component is the remote-controlled monitor and water The main screen (defaults at power-up) enables you to
unit. There is one for ever y four EarthBoxes. This compo- select the site of interest, deter mine the reservoir water
nent is controlled by the hand-held unit and continuously status, and initiate watering, if necessar y. Once the site is
monitors the water level for each of its associated Ear th- selected, the handheld presents the site’s EarthBox water level
Boxes. It also controls the water valves, which are used to and valve activation status. The status can be refreshed at any
a) b) c)
February 2009 – Issue 223
Photo 2 —Take a look at a composite hand-held assembled in a box (a) , a breadboard (b) , and graphic display menu (c) .
of a real-time debugger/programmer Listing 1— The code performs a peripheral pin select function for the PIC24F64GA002, configuring the RP pin
that works with PIC24F microcon- outside on-chip package to be set to different peripherals contained within the chip. In this case, for inputs,
trollers. The PICkit2 Debug Express RP9 is configured for UART1 receive. RP13 is configured for SPI port 1 data in. RP7 is configured for UART2
costs less than $50 for this design receive. For outputs, RP8 is configured for UART1 transmit. RP5 is configured for UART2 transmit. RP12 is
configured for SPI port 1 data out, and RP6 is configured for SPI port 1 clock out.
implementation.
The entire tool set was used. The __builtin_write_OSCCONL(OSCCON & 0xbf); // unlock
PICkit2 plugs nicely into the on-board //input handheld
RPINR18bits.U1RXR =9; //make RP9 UART1 RX
ICSP, which enables quick inser tion
RPINR20bits.SDI1R=13; //make RP13 SDI1
for program updates and debug with- RPINR19bits.U2RXR=7; //make RP7 UART2 RX
out any prototype disassembly. When I //output handheld
assembled the ICSP on the bread- RPOR4bitsRP8R =3; //make RP8 UART1 TX
RPOR2bits.RP5R =5; //make RP5 UART2 TX
boards, I ensured that the PICkit2
RPOR6bits.RP12R=7; //make RP12 SDO1
could be easily mechanically inser ted RPOR3bits.RP6R=8 //make RP6R SCK1
and removed. __builtin_write_OSCCONL(OSCCON | 0x40); //lock
HAND-HELD UNIT
This design is a mixed 5- and 3.3-V Listing 2— The code does a peripheral pin select function for the PIC24F64GA002, configuring the RP pin out-
side on-chip package to be set to different peripherals contained within the chip. In this case, for inputs, RP9
design (see Figure 2). The graphics dis-
is configured for UART1 receive and RP7 is configured for SPI port 1 data in. For outputs, RP8 is configured
play touchscreen requires 5 V. This for UART1 transmit. RP5 is configured for SPI port 1 data out. RP6 is configured for SPI port 1 clock out.
voltage is derived from a SparkFun
Electronics regulator adapter board. builtin_write_OSCCONL(OSCCON & 0xbf); // unlock
//input monitor and control
The 3.3 V is derived from a separate RPINR18bits.U1RXR =9; //make RP9 UART1 RX
3.3-V three-pin regulator connected to RPINR20bits.SDI1R=7; //make RP7 SDI1
the output of the SparkFun board. For- //output monitor and control
tunately for 5-V digital I/O, the RPOR4bitsRP8R =3; //make RP8 UART1 TX
RPOR2bits.RP5R =7; //make RP5 SDO1
PIC24F, which is a 3.3-V par t, can RPOR3bits.RP6R=8 //make RP6R SCK1
accommodate this need. Any nonana- __builtin_write_OSCCONL(OSCCON | 0x40); //lock
log peripheral pin of the PIC24F can
be configured as open-drain digital out-
put that with a pull-up resistor to 5 V,
can drive 5-V logic without issue.
Software
UART2 TX, connected to an LCD, is
configured in this matter using C Development
code: Tools
__ODB5 =1; //set RB5 to be open drain
The Leader in Microcontroller Development Solutions
All input pins on the
ARM
C/C++ Development Kit including ULINK®2 Adapter
PIC24F64GA002 are 5-V tolerant. In best-in-class compilers, genuine Keil for target debugging
this matter, the Multilab serial out- μVision®, and royalty-free RTX RTOS. and Flash programming.
put, a 5-V output, is simply connected www.keil.com/arm
to the UART2 RX from the LCD.
Cx51
A/D I/O Ports Run-
UART1 TX/RX is connected to the Control
condition.”
Note: Two versions of
A couple of other points need to be WATER.C
(main) Water.C hand-held
made. The input samples through the and monitor
menu presentation, are the available each new command. default, these modules operate in a
buttons that the operator uses to oper- “transparent mode.” When operating
ate the system. Note that the status MESH OR PEER-TO-PEER? in this mode, the modules act as a
display is a template initially. It The radios are based on MaxStream serial line replacement. All UART
requires incoming status messages to OEM RF embedded modules that are data received through the DI pin is
be received from the remote monitor compliant with the 802.15.4 standard queued for transmission. When RF
and control units to complete the and operate within the 2.4-GHz band. data is received, the data is sent out of
presentation. Characters are captured The MaxStream XBee type is used in the DO pin. To modify or read RF
by interrupt from the radio, but they this design. They have an outdoor module parameters, the module must
are processed for operational context range of up to 300 ′ and data rates of first enter into Command mode—a
by software one character at a time. 250 kbps. The modems inter face to a state in which incoming characters are
Figure 7 is indicative of the microcontroller through a 3.3-V logic- interpreted as commands. This is
WATER.C code within the monitor level asynchronous serial port. By achieved by issuing a Command mode
and unit. An executive routine contin-
uously performs the wet test on all
the sensors while checking the radio
communication for incoming charac-
ters between tests. After receiving a
command from the hand-held, the
monitor and control checks for its
address condition. The address condi-
tion is valid only for a single com-
mand. The unit must be addressed for
Handheld WATER.C
A
Start
N
Y
Y
Output main screen Main?
N
Y
N Control?
Touch?
N
Output
Y control
Status? screen
Y
Issue control
Output status command
screen template
Y
RX Character? Process status message
N
Message N
complete?
February 2009 – Issue 223
sequence, which looks like a Hayes character. In this case, it is a simple and results of the command operation.
modem command sequence. The man- designator ASCII X for site A and I tried to keep things simple because
ufacturer supplies PC-based software ASCII Z for site B. I was able to do most commands are simply S (status),
designated X-CTU that enables you to this because there is never an X or Z R (relay), and M (menu). The Menu
test and configure modules that are ASCII character used in any other command is not used during nor mal
connected to an adapter board that communications within the system. operation; it’s used only for setups
uses a serial port or USB. Out of the In fact, most communication is using the X-CTU in ter minal mode
box it is trivial to set up and test a restricted to messages with ASCII (thereby allowing me full PC keyboard
point-to-point wireless network. I numbers, a slash, or a colon. The and screen) to set the clock, configure
have expanded on this to quickly con- hand-held uses this address to desig- the alarm, and modify the EEPROM.
figure a peer-to-peer network. For my nate which module a command is The peer-to-peer configuration
purposes, each module at the transpar- intended for. The addressed module works well for my small-to-medium-
ent level uses a unique address one responds with an echo of its address sized 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 exten-
sibility of mesh shouldn’t be underes-
timated 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
Start
Initialize system
Clear RX character
Y
RX Character?
N Y Set my
My address
Wet test sensor1 address? flag
Y
RX Character? N
Am I
addressed?
N
Y
Wet test sensor2
Menu
Y process
Y Menu
command? clear
RX Character? address
flag
N
N
Status
Wet test sensor3 Y process
Status clear
command? address
flag
RX Character? N
Y
Relay
February 2009 – Issue 223
N Y process
Relay clear
command? address
Wet test sensor4
flag?