Skip to content

Instantly share code, notes, and snippets.

@guysoft
Created October 28, 2021 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guysoft/1e87e8a2bdf0f9a767af726a0daa5b9f to your computer and use it in GitHub Desktop.
Save guysoft/1e87e8a2bdf0f9a767af726a0daa5b9f to your computer and use it in GitHub Desktop.
'''
MicroPython pH sensor Eduponics mini - demo
https://github.com/STEMinds/micropython-eduponics
MIT License
Copyright (c) 2020 STEMinds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Ph Buffer table:
|--------------------------------------------------------------------------------------------------|
| T(c) | Buffer 1.679 | Buffer 4.008 | Buffer 7.000 | Buffer 9.180 | Buffer 10.012 | Buffer 12.454 |
---------------------------------------------------------------------------------------------------|
| 5 | 1.668 | 3.998 | 7.087 | 9.395 | 10.245 | 13.207 |
| 10 | 1.670 | 3.997 | 7.059 | 9.332 | 10.179 | 13.003 |
| 15 | 1.672 | 3.998 | 7.036 | 9.276 | 10.118 | 12.810 |
| 20 | 1.675 | 4.001 | 7.016 | 9.226 | 10.062 | 12.627 |
| 25 | 1.679 | 4.005 | 7.000 | 9.180 | 10.012 | 12.454 |
| 30 | 1.683 | 4.011 | 6.987 | 9.139 | 9.966 | 12.289 |
| 35 | 1.688 | 4.018 | 6.977 | 9.102 | 9.925 | 12.289 |
| 40 | 1.694 | 4.027 | 6.970 | 9.068 | 9.889 | 12.133 |
| 45 | 1.700 | 4.038 | 6.966 | 9.038 | 9.856 | 11.984 |
| 50 | 1.707 | 4.050 | 6.964 | 9.010 | 9.828 | 11.795 |
----------------------------------------------------------------------------------------------------
'''
from Eduponics import ads1x15,mcp23017
from machine import I2C, Pin
import time
# IO12 reserved for powering the board, define it
power = Pin(12, Pin.OUT)
# activate the board
power.value(1)
# wait 100ms to make sure the MOSFET power is on
time.sleep(0.1)
# setup I2C
i2c = I2C(scl=Pin(33), sda=Pin(32))
# setup adc for the extension board
ads_address = 0x48
mcp_address = 0x27
gain = 1
PH_CHANNEL = 0
EXTRA_CHANNEL = 1
adc = ads1x15.ADS1115(i2c=i2c, address=ads_address,mcp_address=mcp_address, gain=gain)
def read_ph():
'''
pH 4.0 and 7.0 values should be modified accordingly using the potentiometer
'''
# get voltage
adc.mcp.pin(adc.mcp_pins_sheet[EXTRA_CHANNEL], mode=0, value=1)
time.sleep(0.1)
adc_read = adc.read(PH_CHANNEL)
time.sleep(0.1)
adc.mcp.pin(adc.mcp_pins_sheet[EXTRA_CHANNEL], mode=0, value=0)
raw_voltage = adc_read["raw"]
voltage = adc_read["voltage"]
print("voltage: %s" % voltage)
# pH 4.0
_acidVoltage = 3507
# pH 7.0
_neutralVoltage = 2990.5
# calculate slope and intercept
slope = (7.0-4.0)/((_neutralVoltage-1500.0) / 3.0 - (_acidVoltage-1500.0)/3.0)
intercept = 7.0 - slope*(_neutralVoltage-1500.0)/3.0
# get the pH value
_phValue = slope*(raw_voltage-1500.0)/3.0+intercept
# return it
return round(_phValue, 2)
while True:
print("pH: %s" % read_ph())
print("")
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment