-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINA219_PowerMonitor.py
52 lines (40 loc) · 1.48 KB
/
INA219_PowerMonitor.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
#!/usr/bin/env python3
# The INA219 source is from https://github.com/chrisb2/pi_ina219
# and helped me a lot! Thanks!
# ----------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------
from ina219 import INA219
import RPi.GPIO as GPIO
import time
# ----------------------------------------------------------------------
# Defines
# ----------------------------------------------------------------------
INTERVAL_SEC = 2
SHUNT_OHMS = 0.1
MAX_EXPECTED_AMPS = 1.0
GPIO_LED = 26
ALARM_THRESHOLD_VOTLS = 5.6
# ----------------------------------------------------------------------
# Globals
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# "Main" start...
# ----------------------------------------------------------------------
# setup GPIOs
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_LED, GPIO.OUT, initial = GPIO.HIGH)
ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS)
ina.configure(ina.RANGE_16V, ina.GAIN_AUTO)
try:
while True:
# read voltage
voltage = ina.voltage()
print("Bus Voltage: %.3f V" % voltage)
if(voltage > ALARM_THRESHOLD_VOTLS):
GPIO.output(GPIO_LED, GPIO.HIGH)
else:
GPIO.output(GPIO_LED, not GPIO.input(GPIO_LED))
time.sleep(INTERVAL_SEC)
except KeyboardInterrupt:
GPIO.cleanup()