The perfect self-controlled watering system PIC18F4550 PIC12F509 HD44780

Help me to develope andy-progetti.com
If you find this project interesting, give it a like and share it!

Preface

Fig.1 - The boxed HD44780 LCD display

Fig.1 – The boxed HD44780 LCD display

I want to clarify from the outset that this is an ambitious project. Making a watering system that interacts with the environment and autonomously decides if it will irrigate some plants and how long, is not a trivial matter. For this reason, even if the version 1.00 works quite well and it is already operative in my balcony, probably many others will follow soon.

The concept is not far from the project BP0001: watering triggered by the sun and not by clock time, multiple watering zones, same economic 24V AC solenoid valves, same water piping system. But, as you maybe already know, the BP0001 is a good solution only to buffer for a short time frame (ex. a holiday). Indeed, the code is too simple, based on the knowledge I had at that time, and the watering system does what it needs to do as long as the environment remains unchanged. But that’s the point, that’s the limitation to overcome, that has become my goal.

Fig.2 - The perfect self-controlled watering system with the PIC18F4550 (without the display)

Fig.2 – The watering system

So, I have decided to make a system with a temperature and light transducer, controlled by a powerful MCU, such as the PIC18F4550, and with the possibility to add some more devices on its inputs.

The PIC also has to drive a 16×2 LCD display HD44780 (figure 1) in order to monitor the situation and manage the watering more easily. In addition, a smaller MCU has to keep under control the work of its big brother and intervene if something goes wrong with it. On figure 2 you can see the watering system (without the display).

 

What the watering system does concretely

Below, I’m going to explain what is already possible to do with the watering system and how it does it.

Watering in four independent zones

Fig.3 - A little mess during the tests

Fig.3 – A little mess during the tests

You can install up to four solenoid valves in addition to the main one (the main solenoid valve is useful to maintain the water pressure low inside the piping system. Read more about it on the BP0001 project’s article). In this way you can create up to four zones with a different watering time each.

The zones can be configured and enabled or disabled by software.

Set two manual programs

  1. The Start/Stop program allows you to water in a single zone. Once the start button has been pressed, the watering is activated until you press the stop one.
  2. With the Countdown program it is possible to irrigate multiple zones into the same watering cycle according to the timer you have set before for each one.

Set two automatic programs

  1. The Semi-Automatic program ensures to irrigate your plants at dawn. Watering time and frequency (everyday, every two days, etc…) are set for each zone in advance.
  2. The most interesting and original part of the project is the Full-Automatic program. It works similarly to the Semi-Automatic one but watering  time and frequency are calculated by the microcontroller.

How the Full-Automatic program works

First of all, the microcontroller requires an initial setup because it has to know something about your plants and the environment they live in. So, I have described below the required parameters with an example concerning my Zone 1.

MinTmp and MaxTmp

Fig.4 - Watering time function calculated for my Zone 1

Fig.4 – Watering time for my Zone 1

MinTmp and MaxTmp are general parameters applied to all zones. They are the minimum and the maximum temperature the system usually will operate. That does not mean, the system will stop working outside these values but they are an important reference for the next data. I’ve set MinTmp=+5°C and MaxTmp=+40°C.

Notice that even if the temperature can go down under zero in winter, it makes little sense watering with the pipes frozen :-) Unfortunately I haven’t provided the system to stop this condition but I’ll work on it later.

MinT and MaxT

These values represent how long watering the plants in each zone at the minimum and the maximum temperature set just before (MinTmp and MaxTmp). If you don’t have any idea about what to insert here, my suggestion is to irrigate your plants for some days with different programs than the Full-Automatic one in order to get the hand of it. In any case, don’t be worried about making mistakes, you can fine-tune the parameters whenever you like. For my Zone 1 I’ve set MinT(+5°C)=0:20 and MaxT(+40°C)=4:00.

MinDWT

Fig.5 - Watering frequency calculated for my Zone 1

Fig.5 – Watering frequency my Zone 1

MinDWT means minimum daily watering time. Especially when the temperature starts getting rigid and consequentially the watering time is becomes gradually shorter, it is advisable to irrigate less frequently as well.

