-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC712.py
More file actions
32 lines (24 loc) · 712 Bytes
/
AC712.py
File metadata and controls
32 lines (24 loc) · 712 Bytes
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
from machine import ADC
from time import sleep
maxint = 1 << 16
class AC712:
def __init__(self, adc):
self.adc = adc
self.imean = maxint / 2 * 5 / 3.3
self.corr = 3300 / 100 / maxint
def ReadAllData(self):
from math import sqrt
nrsamples = 100
isum = 0.0
isqr = 0.0
for j in range(0, nrsamples):
i = self.adc.read_u16()
isum += i
isqr += ((i - self.imean) * (i - self.imean))
sleep(0.001)
self.imean = (self.imean + isum / nrsamples) / 2
isqrt = sqrt(isqr / nrsamples)
current = isqrt * self.corr
return current
def close(self):
pass