Automatic Control of Nutrient and pH

A day after installing the hardware and upgrading the firmware to control the pH and EC using an Arduino, the results are as expected-the steady state trends just above the setpoint.

Keeping the pH and EC to a desired set point uses a simple on/off approach. The use of a a PID controller was briefly considered and in the spirit of the minimum viable product, I opted for something simple for now.

Armed with three peristaltic pumps and another on way, some scrap wood, and additional code, a functional setup connected to the Arduino based control panel led to the following setup.

Nutrient Controller

Nutrient Controller

 

 

 

 

 

 

 

Diluting Fertilizers and pH Down solutions

The peristaltic pumps used have a max flow rate of 70 mL/min. Note I received one in the batch that could only pump 27 mL/min so I ended up using that one to control pH.

I opted for constant speed control rather than variable speed and avoided having to deal with PWM and filters to generate the analog output. Based on this information and erring on the safe side, a minimum dispensed volume of 4 ml was assumed. Using trial and error a dilution ratio of 4:1 water:fertilizer and 3:1 water:vinegar make up source to feed the nutrient tank (100 L).  Without dilution, the EC and pH spiked as shown below.

EC Trend

 

 

 

 

 

Control Philosophy

As stated early, on/off control frames the approach to keeping the pH and EC at the desired setpoint.

Hand-Off-Auto

Hand – Dispense fixed volume (mL) and stop. Volume to dispense set from HMI and saved in the Arduino’s non-volatile memory.

Off – turn off pump

Auto – Dispense a fixed volume (mL) every interval (s) . Volume and Interval set from HMI and saved in the Arduino’s non-volatile memory.

  1. Dispense if ( error = (setpoint – present value)) < 0 and trend to control on rising
  2. Dispense if (error = (setpoint – present value)) > 0 and the trend to control on is falling
  3. Stop dispensing when PV is 5% above or below SP depending on the trend to control. This minimizes unnecessary on/off chatter around the SP.

Example:

EC – Desired trend is falling trend. e.g. when below SP control apply control

  • SP = 1050, PV = 1020
  • error = 1050 – 1020 = 30 > 0
  • trend desired: falling
  • Result: control pump
  • Stop control when PV = 1050 * 1.05 ~ 1100

pH – Desired trend is rising trend. e.g. when above SP control apply control

  • SP = 6, PV = 6.2
  • error = 6 – 6.2 = -0.2 < 0
  • trend desired: rising
  • Result: control pump
  • Stop control when PV = 6* .95 = 5.7

The following charts illustrates the result. The left side is when I first moved the controller into Auto and was not tuned to the given process. Both pH and EC respond to drastically to the step change. The right side highlights the EC needing control and the bump in process is not as drastic. One day I may revert to implementing the P part of the PID and dispense the volume based magnitude of the error. So far there is no need for that.

ph ec control

Peristaltic Pumps for Hydroponic Nutrient Dispenser

Nutrient Containers and Pumps

Time to mock up some hardware and write some code to drive the pumps using the nutrient containers and pumps now on hand.

Constraints/Requirements

  1. Use the transistors available spare parts bin
  2. No need for speed control so PWM not required
  3. Dispense a specific volume (ml) of liquid
  4. Pump a volume (ml) liquid over a period of time (s)
  5. Periodically dispense a volume (ml) of liquid every interval (s)

Wiring Diagram

I did not have any MOSFETs on hand and the BJTs available could easily drive small loads.  The measured current draw of the pump came in at around 160mA. The max current the Arduino can source is 40 mA per pin with a max of 200mA combined. Given that the hydroponics drives several circuits, I opted to limit the driving current to 4mA resulting to a 1kΩ resistor and would drive the transistor into saturation. (switch mode).

 

 

 

 

 

 

 

The flyback diode is to prevent any back-emf to harm the transistor.  The before and after reveal  shows the effects of shunting the excess voltage when turning off the pump.

 

 

 

Software

The software is built on top of what is currently written. The partial class diagram with public methods highlights the gist of the PeristalticPump class.

 

 

 

 

 

 

 

Dispensing fluids becomes quite simple. I’ve tested the outputs using an oscilloscope. The one issue the cheap pumps and hoses is the max flow rate degrades over time. e.g. hoses don’t contract and expand the same way. I will deal with that later but there is a method to set the maxflowrate which could be part of the calibration process.

Now that this is unit tested, it is time to replicated the setup 2 more times, and come up with a control philosophy for pH/EC.

 

Home Hydroponic Control System

Hydroponic – 4 Months Later

It has been 4 of months since the Arduino-based hydroponic control system has been in operation and I’ve since harvested arugula and lettuce. I’ve added a couple of columns to to grow cherry tomatoes. For the most part it has been a good experience.

 

 

 

 

 

 

 

I’ve finally added EC and pH measurement from Atlas Scientific  and programmed it using I2C rather than serial. I also added voltage isolation between the probe and the rest of circuitry to reduce changes of noise interference.  Data collected is via modbus over xbee to my SCADA host as before. The updated wiring includes the two extra sensors as shown below.

 

 

 

 

 

 

The spike in measurement resulted from adding more nutrient and pH down to the nutrient tank. More about that later.

 

 

 

 

New Development Environment

Most noteworthy, I migrated to using PlatformIO as the dev environment given that it supports multiple boards, has a command line interface, integrates nicely with the Atom editor and github.