By setting for instance MinDWT=1:00, as i did for my Zone 1, the microcontroller will irrigate that zone only if the watering time calculated is bigger that one minute. Otherwise it will wait as many days as are necessary (adding every calculated daily watering time) until it goes beyond the MinWDT value set.

Fig.6 - Formulas the microcontroller uses to obtain watering time and watering frequency

Fig.6 – Formulas to obtain time and frequency

In the meanwhile the PIC18F4550 collects every hour one temperature sample, it calculates the average of them and, by using the formulas in figure 6, it obtains the watering time and the watering frequency for each zone.

Concerning the formula to calculate the watering time, I think an exponential response is better than a linear one but, since the project is very recent, I cannot bet on this.

Fig.7 -Watering time and watering frequency at 18℃ for my Zone 1

Fig.7 – Time and frequency at 18℃ for my Zone 1

By using the data previously inserted on the setup menu of the watering system (MinTmp, MaxTmp, MinT, MaxT and MinDWT) it is now possible to obtain the water time for every temperature’s value. For instance at 18°C the microcontroller would water every two days for 1:52. Check it out on figure 7 (decimals don’t count).

And now, after the boring theory, finally some practice :-D Watch some screen’s menus and see how to irrigate your plants by using the Start/Stop, the Semi-Automatic and the Full-Automatic programs.

 

The microcontrollers’ codes

The PIC12F509’s code

The PIC18F4550’s code

 

Practical realization

The whole project is formed by seven different PCBs. Each of them has a specific role as summarized on the block diagram in figure 8.

Fig.8 - The seven PCBs' block diagram

Fig.8 – The seven PCBs’ block diagram

The power supply is connected directly only with the MCU 1 that, in its turn, it will allow to power the rest of the circuit only if it doesn’t detect malfunctions.

The MCU 2 performs all the other processes: it acquires the signals from the transducers necessary to “decide” when, which and how long watering the zones. It also “exchanges” information and drives the LCD display. The relays board, driven again by the MCU 2, opens and closes the solenoid valves safely through the snubbers board.

 

The PSU board

Fig.9 - The power supply board

Fig.9 – The PSU board

The power supply is quite simple. It has got two outputs: 5V DC to power all the boards and 24V AC for the solenoid valves.

Fig.10 - The PSU's schematic

Fig.10 – The PSU’s schematic

The first one is obtained thanks to the usual LM7805. By filtering properly the leveled signal from the transformer’s output with some capacitors, no more components are required. The second one is nothing more than the transformer’s output as it is.

As you can see from the schematic in figure 8, I used a 15W transformer with two 24V outputs but I’m quite sure you can use a normal one with no particular problems.

Fig.12 - The PSU's PCB bottom-mask

Fig.12 – The PCB bottom-mask

Fig.11 - The PSU's PCB layout

Fig.11 – The PCB layout

Making the PCB is easy, it is a single side board and the tracks are quite large.

Once you have soldered all the components, double check the formation of unexpected dangerous shortcuts before powering the circuit.

I suggest you install a small heat sink onto the LM7805 and I recommend you always pay attention not to touch anything in this board since 220V AC line flows through it.

 

The MCU 1 board with the PIC12F509

Fig.13 - The MCU 1 board with the PIC12F509

Fig.13 – The MCU 1 board with the PIC12F509

From the beginning I thought that a watering system that operates 365 days per year should be as reliable as possible.

Fig.14 - The MCU 1 board's schematic with the PIC12F509

Fig.14 – The MCU 1 board’s schematic

I can’t imagine finding myself in a situation in which, for instance, my old downstairs neighbor calls me at work, or worse when I’m abroad, because her flat is flooded thanks to my contraption. So, this small microcontroller has the function of making me feel calm.

In practice, the code compiled for the PIC12F509, lets the PSU to supply all the other boards only if abnormal conditions don’t occur, such as:

  • MCU 2 not present;
  • two or more zones watered at same time;
  • watering takes more than 10 minutes for a single zone (that’s is the maximum amount allowed per zone).

Nothing relevant to say here about building this PCB. When it is ready, program the PIC12F509 with its HEX file (you’ll find it by clicking on the Download button).

