-
Notifications
You must be signed in to change notification settings - Fork 0
/
pHSensorReaderTouchScreen.py
60 lines (53 loc) · 2.02 KB
/
pHSensorReaderTouchScreen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#import relevant libraries
import sys
import time
import spidev
import RPi.GPIO as GPIO
import tkinter as tk
from tkinter import *
# set up GPIO pin configuration
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.OUT)
servo = GPIO.PWM(21, 50)
servo.start(2.5)
msg = " "
# set up SPI for pH Sensor as main program focus
spiMain = spidev.SpiDev() #create spi object
spiMain.open(0, 0) #open spi port 0, device (CS) 0, for the MCP3008
spiMain.max_speed_hz=1000000
#configure analog to digital chip pin configuration
def readadc(adcnum): #read out the ADC
if ((adcnum > 7) or (adcnum < 0)):
return -1
r = spiMain.xfer2([1, (8 + adcnum) << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
class pHReaderFrame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
#program statements for frame properties
self.geometry('1000x500')
self.resizable(False, False)
self.grid()
self.frameSpacer = tk.Label(self, text=" ",font=("Arial", 50)).grid(column=0, row=1, padx=100, pady=20)
self.frameButton = tk.Button(self, text="Quit", command=self.destroy).grid(column=0, row=2, padx=100, pady=20)
#start frame loop running
self.mainLoop()
def mainLoop(self):
global msg
#call readadc function to retrieve input from pH sensor device
returnedValue = readadc(1) #read adc channel 1
calculatedValue = float(returnedValue / 1024) * (3.3 / 1000) #reading is in millivolts
gainvalue = 4665
calibrationValue = 1.045
pHValue = (14 - (gainvalue * calculatedValue * calibrationValue))
msg = "pH Value: " + str(round(pHValue, 2))
#diagnostic message to check prgram execution
print(msg)
#update Label value
self.frameLabel = tk.Label(self, text=str(msg),font=("Arial", 50)).grid(column=0, row=0, padx=100, pady=20)
self.after(5000, self.mainLoop)
if __name__=="__main__":
app = pHReaderFrame()
app.mainloop()