Issues

  1. Inconsistent distribution of flow. The drip lines where sitting at the top and some plants would not get enough water. Fix: added 2″ caps (orange in pic) ensuring proper alignment of drip line.
  2. Water leaking to the floor. I could have done a better job in making the holes to host the net pots. When harvested, I would have to keep an empty net pot to avoid dripping of water. Fix: Replaced with 2″ Wye. It holds a 2″ net pot nicely. I could not find white wyes that did not cost and arm and a leg. I opted for the black drainage type.  The photo shows a trial test.
  3. 3″ net pots to hold canabis and cherry tomatoes is throwaway. The holes where made into 4″ pipe and it caused all kinds of issues. Fix: Replace with TODO wyes.
  4. Drain pump. The one I bought is too slow and noisy. Fix: I use a wet vac to drain the water. It is a lot faster and helps with cleaning the tank.
  5. Level Sensor: The sensor got destroyed with the splashing of the nutrient mixture over time. Also, the readings on average were correct but I did not like the range in level during operation.  Fix: Removed and looking for different sensor.
  6. Topping off nutrients is ok a the start but after a while, I just want to system to take care of it all. One has to add it over a period of time rather than in one shot. Otherwise, spikes in pH/EC occur. Fix: purchased nutrient containers  and some peristaltic pumps. With some TODO driver circuitry and code, managing nutrients should be mostly automated.

Exploration

The following plot represents nutrient temperature and growing chamber temperature. The nutrient temperature is on the low end of the range and I’m going to test to see if keeping the temperature around 21C (70F) impacts the growing cycle.

 

 

 

 

Next Steps

The fun stuff begins and it is machine vision. One to assess how well photosynthesis is occurring and the other is to measure growth.

Home Hydroponics

Scope

After experimenting with LED plant lighting, I finally got to the point of building an Arduino based hydroponic controller and ready to start planting things. As usual, functional requirements frames the project and consist of the following:

  •  Lighting control by On Hour/Minutes and Off Hour Minutes
  • Circulation pump and Fan control via on time/off time
  • Hand-Off-Auto for lighting and circulation pump
  • Measure Mixture Temperature
  • Measure Chamber temperature and humidity
  • Drain pump
  • Circulation Pump
  • H2O Inlet valve
  • pH and Electrical Conductivity
  • LCD display of key parameters
  • SCADA integration via modbus
  • xbee connectivity since I have several of those lying around
  • Nice to Have – CO2

P&ID

A simple P&ID of the system the building of the system from which the tag list was created along with the corresponding modbus addresses.

Device Communications

The system consists of a mash-up of several technologies which communicate over different protocols. I like I2C and 1Wire as it simplifies the wiring.

Control Panel

The control panel components came from Digi-Key  as they provide competitive pricing and quick delivery. Note I chose Phoenix terminal blocks for the cheap price and quality.  Enclosures ended up too expensive leading to the din rails, terminal blocks, etc. mounted on wood. It does the job and came out ok.

http://www.flashsense.com/wp-content/myuploads/wiAz1XoRCR0BmEJEZOsA.jpg
http://www.flashsense.com/wp-content/myuploads/wiAz1XoRCR0BmEJEZOsA.jpg

A simple food container houses the LCD/RTC and switch enclosure and the future EC/pH sensor electronics as this is where the I2C devices reside. Labeling is not the nicest but it is good enough for now. The xbee device is left hanging there for now as well.

Piping

Inspiration for the setup came from this site and a chat with the folks at Quick Grow. I was told that 2.5″ piping would be sufficient to grow lettuce and thus ended up using that size for the first phase. I left a space for future expansion for 3″ pipe for cherry tomatoes.

Note this was built in an un-sused shower space in the basement and the folks at Quick Grow stated that the white will reflect the light so no need for reflective material. I also purchased the LED lighting from them for both the growing chamber and seeding area to support the local business.

General Parts List

A partial parts list used for the project.

The Schedule 40 piping and connectors as well as the sharkbite products came from good old Home Depot.

Stripping wire and terminating them became quite easy with these tools

Software

There is a lot of libraries available to connect the various sensors. I also wrote my own to handle discrete inputs and outputs, timer outputs, Hand-Off-Auto, etc. This made things easier to maintain and can be found at https://github.com/chrapchp/IOLib and the main code at https://github.com/chrapchp/PlantLEDLighting/tree/hydroponics

Changes in switch values are detected and invoke a callback function. For example, the HOA class invokes a callback function passing the new switch state detected.

 

Median Filters

Noisy Liquid and CO2 levels was observed over time through the SCADA system. I exported the data in question and explored ways to deal with the extreme fluctuations. Note I did put an oscilloscope and the CO2 signal was clean. After some Excel plotting, I ended up with wanting a median filter with with a window size of 5. Fortunately, a library was written for it which saved me some time. The content of the signal on the left is the before filtering there is about 3000 samples in that image.

CO2

Nutrient Tank Level

Data Acquisition

The flow and CO2 use interrupts. Polling rates of the switch are set up at 500ms unless overridden. e.g.

Flow rates are calculated every 1second based on the pulses from the interrupt handler. The 1-wire and DHT-22 reading is performed every 5 seconds.

The ultrasonic level sensor was “calibrated” to read 100% at 100 L as well as the hi level switch.

 

Wiring

  • Ultrasonic sensor – no special circuitry required to connect to the Arduino. Example here
  • DHT-22 sensor – added a 10k resistor between vcc and signal. Example here
  • 1Wire Temperature sensor -added 4.7k resistor between vcc and data
  • Input and level switches – enabled internal pull-up resistor in Arduino.
  • CO2 – no special circuitry required. Data sheet here
  • Flow sensor – no special circuitry required. How-to here
  • Added 1000uF electrolytic capacitor between 5v and DC ground handle potential inrush currents
  • Added 100nF ceramic capacitor between 5V and DC ground to filter out high frequencies

Next Steps

Installing a camera to take time-lapsed photos of the plants in the growing chamber as well moving to a smaller fan rather than the larger 12″ one in place is on the radar. Creating  mobile friend UI on the SCADA system is also in the queue.