Fig.15 - The MCU 1's PCB layout

Fig.15 – The PCB layout

Fig.16 - The MCU 1's PCB bottom-mask

Fig.16 – The PCB bottom- mask

Then, make the connection with the PSU and test it.

Now you should have the green LED on (system ok) for just one second because the microcontroller doesn’t detect the presence of the MCU 2 (red LED on, system ko).

 

The MCU 2 board with the PIC18F4550

Fig.17 - The MCU 2 board with the PIC18F4550

Fig.17 – The MCU 2 board with the PIC18F4550

I think it goes without saying that the PIC18F4550 is the core of the project and its code is the most difficult part to make.

Indeed, at present I know the code is still a bit raw, surely I need to get my hands on it more, but it works. If you are interested in knowing more about it, click here or get the whole project by pressing the download button on the bottom of the page. I tried to comment the code in detail to easily understand what I did, so that maybe you can give me some hints too ;-)

On the schematic in figure 18 you can notice that I have arranged four optional inputs in order to connect some other transducers or sensors.

For instance a level detector might be useful to stop the irrigation if the saucer was full of water. The over-watering could be due to a bad setting of the timer or because something has intervened to modify the environment. In this second case perhaps some rain filled up the saucer or maybe the plant is dead. So, a level sensor would be a fundamental improvement. On the other hand, if the plant is dead it is maybe because it hasn’t had got enough water and a transducer for testing the humidity of the terrain would have been vital for its survival. These are the things that make this project so ambitious.

Fig.18 – The MCU 2 board with the PIC18F455's schematic

Fig.18 – The MCU 2 board with the PIC18F455’s schematic

 

Building this PCB is not as easy as the previous ones because it is double layered.

Fig.21 - The MCU 2's PCB bottom-mask

Fig.21 – The PCB bottom-mask

Fig.20 - The MCU 2's PCB top-mask

Fig.20 – The PCB top-mask

Fig.19 - The MCU 2's PCB layout

Fig.19 – The PCB layout

Pay attention to solder the SV1 terminal towards the bottom side of the board. In the picture in figure 17, C5 is missed because originally it was a polarized 100µF capacitor that caused problems with the display’s initialization. Without it the issue has been fixed. Later, I substituted C5 with the non-polarized capacitor written in the schematic.

 

The transducers board with a LDR and the LM35

Fig.22 - The tiny transducers board with the LDR and the LM35

Fig.22 – The tiny board

To be honest I don’t know which LDR I used as a light transducer. It’s not very important since it has only the function to notify the day status (day or night) and you can set the threshold by software.

Fig.23 - The transducers board's schematic

Fig.23 – The transducers board’s schematic

The temperature is revealed by the integrated circuit LM35. This transducer is ready to use because its output is a voltage proportional to the temperature, but it needs to be calibrated. So, remember afterwards to adjust the low and the high levels by using the R8 and R9 multiturn trimmers located on the MC2 board.

Fig.24 - The transducers board's PCB layout

Fig.24 – PCB layout

Fig.25 - The transducers board's PCB bottom-mask

Fig.25 – PCB mask

It’s up to you decide if making a PCB for only four components. I did it because it might be easier to fix a board on the wall outside than the transducers themselves. However you can solder the wires, the resistor and the capacitor directly on the transducers.

Then, connect the transducers board to the MCU 2 one by a 4 wires cable. For distances higher of a couple of meters, you’d better to use a cable with twisted wires.

 

The HD44780 LCD display board

Fig.26 - The HD44780 LCD display board

Fig.26 – The LCD display board

Fig.29 - The LCD display installed on its box and connected to the VGA terminal

Fig.29 – The LCD to the VGA terminal

The LCD display I used is a blue backlit 16×2 size HD44780 controller configured to operate in 4-bit mode. Both VCC and code are supplied by the PIC18F4550. I used a standard VGA cable to put in communication the LCD display board with the MCU 2 one (detail in figure 29).

Fig.27 - The buttons placement

Fig.27 – The buttons

