Saturday, February 23, 2013

DIY Arduino PH Controller Tutorial

Controlling the ph of a solution by hand is hard and tedious. It is a prime candidate for automation.

Lets go over the materials you will need to gather:

1. First you will need to buy a peristaltic pump. I bought mine off of ebay for 85$(You are saving money on anything that you get under $175 vs retail). To find them search "peristaltic pump" on ebay. Here are some pics of the one I bought. Make sure you buy a manual one, not one with it's own computer, because the pump will be controlled by an arduino.

Normally, in a ph solution, the ph if left alone will rise out of the desired range. To knock the ph back down we add an acid, which lowers the ph. Acids cause corrosion. For this reason a peristaltic pump is the correct answer for dosing acid because a peristaltic pump does not come in contact with the solution that it is pumping and less likely be corroded.

2. The second item you will need is peristaltic pump tubing. The peristaltic pump works by squeezing liquid through tubing. Here is some info about tubing. For my setup I used 1/16" ID x 1/8" OD 0.30" wall silicone tubing. You can search "1/16 id 1/8 od silicone tubing" on google and it will show up, but make sure you buy the correct tubing for your pump.

Here are some pics of the tubing installed in the pump. As you can see, when the pump is on, the head turns and squeezes liquid through the tube. The knob on the left controls the rate that the head spins at, and the switch controls the direction that the head rotates in.

3. The third item you need is a container that will hold the solution. I am using some glassware that I had Allen Scientific Glass in Boulder, Colorado make for me.

Remember that I said that the acids that are mixed into a solution to lower its ph are corrosive. Glass is a good pick for a container because it has a very good 'chemical resistance'. To see how it compares to other materials see this. For most chemicals, it shows no damage after 30 days, and for others only surface etching.

The acid I am using is labeled 'ph down'. I know from working with ph down that it is usually diluted phosphoric acid.

Looking at the sheet, Glass shows no damage after 30 days of exposure to Phosphoric Acid 5% and 85%. But if you pay attention the HDPE material also shows no damage after 30 days of exposure to Phosphoric acid 5% and 85%.

So if glass is too expensive, use HDPE.

HDPE buckets are readily available online. I might switch to using them if I need a larger container. I would recommend an HDPE bucket from an online diy home brewing store because they usually have a spigot, from which you can easily drain the bucket, or extract a portion of the solution. You can find these by searching google for 'fermenting bucket.' This is what searching "fermenting bucket with spigot" came up with:

4. The fourth item you will need is a ph probe. Aside from minor differences, all of them are the same. I am using this one, and I am using it specifically because it is threaded.

The reason I am using a threaded ph probe is because I can screw it into the glassware I had made. Im not going to take off the ph probe boot yet, but this is the part where it screws in.

10. A arduino screw terminal shield. This will hold the wires in without soldering.

11. The last item you need is an lcd shield. I bought this on ebay, from seller emartee2009. The listing you should look for will be something like 'Nokia 5110 LCD Shield With SD -Arduino Compatible'.

I will be using the arduino and ph shield+probe to monitor the ph, display it on the lcd screen shield, and then control the ph by turning on the peristaltic pump that administers ph down into the container.

The next steps cover assembly and programming of the controller.

1. Insert the screw shield into the arduino uno.

2. Insert the ph shield on top of it.

3. Insert the lcd shield next.

4. Connect the ph probe.

5. Now connect the arduino to your computer via usb and start the arduino IDE. If you do not have the arduino IDE installed, go to the download page.

The first thing we need to do is install the library for the lcd shield to work.

Now we need to copy the library to the arduino library folder. It is in different locations on the windows version, osx version, and linux version.

On OSX, you need to show package contents on the arduino.app file.

Then the libraries folder is located in

Contents > Resources > Java > Libraries

Alternatively, you can also create a library folder in the Home > Documents > Arduino folder.

Copy the extracted LCD5110 folder to the arduino libraries folder.

