Copied and pasted the code below into a python script but needed to add the utf encoding at the top as the file format wasnt compatible when pasted from my browser:
# -*- coding: utf-8 -*-
import math
import sys
import time
from grove.adc import ADC
# CONSTANTS
Vref = 4.95 # volts
Vcal_mv = 646 # milli-volts (using raw value 535 * 4.95/4096 * 1000)
#Vcal_mv = 2588 # milli-volts (using raw value 535 * 4.95/1023 * 1000) note 10-bit A2D
mv_per_ph = 59.6
ph_per_mv = 1.0 / mv_per_ph
class GrovePH:
def __init__(self, channel):
self.channel = channel
self.adc = ADC()
@property
def PH(self):
value = self.adc.read(self.channel)
if value != 0:
voltage_mv = value * Vref / 4096.0 * 1000
#voltage_mv = value * Vref / 1023.0 * 1000
PHValue = 7 - ((voltage_mv - Vcal_mv) * ph_per_mv)
return PHValue
else:
return 0
def main():
#if len(sys.argv) < 2:
# print(āUsage: {} adc_channelā.format(sys.argv[0]))
# sys.exit(1)
#sensor = GrovePH(int(sys.argv[1]))
sensor = GrovePH(0)
print('Detecting PH...')
while True:
try:
print('PH Value: {:2.1f}'.format(sensor.PH))
time.sleep(1)
except KeyboardInterrupt:
print("\nExiting...")
sys.exit(0)
if __name__ == '__main__':
main()
TODO:
Test in multiple solutions for accuracy
Find out how to code the built in temp sensor (if indeed there is one)
Maybe add a noise isolator if 1) doesnt work when I test it in my tank (no idea how successful this will be)
Integrate the code into a bash script which will:
Turn on my CO2 supply at a specified time via a wifi sonoff or similar before so I dont have to integrate with 240v supply or I may get a separate DC power supply and use a relay hat on the PI to control it.
Detect the PH, when it reaches ~ph 6, switch off the power on my co2 solenoid.
Send all the debug and results via pushover to my phone.
Parting Notes:
The AnyLeaf board has its own set of challenges. First and foremost, the Raspberry Pi 3B is a poor choice for any of the pH probes I have tried. I could not get any of them to work with the 3B and the dev recommended to try a zero or a 4. If you have any issues with values that donāt make sense, get a new probe. Your pH values should not bounce around wildly. The calibration routine built into the example script is a bit cumbersome but it works. If you get any readings that seem off par, recalibrate.
I have noticed that the probe stays in calibration until you add any new peripheral device to the pi via GPIO. I have had my settings persist after reboot but be prepared to calibrate it often. If the probe is in your tank 24 / 7, I donāt think you will need to recalibrate as often. I will find out here in a few weeks how well calibration stays.
I really appreciate everyone in this forum. Thank you for the support!
After some more testing my final verdict for the blue seeed pH-electrode: donĀ“t buy it. Not surprisingly, I know. I tested with different solutions of known pH (4, 7, 9) and checked the raw values coming from the board. While there is a clear difference between the values of pH 4 and 7 there is nearly no difference between 7 and 9. So all calibration attempts are useless.
Just out of curiosity I will order a pH-electrode with a BNC-connector for aquaristics and plug it into my setup. I`ll set myself a cost limit of 50 Euro and see what I can get for the money. Will post the findings here, but it might take some time.
I am using a pH-electrode from an aquaristics supply vendor at the moment, the rest of the setup is unchanged. However, I wasnĀ“t able to calibrate the new electrode. I had the same behaviour as with the seeed electrode, at pH 7 the raw value was more or less constant when moving into higher pH-ranges. Maybe itĀ“s a problem with the interface board, I donĀ“t know and leave it at that. So right now the pH-values are more of a qualitative nature, one could see sudden drops in value but the explicit value measured ist more or less a guideline.
Another interesting fact: as soon as the lights go down in the aquarium the pH-value goes up significantly to stay at that higher level until the lights go on again. This fits nicely with something stated way above this post: small currents leaking from the devices in the aquarium into the water can have a huge effect on measurements. I will code some time-based correction factor into my script, as I know exactly when the lights are on or off.
Thats it from me, I learned a lot with this project and really appreciate the friendly discussion and support here.