The four push buttons around the LCD display cover one quarter of the screen each and they get the function written on the display enclosed by two square brackets (figure 27).

The fifth one has a fixed toggle function: displaying the menu or exiting from it.

Fig.28 – The HD44780 LCD display board's schematic

Fig.28 – The HD44780 LCD display board’s schematic

 

Like the MCU 2 board, the LCD display one is a double layered board too.

Fig.30 - The HD44780 LCD display board's PCB layout

Fig.30 – The PCB layout

Fig.31 - The HD44780 LCD display board's PCB top-mask

Fig.31 – The PCB top-mask

Fig.32 - The HD44780 LCD display board's PCB bottom-mask

Fig.32 – The PCB bottom-mask

 

In any case, finally you can properly test your boards.

Fig.33 - The splash screen

Fig.33 – The splash screen

Connect the five PCBs you have built up to now together according to the diagram in figure 8. Verify the Vcc and the GND signals arrive where they should arrive and power up the system.

If everything works fine, initially it should appear on the display the splash screen with the andy-progetti’s logo (figure 33), then, after some seconds, the default screen. Press the menu/exit button and surf through it in order to verify the function of the other four buttons.

 

The relays board

Fig.34 - The relays board

Fig.34 – The relays board

In the relays board each of the four solenoid valves are opened and closed by a relay, triggered, in its turn, by a BJT once received the pulse from the PIC18F4550 microntroller. D1 to D5 are the fundamental flyback diodes for eliminating the voltage spikes generated by the relays’ coils.

If I hadn’t had so many problems with the solenoid valves and the PIC18F4550, I could have also included a snubber section in this board. Hence, initially I decided to build only the PCB concerning the relay section in order to carry on, in the meanwhile, the tests on the snubbers. Read more about it on the next heading.

Fig.35 - The relays board's schematic

Fig.35 – The relays board’s schematic

 

Fig.36 - The relays board's PCB layout

Fig.36 – The PCB layout

Fig.37 - The relays board's PCB mask

Fig.37 – The PCB mask

For the same reason I’ve explained before in the MCU 2 section, I have removed the polarized capacitor C1 (notice it in figure 34).

Later, as you can glimpse from figures 2 and 3, I thought even to take out the X1 terminal and to solder directly a flat cable on the PCB to connect the relays board to the snubbers one by a connector.

 

The snubbers board and the restart issue

Fig.38 - The snubbers board

Fig.38 – The snubbers board

As mentioned just before, I have had lots of problems to find the right solution to not allow the solenoid valves to restart the PIC18F4550 microcontroller.

Fig.39 - The snubbers board's schematic

Fig.39 – The snubbers board’s schematic

After having tested various R-C snubber configurations and values, I found the one shown in the schematic in figure 39 the most effective.

Despite this, the snubbers board didn’t solve my issue completely because I still had some random restarts (to be honest, I have to say, I have tested the watering system in the worst condition by connecting the solenoid valves to the snubber board with very short wires of about 10cm).

Fig.41 - The snubbers board's PCB mask

Fig.41 – The PCB mask

Fig.40 - The snubbers board's PCB layout

Fig.40 – The PCB layout

Thus, I have soldered directly to the solenoid valves transformer’s output the same R-C configuration adopted by the snubbers board (ex. R1, C1, R2, C2). Finally, since that moment, no more restarts have occurred.

 

Download the project

Pressing the button below you can download a compressed file with all the necessary material to build this project:

  • the PIC12F509, the PIC18F4550, the HD44780, the LM35 and the LM7805 data sheet;
  • the pictures of the final project, the schematics, the codes, the HEX files, the PCB masks and the PCB layouts as shown in this post;
  • the schematics and the board files in Eagle format;

If you need some help please do not hesitate to contact me or leave your comments below. Enjoy it!

BP0005 (10.3 MiB, 113 downloads)

 

I have decided to share my knowledge and my projects for free, so I have not inserted any annoying ads on andy-progetti.com. Despite this, if you like my website, please help me in developing it by leaving comments and suggestions, or by making a small donation pressing the button below.
Grazie di cuore, Andrea Dal Maso

 


 

CC BY-NC-SA 4.0
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>