Arduino pH Controller

Jeremy Bohrer
5 min readNov 3, 2020

In this guide we will build an Arduino pH controller that is used to monitor the pH level of a solution and raise the pH to a target level when the pH gets too low. All the code for this project is available on Github here.

Arduino pH controller

It might be some time since you were in a high school chemistry class and thought of the pH scale, so let’s do a quick overview before we mention it in every other sentence. pH is a scale from 0 to 14 to specify how acidic or basic an aqueous solution is. Low pH is acidic (lemon juice), high pH is basic (milk), with 7 being neutral.

I became interested in building a pH controller while working on a hydroponics project to grow tomatoes among other things. I would see wild swings in pH as my plants drank excessive amounts of water, making for an extremely acidic water bath. This was not good news — tomato plants like a very narrow band of pHs to absorb specific nutrients and produce tomatoes. At least once a week I would check the pH, realize my plant was on the brink of dying, and have to raise the pH. Hopefully my next hydroponics excursion will go much smoother with this pH controller!

Parts

This diagram is not exact, follow the instructions below on how to wire up.

Discussion of Parts

The pH sensor I used in this guide is the Gravity pH Meter Pro, but at the time of this writing a V2 version of the sensor has been released. I used this pH sensor in an extended hydroponics project for multiple months without any issues, so did not see the need to upgrade. If I was going to buy a new pH meter I would upgrade to the new version. It looks like the V2 version uses the same pH meter as the V1 version, but has a new board with some nice software benefits such as supporting a wider range of voltage and being easier to calibrate.

If you don’t care to calibrate your pH sensor, you can always use water and an acidic liquid like lemon juice.

Wiring It Up

The diagram above is not exact, please follow the instructions below on how to wire up your board.

  1. Connect the 5v and GND to the bread board.
  2. Add the pH sensor by connecting red to 5v, black to GND, and blue to pin A0 or any other analog pin.
  3. Add the peristalitic pump by connecting red to 5v, black to GND, and green to pin 9 or any PPM supported digital pin.
  4. Upload your sketch and power with either a 12V wall adapter or USB cable.

Coding

The logic for how the pH controller is pretty simple. It reads the pH every couple of seconds, if the pH is below a certain level + threshold, then it will begin to dose.

Calibrating

Make sure to unscrew the pH sensor’s lid! When calibrating for the first time I did not realize I had to do this. I placed my sensor in the 4.00 solution and was scratching my head for an hour when I kept reading 7.00! It is a good idea to calibrate a new pH sensor to ensure accuracy. It’s an even better idea to calibrate with the lid off.

pH calibration solutions
  1. Put the pH sensor in 7.00 pH calibration liquid.
  2. Uncomment phCalibration() and upload the sketch. Open up your serial monitor, you will see an output saying what your phOffset is.
  3. Update const phOffset with the result from step 3 and upload the sketch.
  4. Put the pH sensor in 4.00 pH calibration liquid and let the value stabilize. In your serial monitor you should see the pH reading of around 4.00.

Testing

Time for the fun part! We’ve wired up the schematic, uploaded the sketch, and are ready to regulate the pH of our liquid.

  1. Place the pH sensor, one of the pump’s tubes, and water into a container.
  2. Place the other pump’s tube and some of the 10.00 pH calibration solution into a second container.
  3. Run the sketch with your Serial monitor open. You should see it outputting the current pH of around 7.00.
  4. Drop a small amount of the 4.00 pH calibration solution in the container with your pH sensor. When the pH goes below 6.75 You should see the output “pH lower than target, beginning to raise acidity” and the pump begin to move the alkaline solution into the acidic solution to raise the pH.
  5. When the pH of the solution is 7.00 again the pump will stop.
Experimenting with our Arduino pH controller

Next Steps

For your own use case you will probably want to update the phTarget and the phToleration to meet your own needs. For example, when growing tomatoes, they enjoy slightly acidic conditions between 6 to 6.5 pH. So I would set a pH target of 6.5 and a pH toleration of 0.5. There is a bit of an art to this, so have fun experimenting!

Here are some fun and interesting ways we can extend this pH controller in the future.

  • Regulate the pH up and down. This would be pretty easy, all we would need to do is add a second peristalitic pump that is used to pump acidic liquid.
  • Read the temperature of the liquid to get a more accurate pH reading.
  • IoT enabling the device.

References

--

--