On windows and linux you will have to find out where the arduino libraries folder is. I know that when you download the windows version the arduino ide is in a folder, so it should be somewhere under that folder.

6. Now we make sure the LCD works by itself first. Open the Arduino IDE if you do not have it running and select the LCD example:

Make sure your arduino is connected to your computer by usb then hit upload.

You should see the result on the LCD.

Once you have verified the LCD works, then you can proceed.

7. Now it is time to load the full program and set it up. Create a new sketch and paste the following code in to it:

Connect your ph probe, and after clicking upload you should see a screen like this:

8. Now we calibrate the ph probe. The reason we need to calibrate is because ph probes age. Normally they output a certain +-mv per ph, but over time this drifts. Calibration overcomes this inconvenient fact of life. Here I have the ph 4.01 calibration solution on the left, tap water(If you have distilled water that is better) in the middle, and ph 7.0 calibration on the right.

First rinse your ph probe in the tap water.

Then dip your ph probe in the 7.0 solution and write down the voltage value. It is 2.304 volts.

Now re-rinse the probe.

The next step is to dip your probe in the 4.0 calibration solution. Write down the volts and temperature. So 3.341 volts and 31.1C. As you can see my thermostat kicked in.

Since temperature has no effect on ph at ph 7, we do not record it then. Temperature does affect the 4.0 ph measurement however, so we write it down.

Now we will plug the values that we wrote down into the program. Find the following variables:

We need to change them to the values that we just measured.

volt4 = 3.341;

volt7 = 2.304;

calibrationTempC = 31.1;

Then press upload. This completes the calibration.

9. Step nine is to attach the relay to the arduino. You will need a 5/64" screwdriver for this part.

Wire the terminal labeled "+in" on the relay to pin 13 on the arduino. Wire the terminal labeled "-in" on the relay to GND on the arduino.

10. Now that we have the relay connected, we alter the code for when to turn the relay on. At the bottom of the code there is this snippet:

Since I will be growing hydroponic strawberries in coco this summer(hopefully) a ph of 5.5-6.2 is the best range for me.

11. The rest of the steps are just assembly. Connect the relay to the peristaltic pump, connect the other end of the relay to power. Mount the ph probe into the solution container. Connect the peristaltic pump tubing to the ph down.

The values that you get when doing your calibration will be specific to the probe that you used, and calibrationTempC is for storing the temp that the calibrations were taken at. This is because the 4.0 calibration point needs to be temp compensated. You can use the default values, which are what my probe puts out, but you will lose precision.

This looks like a great tutorial! I just started growing vegetables hydroponically and already see the need for automated pH dosing. I am going to follow in your footsteps. Did you implement this with a proper hydro setup? How did it turn out and do you have any more documentation?

Sorry for the late reply! Yes I used it last summer to water tomatoes in coco pots. They grew well, but they are tomatoes which are easy. I want to grow strawberries, and am in the process of implementing an aeroponic setup. Should be ready maybe next summer or 2014 late summer. I haven't had enough time to devote to this since I started day trading.

Reza — can you explain how these variables are used — tempPin and calPin? Can't figure out how it works, as there's no separate temp probe connected in your setup, is it? I have a non-pro version of this DF Robot ph probe, which doesn't have a separate line for temp, and I guess it does not differ from Pro version. What's the difference between these two anyway? Will the industrial version work better, and standard one is goin to fail at some point, when doing 24/7 monitoring? Any idea?

Hi Reza!Can the code be modified so that, instead of having a target set point (ex. 5.5-6.2), there is a dynamic target? For example, if I would like to have the pH probe read the ambient pH and then have the acid delivered until the pH drops 0.3 units, can I do that? I have a flow through system, and I want to adjust the pH downward by 0.3 units from the ambient pH always, can I program this to do that?

Hello Reza ! im using your sketch to make an Arduino Mega based controller for my greenhouse, making some modifications on the code now, i have a question.In your code, before the setup you use a "int calPin = 8;", why is that? i cant see it in the rest of the code...thank you !